我使用google maps api v2製作自定義android地圖。我希望把一個標記用戶當前location.my代碼:無法在當前位置添加標記
// Enabling MyLocation in Google Map
getMap().setMyLocationEnabled(true);
// BY THIS YOU CAN CHANGE MAP TYPE
// mGoogleMap.setMapType(mGoogleMap.MAP_TYPE_SATELLITE);
try {
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
Currentloc = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (Currentloc != null) {
onLocationChanged(Currentloc);
latitude = Currentloc.getLatitude();
longitude = Currentloc.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (Currentloc == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
Currentloc = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (Currentloc != null) {
onLocationChanged(Currentloc);
latitude = Currentloc.getLatitude();
longitude = Currentloc.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
markerOptions = new MarkerOptions();
// double lat = Currentloc.getLatitude();
// double lng = Currentloc.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
// Setting the position for the marker
markerOptions.position(latLng);
// Setting the title for the marker.
// This will be displayed on taping the marker
markerOptions.title("ADD IMAGE?");
// markerOptions.snippet("ADD IMAGE?");
// Placing a marker on the touched position
getMap().addMarker(markerOptions);
getMap().setOnMarkerClickListener((OnMarkerClickListener) this);
和OnLocationChanged代碼
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
mLatitude = location.getLatitude();
mLongitude = location.getLongitude();
// LatLng latLng = new LatLng(mLatitude, mLongitude);
//markerOptions.notify();
//getMap().addMarker(markerOptions);
getMap().moveCamera(
CameraUpdateFactory.newLatLng(new LatLng(
location.getLatitude(), location.getLongitude())));
getMap().animateCamera(CameraUpdateFactory.zoomTo(12));
}
我運行沒有錯誤的程序。但我的問題是該行
getMap().setMyLocationEnabled(true);
給了我在地圖上的藍色指針用戶當前位置和我手動用戶CURENT位置添加一個標記(AS上面的代碼中)。我的標記和藍色指針必須因爲他們描述相同的位置(相同的經度和緯度),但他們不是。運行我的代碼後,輸出看起來像這個圖像 https://www.dropbox.com/s/argxzb1fnarzie4/Screenshot_2014-11-05-02-43-06.png?dl=0 我的問題是根據我的代碼他們需要相同的位置,但爲什麼不呢?我怎樣才能在藍色指針的相同位置添加標記。 Thnanks提前
會發生什麼OnLocationChanged(位置定位)//getMap().addMarker(markerOptions)爲什麼這個評論; – 2014-11-04 21:32:17