【发布时间】:2015-04-23 16:29:48
【问题描述】:
我有一个关于 Google Maps API v2 的问题。
我的英语很差,这是我和谷歌的翻译一起做的,希望你能理解我,我的问题是:是否可以实时更正谷歌地图API v2中的折线点? ,我需要在带有可拖动标记的谷歌地图 v2 混合中围绕给定的周长,但是当我移动标记时,折线没有更正到它的新形状,有人能告诉我怎么做吗?,这是我的一个片段代码
public class MapsActivity extends FragmentActivity implements GoogleMap.OnMapClickListener, GoogleMap.OnMapLongClickListener, GoogleMap.OnMarkerDragListener {
//utilidad
int cont;
String description;
ArrayList<LatLng> PolyLinePoints = new ArrayList<LatLng>();
//elementos del mapa
final int RQS_GooglePlayServices = 1;
Location myLocation;
boolean markerClicked;
PolygonOptions polygonOptions;
Polygon polygon;
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
/**
* Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
* installed) and the map has not already been instantiated.. This will ensure that we only ever
* call {@link #setUpMap()} once when {@link #mMap} is not null.
* <p/>
* If it isn't installed {@link SupportMapFragment} (and
* {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to
* install/update the Google Play services APK on their device.
* <p/>
* A user can return to this FragmentActivity after following the prompt and correctly
* installing/updating/enabling the Google Play services. Since the FragmentActivity may not
* have been completely destroyed during this process (it is likely that it would only be
* stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this
* method in {@link #onResume()} to guarantee that it will be called.
*/
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();
mMap.setMyLocationEnabled(true);
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
mMap.setOnMapClickListener(this);
mMap.setOnMapLongClickListener(this);
mMap.setOnMarkerDragListener(this);
mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
//marker.remove();
}
});
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
/**
* This is where we can add markers or lines, add listeners or move the camera. In this case, we
* just add a marker near Africa.
* <p/>
* This should only be called once and when we are sure that {@link #mMap} is not null.
*/
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Mi marcador").snippet("un marcador simple").draggable(false));
}
@Override
public void onMapClick(LatLng point) {
mMap.animateCamera(CameraUpdateFactory.newLatLng(point));
markerClicked = false;
}
@Override
public void onMapLongClick(LatLng point) {
//mMap.addMarker(new MarkerOptions().position(point).title(String.valueOf(cont)).snippet("Clic aqui para eliminar").draggable(true));
// Instantiating the class MarkerOptions to plot marker on the map
MarkerOptions markerOptions = new MarkerOptions();
// Setting latitude and longitude of the marker position
markerOptions.position(point);
// Setting titile of the infowindow of the marker
markerOptions.title("Position");
// Setting the content of the infowindow of the marker
markerOptions.snippet(point.latitude + ", "+ point.longitude);
//can move Marker on the all map
markerOptions.draggable(true);
// Instantiating the class PolylineOptions to plot polyline in the map
PolylineOptions polylineOptions = new PolylineOptions();
// Setting the color of the polyline
polylineOptions.color(Color.RED);
// Setting the width of the polyline
polylineOptions.width(3);
// Adding the taped point to the ArrayList
PolyLinePoints.add(point);
// Setting points of polyline
polylineOptions.addAll(PolyLinePoints);
// Adding the polyline to the map
mMap.addPolyline(polylineOptions);
// Adding the marker to the map
mMap.addMarker(markerOptions);
markerClicked = false;
}
@Override
public void onMarkerDragStart(Marker marker) {
}
@Override
public void onMarkerDrag(Marker marker) {
}
@Override
public void onMarkerDragEnd(Marker marker) {
//Point[] mark = mMap.getProjection().toScreenLocation(marker.getPosition());
Toast.makeText(this, "LatLgn: " + mark.toString(), Toast.LENGTH_SHORT).show();
}
}
这是我所做的图片,链接在这里(我无法在帖子中添加图片):
这是我尝试更新标记位置时发生的情况的图片,请点击此处链接(我无法在帖子中添加图片):
有人可以告诉我我需要什么吗?或者我可以在哪里找到我的解决方案?
非常感谢您的关注,希望对您有所帮助
【问题讨论】:
-
从提供的图片来看,您似乎只更新了标记的纬度。也许您应该使用更新的 latlng 数据再次调用绘制折线的函数。
-
如何更新积分?
-
不要这样做,不要回忆多次绘制折线的方法,否则会在另一条上绘制多条折线。
标签: android google-maps google-maps-api-2 polyline