如何在Google Maps Android API V2中获取位置及其更新

Bas*_*sit 1 android google-maps-android-api-2

我想在Google Maps Android API v2中启用位置.我想要的是:

  1. 以固定纬度/经度为中心加载地图.
  2. 开始收听位置更改,一旦提供者(网络/ GPS)获取位置,将地图置于此位置的中心并为其设置动画(动画仅运行一次).同时,位置监听器在指定的时间量后更新用户的位置,
  3. 存储此位置数据并使用它通过我的API获取附近的景点.

我对如何定期更新,选择时间间隔的数量感到困惑.现在,我已经设置了项目来显示地图并使用

googleMap.setMyLocationEnabled(true);
googleMap.setOnMyLocationChangeListener(myLocationChangeListener);

private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
    @Override
    public void onMyLocationChange(Location location) {
        LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
        if(googleMap != null){
            if(!firstTime){
                CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(loc).zoom(zoom).build();
                googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 
                firstTime = true;
            }

            lat = location.getLatitude();
            lng = location.getLongitude();
            Toast.makeText(context, "Location is: " + String.valueOf(lat) + ", " 
                    + String.valueOf(lng), Toast.LENGTH_LONG).show();
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

在变量中设置lat/lng.我的问题是:

  1. 这是正确的方法吗?或者可以做得更好?
  2. 位置更改侦听器经常更新自身.我看到几秒钟内就会制作吐司.如何规范呢?
  3. 如何在活动消失(暂停/停止)时阻止侦听器获取位置?谢谢!

and*_*her 5

问题2:

使用 requestLocationUpdates(long minTime, float minDistance, Criteria criteria, PendingIntent intent)

前两个参数是:

参数minTime:位置更新之间的最小时间间隔,以毫秒为单位minDistance:位置更新之间的最小距离(以米为单位)

           private static final long POLLING_FREQ = 1000 * 10;
           private static final float MIN_DISTANCE = 10.0f;

           // Reference to the LocationManager and LocationListener
           private LocationManager mLocationManager;
           private LocationListener mLocationListener;

           // Register for network location updates
            if (null != mLocationManager
                    .getProvider(LocationManager.NETWORK_PROVIDER)) {
                mLocationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER, POLLING_FREQ,
                        MIN_DISTANCE, mLocationListener);
            }

            // Register for GPS location updates
            if (null != mLocationManager
                    .getProvider(LocationManager.GPS_PROVIDER)) {
                mLocationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER, POLLING_FREQ,
                        MIN_DISTANCE, mLocationListener);
            }
Run Code Online (Sandbox Code Playgroud)

您可以根据自己的需要进行调整.

问题3:

使用:removeUpdates(PendingIntent intent)删除指定挂起意图的所有位置更新.

截至问题1.

你的方法似乎合理.如果你需要不断的位置更新,那么我可以建议其他的东西来减少电池消耗.

     mLocationListener = new LocationListener() {

    // Called back when location changes

    public void onLocationChanged(Location location) {


        // Get your location here
        // estimate

    }

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

    public void onProviderEnabled(String provider) {
        // NA
    }

    public void onProviderDisabled(String provider) {
        // NA
    }
};
Run Code Online (Sandbox Code Playgroud)