Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当前用户位置未检索_Android_Google Maps_Map_Gps - Fatal编程技术网

Android 当前用户位置未检索

Android 当前用户位置未检索,android,google-maps,map,gps,Android,Google Maps,Map,Gps,这是我关于gps的最后一个问题 下面是我用来获取用户当前位置的代码 LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Criteria crta = new Criteria(); crta.setAccuracy(Criteria.ACCURACY_FINE); crta.setAltitudeRequired

这是我关于gps的最后一个问题

下面是我用来获取用户当前位置的代码

 LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria crta = new Criteria();
        crta.setAccuracy(Criteria.ACCURACY_FINE);
        crta.setAltitudeRequired(false);
        crta.setBearingRequired(false);
        crta.setCostAllowed(true);
        crta.setPowerRequirement(Criteria.POWER_LOW);
        String provider = mlocManager.getBestProvider(crta, true);
        Location loc = null;
        if (provider != null) {
            loc = mlocManager.getLastKnownLocation(provider);
        }
        LocationListener mlocListener = new MyLocationListener();
        mlocListener.onLocationChanged(loc);
        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                2000, 10, mlocListener);


public class MyLocationListener implements LocationListener{
    public MyLocationListener() {
    }

    @Override
    public void onLocationChanged(Location loc) {
        if (null != loc) {
        String Text = "Your current location is: \n" + "Latitude = \n"
        + loc.getLatitude() + "\nLongitude = \n" + loc.getLongitude();
        Toast.makeText(getApplicationContext(),Text,Toast.LENGTH_SHORT).show();

        GeoPoint myGeoPoint = new GeoPoint((int)(loc.getLatitude()*1E6),(int)(loc.getLongitude()*1E6));
         mpc.animateTo(myGeoPoint);
         mpc.setZoom(10);
         objMapView.invalidate();

        }
    }

    @Override
    public void onProviderDisabled(String provider){
        Toast.makeText(getApplicationContext(), "gps disabled",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(getApplicationContext(), "gps enabled",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }
}
现在我面临的问题是,当gps开启时,它不会向我显示当前位置。loc对象,loc=mlocManager.getLastKnownLocation(提供者);始终返回null。我得到了作为gps的提供商的值

但是如果我打开我的gps连接,同一个loc对象将具有相关信息,并且它工作正常。也就是说,它给了我最近的位置。我指的是我所在的整个城市

但如果我在我的gps连接上,它甚至也不给我城市位置,不进入if循环只在位置侦听器类内。我不明白这里出了什么问题

有人能告诉我怎么解决吗

更新:

这是我的gps关闭时loc对象的值

位置[mProvider=network,mTime=1331718353322,mLatitude=12.9053401,mLongtude=74.8359128,mHasAltude=false,Maltude=0.0,mHasSpeed=false,mHasBeating=false,mBaring=0.0,mHasAccuracy=true,mAccuracy=36.0,mXtras=Bundle[mParceledData=148]]


但是,如果gps开启,loca返回null

首先,我不熟悉Android定位API,但是你在外面尝试过gps吗?由于GPS在建筑物内不工作,因此仅使用GPS很难检索移动设备的位置。在室内时,通常使用WiFi接入点连接来确定“GPS”位置。

CustomLocationManager.Java


LocationValue.Java


YourActivity.Java


AndroidManifest.xml



我不认为这是问题的根源。我还尝试了android手机上的默认地图应用程序。这很好。我现在怀疑我的代码,但没有弄清楚到底哪里出了问题。谢谢。请给我一些时间检查一下这个代码。我会很高兴的,非常感谢你。它工作得很好。你能告诉我现在如何标记用户的确切位置吗。我可以搬到他所在的城市。我想标记一个圆,就像我们在地图应用程序中得到的那样。你介意告诉我怎么做吗?要画标记,只需启用我的位置。下面是代码mMyLocationOverlay=新的MyLocationOverlay(VenueDescription.this,objMapView);mMyLocationOverlay.enableMyloLocation();objMapView.getOverlays().add(mMyLocationOverlay);并将“缩放”设置为更高的数字,如30+
import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class CustomLocationManager {

    private LocationManager mLocationManager;
    private LocationValue locationValue;
    private Location networkLocation = null;
    private Location gpsLocation = null;

    private Timer mTimer;

    private boolean isGpsEnabled = false;
    private boolean isNetworkEnabled = false;

    private static CustomLocationManager _instance;

    private CustomLocationManager() {}

    public static CustomLocationManager getCustomLocationManager() {
        if (_instance == null) {
            _instance = new CustomLocationManager();
        }
        return _instance;
    }

    public LocationManager getLocationManager(Context context) {
        if (mLocationManager == null)
            mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        return mLocationManager;
    }

    public boolean getCurrentLocation(Context context, LocationValue result) {
        locationValue = result;
        if (mLocationManager == null)
            mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        try {
            isGpsEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        } catch (Exception ex) {}

        try {
            isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        } catch (Exception ex) {}

        if (!isGpsEnabled && !isNetworkEnabled)
            return false;

        if (isGpsEnabled)
            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, gpsLocationListener);

        if (isNetworkEnabled)
            mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, networkLocationListener);

        mTimer = new Timer();
        mTimer.schedule(new GetLastKnownLocation(), 20000);

        return true;
    }

    LocationListener gpsLocationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            mTimer.cancel();
            locationValue.getCurrentLocation(location);
            mLocationManager.removeUpdates(this);
            mLocationManager.removeUpdates(networkLocationListener);
        }

        public void onProviderDisabled(String provider) {}

        public void onProviderEnabled(String provider) {}

        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };

    private LocationListener networkLocationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            mTimer.cancel();
            locationValue.getCurrentLocation(location);
            mLocationManager.removeUpdates(this);
            mLocationManager.removeUpdates(gpsLocationListener);
        }

        public void onProviderDisabled(String provider) {}

        public void onProviderEnabled(String provider) {}

        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };

    private class GetLastKnownLocation extends TimerTask {
        CurrentLocationHandler handler;

        GetLastKnownLocation() {
            handler = new CurrentLocationHandler();
        }

        @Override
        public void run() {
            mLocationManager.removeUpdates(gpsLocationListener);
            mLocationManager.removeUpdates(networkLocationListener);

            if (isGpsEnabled)
                gpsLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

            if (isNetworkEnabled)
                networkLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

            handler.sendEmptyMessage(0);
        }
    }

    private class CurrentLocationHandler extends Handler {
        @Override
        public final void handleMessage(Message msg) {
            if (gpsLocation != null && networkLocation != null) {

                if (gpsLocation.getTime() > networkLocation.getTime())
                    locationValue.getCurrentLocation(gpsLocation);
                else
                    locationValue.getCurrentLocation(networkLocation);

                return;
            }

            if (gpsLocation != null) {
                locationValue.getCurrentLocation(gpsLocation);
                return;
            }

            if (networkLocation != null) {
                locationValue.getCurrentLocation(networkLocation);
                return;
            }

            locationValue.getCurrentLocation(null);
        }
    }
}
import android.location.Location;

public abstract class LocationValue {
    public abstract void getCurrentLocation(Location location);
}
private void getCurrentLocation() {
        CustomLocationManager.getCustomLocationManager().getCurrentLocation(this, locationValue);
    }

    public LocationValue locationValue = new LocationValue() {
        @Override
        public void getCurrentLocation(Location location) {
            // You will get location here if the GPS is enabled
            if(location != null) {
                Log.d("LOCATION", location.getLatitude() + ", " + location.getLongitude());
            }
        }
    };
  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>