gpt4 book ai didi

java - Android CameraUpdateFactory 未初始化

转载 作者:行者123 更新时间:2023-11-30 01:04:59 24 4
gpt4 key购买 nike

我正在尝试使用谷歌地图开发 android 应用程序,但出现错误 -

NullPointerException: CameraUpdateFactory is not initialized

    GoogleMap mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);


Criteria criteria = new Criteria();


String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 20000, 0, this);

double lat = Double.parseDouble("xx.xxxxxx");
double log = Double.parseDouble("yy.yyyyyy");
LatLng currentPosition = new LatLng(lat, log);

mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentPosition,16));
mMap.addMarker(new MarkerOptions()
.position(currentPosition)
.snippet("Lat:" + lat + "Lng:" + log));

请帮帮我。提前致谢。

最佳答案

你需要实现

OnMapReadyCallback

然后覆盖

onMapReady

这是为了确保您的 map 已正确设置并准备好进行任何相机更新。

public class YourMapFragment extends Fragment implements OnMapReadyCallback { 
...
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentPosition,16));

mMap.addMarker(new MarkerOptions()
.position(currentPosition)
.snippet("Lat:" + lat + "Lng:" + log));
}

...
}

关于java - Android CameraUpdateFactory 未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38952556/

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