如何在片段中使用 markerOptions 以使用 LatLng() 方法获取 MyLocation
How to use markerOptions in fragment to get MyLocation using LatLng() Method
我正在开发应用程序,我必须在 map.I 上显示没有标记通过片段膨胀地图,现在想通过使用 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 导致的应用程序崩溃为 null。
作为对这个问题的回答:
进口
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
}
我正在开发应用程序,我必须在 map.I 上显示没有标记通过片段膨胀地图,现在想通过使用 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 导致的应用程序崩溃为 null。
作为对这个问题的回答:
进口
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
}