我想通过HERE地图上的不同按钮点击添加多个标记,但是,我添加的第二个标记没有出现,并且多次点击屏幕会导致应用崩溃。
以下是我的点击事件声明代码示例:
private MapGesture.OnGestureListener setDestinationListener2 = new MapGesture.OnGestureListener.OnGestureListenerAdapter() {
@Override
public boolean onTapEvent(PointF pointF) {
GeoCoordinate endpoint = map.pixelToGeo(pointF);
if (destinationMarker != null)
{
map.removeMapObject(destinationMarker);
}
else
{
destinationMarker = new MapMarker(endpoint,image);
destinationMarker.setAnchorPoint(new PointF(image.getWidth()/2, image.getHeight()));
map.addMapObject(destinationMarker);
destination = destinationMarker.getCoordinate();
map.addMapObject(destinationMarker);
}
return super.onTapEvent(pointF);
}
}; private MapGesture.OnGestureListener addCrowdListener2 = new MapGesture.OnGestureListener.OnGestureListenerAdapter() {
@Override
public boolean onTapEvent(PointF pointF) {
blockedPath = map.pixelToGeo(pointF);
blockedRoad = RoadElement.getRoadElement(blockedPath, "eng" );
blocked = new MapMarker(blockedPath, image2 );
blocked.setAnchorPoint(new PointF(image.getWidth()/2, image.getHeight()));
map.addMapObject(blocked);
return super.onTapEvent(pointF);
}
};
按钮的功能,我声明了:
private void initAddDestinationButton() {
m_setDetinationButton = (Button) findViewById(R.id.setDestinationButton);
m_setDetinationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/*
* Clear map if previous results are still on map,otherwise proceed to creating
* route
*/
if (map != null && m_mapRoute != null) {
map.removeMapObject(m_mapRoute);
m_mapRoute = null;
} else
{
if (destinationMarker == null) {
image = new Image();
try {
image.setImageResource(R.drawable.letterx);
} catch (final IOException e) {
e.printStackTrace();
}
}
mapFragment.getMapGesture().removeOnGestureListener(addCrowdListener2);
mapFragment.getMapGesture().addOnGestureListener(setDestinationListener2, 1, true);
}
}
});
}
private void initAddCrowdButton() {
m_addCrowdButton = (Button) findViewById(R.id.addCrowdButton);
m_addCrowdButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/*
* Clear map if previous results are still on map,otherwise proceed to creating
* route
*/
if (map != null && m_mapRoute != null) {
map.removeMapObject(m_mapRoute);
m_mapRoute = null;
} else
{
if (blocked == null) {
image2 = new Image();
try {
image.setImageResource(R.drawable.marker);
} catch (final IOException e) {
e.printStackTrace();
}
}
mapFragment.getMapGesture().removeOnGestureListener(setDestinationListener2);
mapFragment.getMapGesture().addOnGestureListener(addCrowdListener2, 10, true);
}
}
});
}
答案 0 :(得分:0)
问题是在代码中重复使用mapmarker变量。
您可以在每次点击时执行此操作:
destinationMarker = new MapMarker(endpoint,image);
[...]
map.addMapObject(destinationMarker);
您将destinationMarker添加到地图中,并将destinationMarker的引用保留为您身边的成员。
在第二次点击时,您重复使用此引用,为destinationMarker创建一个新的mapmarker实例,并尝试将其添加到地图中。对于地图,它再次成为相同的destinationMarker。不会添加第二个标记,因为即使您更改了实例,也已添加此引用。
您希望在代码中保留对您的标记的引用(例如,稍后再从地图中删除它们),因此需要对所有地图标记实例使用List:
MapMarker m = new MapMarker(endpoint,image);
markerList.add(m);
map.addMapObject(m);
顺便说一下:除了这个问题之外,我还没有必要在地图上添加两次相同的mapobject:
map.addMapObject(destinationMarker);
destination = destinationMarker.getCoordinate();
map.addMapObject(destinationMarker);