我试图获取 map fragment 中的当前位置,但由于 googleMap.setMyLocationEnabled(true); 行中的错误而崩溃。 我使用 Location 和 Latlang 来检测当前位置。
mapFragment.java
public class mapFragment extends Fragment {
MapView mMapView;
private GoogleMap googleMap;
Location location;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflat and return the layout
View v = inflater.inflate(R.layout.fragment_map, container,
false);
mMapView = (MapView) v.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();// needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
googleMap.setMyLocationEnabled(true);
googleMap = mMapView.getMap();
// latitude and longitude
LatLng lat = new LatLng(location.getLatitude(), location.getLongitude());
/*double latitude = 17.385044;
double longitude = 78.486671;*/
// create marker
MarkerOptions marker = new MarkerOptions().position(
lat).title("Hello Maps");
// Changing marker icon
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
// adding marker
googleMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(lat).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
// Perform any camera updates here
return v;
}
@Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}
}
fragment_map.xml
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
最佳答案
看起来您遇到了一些问题,但崩溃正在发生,因为您需要在调用 setMyLocationEnabled()
之前调用 getMap()
。
更改此:
googleMap.setMyLocationEnabled(true);
googleMap = mMapView.getMap();
...对此:
googleMap = mMapView.getMap();
googleMap.setMyLocationEnabled(true);
更新:我能够让这段代码正常工作。
获取当前位置应使用 Fused Location Provider API 来完成,但这超出了这个问题的范围。
此示例代码中的方法可能适用于较旧的设备,但在 4.4.4 上,由于 getMyLocation()
已被弃用,它会返回空位置。
但是,在进行空检查后, map 会正确显示,您可以单击位置按钮将 map 移动到您当前的位置。
MapView mMapView;
private GoogleMap googleMap;
Location location;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflat and return the layout
View v = inflater.inflate(R.layout.fragment_main, container,
false);
mMapView = (MapView) v.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();// needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
googleMap = mMapView.getMap();
googleMap.setMyLocationEnabled(true);
location = googleMap.getMyLocation();
if (location != null) {
// latitude and longitude
LatLng lat = new LatLng(location.getLatitude(), location.getLongitude());
//double latitude = 17.385044;
//double longitude = 78.486671;
//LatLng lat = new LatLng(latitude,longitude);
// create marker
MarkerOptions marker = new MarkerOptions().position(
lat).title("Hello Maps");
// Changing marker icon
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
// adding marker
googleMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(lat).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}
// Perform any camera updates here
return v;
}
关于java - 无法获取 Google map fragment 中的当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29480717/