我是Android編程新手。我正在開發一個需要訪問Google地圖的Android項目。我有一個包含谷歌地圖位置的選項的導航抽屜。Map Fragment不加載相同的GPS位置和地圖類型
因此,無論何時我第一次嘗試加載地圖時,它都能正常工作,並使用當前GPS位置和定義的MAP TYPE加載地圖。
但是,每當我嘗試切換到其他片段並回到地圖 片段時,GPS功能和地圖類型不起作用。
需要幫助。提前致謝。我正在嘗試修復它。 :)
我的代碼如下。
我想在這裏做的是,每當用戶觸摸的地方,一個 標記將被放置在該位置。
public class AddCustomerLocationFragment extends Fragment implements
LocationListener {
private MapView mapView;
private GoogleMap map;
private Marker marker;
private boolean markerAvailable;
private Bundle bundle;
Button btnReset, btnSetLocation;
LocationManager locationManager;
private View view;
private double[] addCustomerLocation = new double[10];
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_addcustomer_location,
container, false);
MapsInitializer.initialize(getActivity());
mapView = (MapView) v.findViewById(R.id.map);
btnReset = (Button) v.findViewById(R.id.btnReset);
btnSetLocation = (Button) v.findViewById(R.id.btnSetLocation);
mapView.onCreate(bundle);
map.setMyLocationEnabled(true);
locationManager = (LocationManager) getActivity()
.getSystemService(
Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
getGPS();
} else {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 1000, 10, this);
}
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
map.getUiSettings().setMyLocationButtonEnabled(true);
map.getUiSettings().setCompassEnabled(true);
map.setOnMapClickListener(new OnMapClickListener() {
public void onMapClick(LatLng point) {
Toast.makeText(
getActivity(),
"Latitude:" + point.latitude + ", Longitude:"
+ point.longitude, Toast.LENGTH_SHORT).show();
addMarker(point);
markerAvailable = true;
}
});
btnSetLocation.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(
getActivity(),
"Your current loaction is set to: Latitude:"
+ addCustomerLocation[0] + ", Longitude:"
+ addCustomerLocation[1], Toast.LENGTH_SHORT)
.show();
}
});
btnReset.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
marker.remove();
Toast.makeText(getActivity(), "Marker is Reset",
Toast.LENGTH_SHORT).show();
markerAvailable = false;
}
});
// add a marker
//
return v;
}
private void getGPS() {
// TODO Auto-generated method stub
onLocationChanged(locationManager
.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER));
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bundle = savedInstanceState;
setRetainInstance(true);
}
@Override
public void onResume() {
super.onResume();
mapView.onResume();
getGPS();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
private void outGPS() {
// TODO Auto-generated method stub
locationManager.removeUpdates(this);
}
@Override
public void onDestroy() {
mapView.onDestroy();
super.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
// @Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
LatLng latLng = null;
if (!markerAvailable) {
latLng = new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(
latLng, 18f);
map.animateCamera(cameraUpdate);
addMarker(latLng);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(final String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
private void addMarker(LatLng point) {
map.clear();
marker = map.addMarker(new MarkerOptions().position(
new LatLng(point.latitude, point.longitude)).title(
"Your Location!?"));
}
}
有什麼問題? –
當我從地圖片段切換到另一個片段並返回到地圖片段時,地圖不會以HYBRID類型加載,也不會有我定義的默認GPS位置。 –