2013-03-23 80 views
1

我使用此代码查找我的位置(经度和纬度),但此代码有时会快速(几秒钟)响应
,有时会延迟超过10分钟。 问题在哪里?位置查找响应

public class MainActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10L, 
       5.0f, locationListener); 
    } 

    private void updateWithNewLocation(Location location) { 
     TextView myLocationText = (TextView) findViewById(R.id.text); 
     String latLongString = ""; 
     if (location != null) { 
      double lat = location.getLatitude(); 
      double lng = location.getLongitude(); 
      latLongString = "Lat:" + lat + "\nLong:" + lng; 

     } else { 
      latLongString = "No location found"; 
     } 
     myLocationText.setText("Your Current Position is:\n" + latLongString); 
    } 

    private final LocationListener locationListener = new LocationListener() { 

     @Override 
     public void onLocationChanged(Location location) { 
      updateWithNewLocation(location); 
     } 

     @Override 
     public void onProviderDisabled(String provider) { 
      updateWithNewLocation(null); 
     } 

     @Override 
     public void onProviderEnabled(String provider) { 
     } 

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

} 

回答

1

您正在使用GPS_PROVIDER获取位置。

GPS->此提供商使用卫星确定位置。根据条件,此提供商可能需要一段时间才能返回定位,并提供约20英尺的精确结果。

您可以使用的另一个提供程序是NETWORK_PROVIDER。 该提供商根据蜂窝塔和WiFi接入点的可用性来确定位置,并给出准确的结果大约200英尺,并且它消耗较少的内存,然后GPS_PROVIDER。 NETWORK_PROVIDER使用此权限

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />