bra*_*hy1 1 android google-maps-android-api-2
Google最终在Android API v2中添加了位置更改回调!但是,我无法直观地将其用于工作,Google也没有太多文档.有没有人让它上班?我还需要什么?
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
}
}
Run Code Online (Sandbox Code Playgroud)
这是我在第一次进行位置更新时导航到地图中心的方法.
我的班级标题:
public class FragActivity extends SherlockFragmentActivity implements OnMyLocationChangeListener
private GoogleMap mMap;
Run Code Online (Sandbox Code Playgroud)
我的mMap-setup:
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();
}
Run Code Online (Sandbox Code Playgroud)
setUpMap法:
private void setUpMap() {
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationChangeListener(this);
}
Run Code Online (Sandbox Code Playgroud)
和我的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);
}
Run Code Online (Sandbox Code Playgroud)
奇迹般有效
| 归档时间: |
|
| 查看次数: |
8803 次 |
| 最近记录: |