2012-03-14 28 views
0

这是我关于GPS当前用户的位置不会检索

Getting 0.0 for latitude and longitude while showing current location in map

最后一个问题,现在,这里是我使用来获取用户当前位置的代码。

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(provider);总是返回null。我获得了供应商的价值作为GPS。

但是,如果我打开我的gps连接,相同的loc对象将具有相关的信息,它的作用偏正确。这意味着,它给了我最近的位置。我的意思是我所坐的城市位置。

但是,如果我在我的gps连接上,它也不会给我连城市的位置,如果只在位置监听器类内部进入循环。我不知道这里出了什么问题。

任何人都可以告诉我如何解决它?

更新:

这是我得到的禄对象,如果我的GPS是关闭

位置值[mProvider =网络,修改时间= 1331718353322,mLatitude = 12.9053401,mLongitude = 74.8359128,mHasAltitude =假,mAltitude = 0.0,mHasSpeed =假,MSPEED = 0.0,mHasBearing =假,mBearing = 0.0,mHasAccuracy =真,mAccuracy = 36.0,mExtras =捆绑[mParcelledData.dataSize = 148]]。

但如果GPS上,LOCA返回NULL

回答

2

CustomLocationManager.Java

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); 
     } 
    } 
} 


LocationValue.Java

import android.location.Location; 

public abstract class LocationValue { 
    public abstract void getCurrentLocation(Location location); 
} 


YourActivity.Java

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()); 
      } 
     } 
    }; 


AndroidManifest.xml中

<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"/> 
+0

谢谢。请给我一些时间来检查此代码。我会回来 – tejas 2012-03-14 10:54:00

+0

超级,非常感谢你拉利特。它工作完美。你能告诉我如何标记用户的确切位置吗?我能够搬到他所站的城市。我想标记一轮,就像我们在地图应用程序中获得的那一轮一样。你介意告诉我怎么做? – tejas 2012-03-14 11:24:02

+0

要绘制标记,只需启用myLocation。这里是代码\t mMyLocationOverlay = new MyLocationOverlay(VenueDescription.this,objMapView); \t \t \t \t \t mMyLocationOverlay.enableMyLocation(); \t \t \t \t \t objMapView.getOverlays()。add(mMyLocationOverlay);并将缩放比例设置为30+以上 – tejas 2012-03-15 09:26:26

0

首先我不熟悉Android API的位置,但你尝试外的GPS?由于GPS在建筑物内不起作用,因此仅使用GPS来检索移动设备的位置非常困难。在室内时,通常使用WiFi访问点连接来确定您的“GPS”位置。

+0

我不认为这是导致我的问题。此外,我尝试在Android手机默认地图应用程序。这很好。我现在怀疑我的代码,但没有得到什么错误。 – tejas 2012-03-14 09:42:56