2

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
        }
}
4

1 回答 1

7

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

我的班级标题:

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

和我的位置变化:

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

奇迹般有效

于 2013-02-27T13:03:16.573 回答