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
}
}
最佳答案
这是我在第一次位置更新时导航到 map 中心的方式。
我的类(class)标题:
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);
}
像魅力一样工作
关于android - 在 Google Maps Android API v2 中使用新的 OnMyLocationChangeListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15104044/