我使用google maps api v2制作自定义Android地图。我想在用户当前位置放置一个标记。我的代码是:
// 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);
在地图上给我一个蓝色指针在用户当前位置我手动在用户当前位置添加标记(如上面的代码所示)。我的标记和蓝色指针必须是相同的位置,因为它们描述相同的位置(相同的纬度和经度)但它们不是。运行我的代码后输出看起来像这个图像 https://www.dropbox.com/s/argxzb1fnarzie4/Screenshot_2014-11-05-02-43-06.png?dl=0 我的问题是根据我的代码他们需要相同的位置,但为什么不呢?如何在蓝色指针的相同位置添加标记。 事先提前了
答案 0 :(得分:0)
我要尝试的第一件事就是在onLocationChanged()
中移动下面的代码行 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);
答案 1 :(得分:0)
我认为是因为你的currentLocation是最后一个已知的位置。看看如果将currentLocation设置为
会发生什么Currentloc = getMap().getMyLocation()