我有以下函数,可以返回设备的当前位置:
但是一些方法显示为红色,就像它们未定义一样: 正如您所注意到的那样,这些方法与 map 有关,在 onMapReady() 函数中使用时可以正常工作,但在外部却不被识别。为什么会这样?我需要添加哪些库?我是这样声明 map 的:
void getCurrentLocation()
{
Location myLocation = map.getMyLocation();
if(myLocation!=null)
{
double dLatitude = myLocation.getLatitude();
double dLongitude = myLocation.getLongitude();
map.addMarker(new MarkerOptions().position(new LatLng(dLatitude, dLongitude))
.title("My Location").icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(dLatitude, dLongitude), 8));
}
else
{
Toast.makeText(this, "Unable to fetch the current location", Toast.LENGTH_SHORT).show();
}
}
但是一些方法显示为红色,就像它们未定义一样: 正如您所注意到的那样,这些方法与 map 有关,在 onMapReady() 函数中使用时可以正常工作,但在外部却不被识别。为什么会这样?我需要添加哪些库?我是这样声明 map 的:
private MapFragment map;
getCurrentLocation()
放在哪里? - Muhammad Shahzadgeo fix xxx xxxx
和其他所有可能的方法,但都失败了。 - Muhammad Shahzad