我已将GoogleMaps应用到我的应用程序中。我要做的是显示用户当前位置,然后允许用户在地图上的任何其他位置添加标记。我通过将setMyLocationEnabled设置为true来显示用户的当前位置。然后,只要您在屏幕上单击,就会添加标记。我想获得标记的新lat / lng坐标,但它不断给我用户位置的坐标。当我将setMyLocationEnabled更改为false并添加标记时,它返回正确的坐标。因此,似乎我可能必须编写自己的代码来获取用户当前位置而不是使用setMyLocationEnabled。有任何想法吗?以下是我的代码
public class BasicMapDemoActivity extends FragmentActivity implements
OnMarkerDragListener, OnMapClickListener {
private GoogleMap mMap;
private String TAG = "TEST";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.basic_demo);
setUpMapIfNeeded();
mMap.setMyLocationEnabled(true);
mMap.setOnMarkerDragListener(this);
mMap.setOnMapClickListener(this);
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the
// map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
// setUpMap();
}
}
}
@Override
public void onMarkerDrag(Marker marker) {
// TODO Auto-generated method stub
}
@Override
public void onMarkerDragEnd(Marker marker) {
// TODO Auto-generated method stub
LatLng position = marker.getPosition(); //
Toast.makeText(
BasicMapDemoActivity.this,
"Lat " + position.latitude + " " + "Long " + position.longitude,
Toast.LENGTH_LONG).show();
}
@Override
public void onMarkerDragStart(Marker marker) {
// TODO Auto-generated method stub
}
@Override
public void onMapClick(LatLng point) {
// TODO Auto-generated method stub
// Creating a marker
MarkerOptions markerOptions = new MarkerOptions();
// Setting the position for the marker
markerOptions.position(point);
// Setting the title for the marker.
// This will be displayed on taping the marker
markerOptions.title(point.latitude + " : " + point.longitude);
// Clears the previously touched position
mMap.clear();
// Animating to the touched position
mMap.animateCamera(CameraUpdateFactory.newLatLng(point));
// Placing a marker on the touched position
mMap.addMarker(markerOptions).setDraggable(true);
}
}
答案 0 :(得分:0)
我不清楚标记来自您尝试获取其位置的方法,但看起来您不会保留对要报告其位置的标记的引用。< / p>
调用此方法时:
// Placing a marker on the touched position
mMap.addMarker(markerOptions).setDraggable(true);
它返回对添加的标记的引用。我认为您需要保留它返回的引用,然后根据该引用查询位置。
所以你可以尝试这样的事情:
// Placing a marker on the touched position
Marker myMarker = mMap.addMarker(markerOptions).setDraggable(true);
LatLng position = myMarker.getPosition(); //
Toast.makeText(
BasicMapDemoActivity.this,
"Lat " + position.latitude + " " + "Long " + position.longitude,
Toast.LENGTH_LONG).show();
<强>更新强>
不确定为什么投票,也许有人可以评论出错了什么?无论如何,我会尝试添加更多信息,也许这会有所帮助。
首先,我没有看到发布的代码中触发对onMarkerDragEnd()事件的调用。我没有在文档中看到任何内容,只是通过向地图添加标记就会触发它。如果你想在onMapClick()方法的末尾添加制造商的位置,那么我上面展示的应该是这样做的。虽然这应该与传递给onMapClick()事件的点对象相同,但问题是关于添加的标记的位置,而不是点击的点,即使在这种情况下他们也是应该是一样的。