在 HERE java sdk 中添加多个标记
Adding multiple markers in HERE java sdk
我想通过在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);
}
}
});
}
问题是在您的代码中重复使用了 mapmarker 变量。
您在每次点击时执行此操作:
destinationMarker = new MapMarker(endpoint,image);
[...]
map.addMapObject(destinationMarker);
您将 destinationMarker 添加到地图并将 destinationMarker 的引用保留为您这边的成员。
在第二次点击时,您重复使用此引用,为 destinationMarker 创建一个新的地图标记实例,并尝试将其添加到地图中。对于地图,它又是相同的 destinationMarker。第二个标记不会被添加,因为这个引用已经被添加,即使你的实例改变了。
你当然想在你的代码中保留对你的标记的引用(例如稍后再次从地图中删除它们),所以建议对你的所有地图标记实例使用一个列表:
MapMarker m = new MapMarker(endpoint,image);
markerList.add(m);
map.addMapObject(m);
顺便说一句:除了这个问题,我还没有必要向地图添加两次相同的地图对象:
map.addMapObject(destinationMarker);
destination = destinationMarker.getCoordinate();
map.addMapObject(destinationMarker);
我想通过在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);
}
}
});
}
问题是在您的代码中重复使用了 mapmarker 变量。
您在每次点击时执行此操作:
destinationMarker = new MapMarker(endpoint,image);
[...]
map.addMapObject(destinationMarker);
您将 destinationMarker 添加到地图并将 destinationMarker 的引用保留为您这边的成员。
在第二次点击时,您重复使用此引用,为 destinationMarker 创建一个新的地图标记实例,并尝试将其添加到地图中。对于地图,它又是相同的 destinationMarker。第二个标记不会被添加,因为这个引用已经被添加,即使你的实例改变了。
你当然想在你的代码中保留对你的标记的引用(例如稍后再次从地图中删除它们),所以建议对你的所有地图标记实例使用一个列表:
MapMarker m = new MapMarker(endpoint,image);
markerList.add(m);
map.addMapObject(m);
顺便说一句:除了这个问题,我还没有必要向地图添加两次相同的地图对象:
map.addMapObject(destinationMarker);
destination = destinationMarker.getCoordinate();
map.addMapObject(destinationMarker);