我正在使用应用程序,我必须在地图上显示没有Marker。我已经通过片段展示地图,现在想要使用markerOptions()
来获取我的位置。但是这显示了GoogleMap null
public class MapFineTech extends Fragment {
GoogleMap googleMap;
double getlatitude,getlongitude;
String latitude_mString,longitude_mString;
MapView mapView;
ViewGroup container;
Fragment fragment;
GPSTracker gps;
MarkerOptions marker;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.map_fine_tech, container, false);
fragment = getFragmentManager().findFragmentById(R.id.map);
Log.d("u r", "ur in register device");
new Thread(null,registerDeviceThread,"").start();
gps = new GPSTracker(getActivity());
if(gps.canGetLocation()){
getlatitude = gps.getLatitude();
getlongitude = gps.getLongitude();
latitude_mString = String.valueOf(getlatitude);
longitude_mString = String.valueOf(getlongitude);
Log.d("Value of lat and long", latitude_mString+""+longitude_mString);
Log.d("u r", "ur in register lati loni");
new Thread(null,latlongThread,"").start();
}else{
gps.showSettingsAlert();
}
Log.d("Value of lat and long", latitude_mString+""+longitude_mString);
marker = new MarkerOptions().position(new LatLng(getlatitude, getlongitude));
googleMap.addMarker(marker);
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.applogo));
return rootView;
}
@Override
public void onDestroyView() {
// TODO Auto-generated method stub
try{
if (fragment != null) {
fragment = getFragmentManager().findFragmentById(R.id.map);
FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}
}
catch(Exception e)
{
e.printStackTrace();
}
Thread.interrupted();
super.onPause();
}
通过此代码映射可以显示,但是当我将addMarker添加到googleMap时,由于googleMap而导致的app crasher为null。
答案 0 :(得分:0)
作为这个问题的答案:
进口
com.google.android.gms.maps.MapFragment
并编辑
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.map_fine_tech, container, false);
fragment = getFragmentManager().findFragmentById(R.id.map);
googleMap = ((MapFragment) fragment).getMap(); // this is the edit
......// the rest of the code
}