我正在尝试将谷歌地图 API 用于学校项目。当我使用两个硬编码数字创建 LatLng 对象时, map 效果很好。当我尝试使用两个双变量时它不起作用?请帮忙,我快疯了!
请相信我,变量纬度和经度被分配给完美的 double 值,当打印为字符串时恰好是 33.190802, -117.301805
//WORKS PERFECTLY
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
//Specify our location with latlng class
LatLng myPostition = new LatLng(33.190802, -117.301805);
//Add our position to the map
map.addMarker(new MarkerOptions().position(myPostition).title("Current Location").icon(BitmapDescriptorFactory.fromResource(R.drawable.my_marker)));
//move camera to our position
CameraPosition cameraPosition = new CameraPosition.Builder().target(myPostition).zoom(10.0f).build();
//update the position --> move to the location
CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
//instruct the map to move to this position
map.moveCamera(cameraUpdate);
//DOSEN'T WORK--But I need latitude and longitude to be set programmatically
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
//Specify our location with latlng class
LatLng myPostition = new LatLng(latitude, longitude);
//Add our position to the map
map.addMarker(new MarkerOptions().position(myPostition).title("Current Location").icon(BitmapDescriptorFactory.fromResource(R.drawable.my_marker)));
//move camera to our position
CameraPosition cameraPosition = new CameraPosition.Builder().target(myPostition).zoom(10.0f).build();
//update the position --> move to the location
CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
//instruct the map to move to this position
map.moveCamera(cameraUpdate);
最佳答案
您的变量中没有有效的 avlue
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
latitude = 33.190802;
longitude = -117.301805;
//Specify our location with latlng class
LatLng myPostition = new LatLng(latitude, longitude);
......
您可能需要一种将有效值传递给您的变量的方法
public void onMapReady(GoogleMap googleMap, latitude, longitude) {
map = googleMap;
LatLng myPostition = new LatLng(latitude, longitude);
......
关于java - 如果使用双变量实例化 LatLng 对象,则该对象将不起作用,只有在输入实数时才起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56198315/