Android-使用GPS跟踪,getMap已弃用
我试着用gps追踪并在地图上标出位置。我尝试了上面的代码,但是getMap不推荐使用,我的手机上出现了一个屏幕。有人能帮我解决这个问题吗 GetMap()已弃用。您需要在类中实现Android-使用GPS跟踪,getMap已弃用,android,google-maps,gps,Android,Google Maps,Gps,我试着用gps追踪并在地图上标出位置。我尝试了上面的代码,但是getMap不推荐使用,我的手机上出现了一个屏幕。有人能帮我解决这个问题吗 GetMap()已弃用。您需要在类中实现OnMapReadyCallback public class LocationActivity extends FragmentActivity implements LocationListener, GoogleApiClient.ConnectionCallbacks,
OnMapReadyCallback
public class LocationActivity extends FragmentActivity implements
LocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = "LocationActivity";
private static final long INTERVAL = 1000 * 60 * 1; //1 minute
private static final long FASTEST_INTERVAL = 1000 * 60 * 1; // 1 minute
Button btnFusedLocation;
TextView tvLocation;
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
Location mCurrentLocation;
String mLastUpdateTime;
GoogleMap googleMap;
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!isGooglePlayServicesAvailable()) {
finish();
}
createLocationRequest();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
setContentView(R.layout.activity_location_google_map);
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
googleMap = fm.getMap();
googleMap.getUiSettings().setZoomControlsEnabled(true);
}
}
并重写:onMapReady()
函数
public class LocationActivity extends FragmentActivity implements
LocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, OnMapReadyCallback {
另外,在代码中添加:
@Override
public void onMapReady(GoogleMap map) {
googleMap = map;
googleMap.getUiSettings().setZoomControlsEnabled(true);
//add code here
}
在布局上:
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
fm.getMapAsync(this);
您可以按照这个很好的答案正确地实现映射:我已经尝试过了,但是它说“尝试在空对象引用上调用getMapAsync(..)。您是否调用了fm.getMapAsync(this);
?是的,MapFragment MapFragment=(MapFragment)getFragmentManager().findFragmentById(R.id.map);mapFragment.getMapAsync(this);使用:SupportMapFragment fm=(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);fm.getMapAsync(this);
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent">