【问题标题】:Using new OnMyLocationChangeListener in Google Maps Android API v2在 Google Maps Android API v2 中使用新的 OnMyLocationChangeListener
【发布时间】:2013-02-27 04:08:49
【问题描述】:

Google 终于在 Android API v2 中添加了位置更改回调!但是,我不能直观地让它工作,而且谷歌没有太多的文档。有没有人让它工作?我还需要什么?

    public class ... extends SupportMapFragment implements GoogleMap.OnMyLocationChangeListener {
GoogleMap map;
LocationManager locationManager;
String provider;

        @Override
        public void onActivityCreated(android.os.Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            map = getMap();
                    if (map != null) {
                       Criteria criteria = new Criteria();
                       criteria.setAccuracy(Criteria.ACCURACY_FINE);
                           locationManager =(LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
                provider = locationManager.getBestProvider(criteria, false);
            }
        }

        @Override
        public void onResume() {
            super.onResume();
            while(map == null) {
                map = getMap();
                map.setMyLocationEnabled(true);
                map.setOnMyLocationChangeListener(this);
            }
        }
        @Override
        public void onMyLocationChange(Location loc) {
            //implementation
        }
}

【问题讨论】:

标签: android google-maps-android-api-2


【解决方案1】:

当我们获得第一次位置更新时,这就是我导航到地图中心的方式。

我的班级标题:

public class FragActivity extends SherlockFragmentActivity implements  OnMyLocationChangeListener

private GoogleMap mMap;

我的 mMap 设置:

    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = customMapFragment.getMap();

        // Check if we were successful in obtaining the map.
        if (mMap != null)
            setUpMap();
    }

setUpMap-方法:

private void setUpMap() {
    mMap.setMyLocationEnabled(true);
    mMap.setOnMyLocationChangeListener(this);
}

还有我的 onlocationchange:

@Override
public void onMyLocationChange(Location lastKnownLocation) {
    CameraUpdate myLoc = CameraUpdateFactory.newCameraPosition(
            new CameraPosition.Builder().target(new LatLng(lastKnownLocation.getLatitude(),
                    lastKnownLocation.getLongitude())).zoom(6).build());
    mMap.moveCamera(myLoc);
    mMap.setOnMyLocationChangeListener(null);
}

像魅力一样工作

【讨论】:

  • 我这样做了,但是,第一次位置更改更新从未到来。我正在“customMapFragment”中执行此过程。会不会是这个问题?
  • 我使用了以“update”开头的解决方案。 stackoverflow.com/questions/13742551/…
  • 您必须安装 google play services rev >=5 才能使用 OnMyLocationChangeListener
  • @brainmurphy1 :comment line mMap.setOnMyLocationChangeListener(null);
  • 它有效,但地图右上角还有 MyLocation 按钮。我们怎样才能删除它?方法如下:googleMap.getUiSettings().setMyLocationButtonEnabled(false);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-24
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多