gpt4 book ai didi

java - onLocationChanged() 中的 map.moveCamera

转载 作者:太空宇宙 更新时间:2023-11-04 10:17:22 25 4
gpt4 key购买 nike

我遇到了这样的问题:我在我的应用程序中的 Fragment 中使用 GoogleMaps map ,并且在调用 onLocationChanged 方法时它会中断。问题是这一行

mMap.moveCamera(CameraUpdatesFactory.newLatLngZoom(position, 15);

可能是因为它找不到 mMap 或者我不知道。这是代码部分:

public class home extends AppCompatActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener{

GoogleMap mMap;
Marker marker;

private void checkAndStartLocationUpdate() {
if (permissionGranted && googleApiClientReady) {
Log.d("MainActivity", "Start updating location");
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
try {

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

} catch (SecurityException e) {
e.printStackTrace();
}
}
}

public void onLocationChanged(Location location) {

Log.d("MainActivity", "Location update received: " + location.toString());

latitude = location.getLatitude();
longitude = location.getLongitude();

Log.d("MainActivity", longitude + " " + latitude);

LatLng position = new LatLng(longitude, latitude);
marker.setPosition(position);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 15));

}

最佳答案

你可以做任何一个

@Override
public void onMapReady(GoogleMap googleMap)
{
mMap = googleMap;

// Tika
// Tika
// Tika
}

并改变

mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 15));

if(mMap != null)
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 15));

或者您可以简单地调用

    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

onMapReady内部之后mMap = googleMap;

关于java - onLocationChanged() 中的 map.moveCamera,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51539209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com