在我的应用程序中,我使用if / onMapLongClick语句,如果存在标记,它将更新标记的位置,如果不存在,则创建标记。我想在Google中使用SearchView,所以我使用了一个教程,我在Stack溢出上阅读文章。它利用的搜索结果创建一个标记。好的!然而,它正在使用自己的addMarker。所以,我希望有人能让我走到正确的方向,这样我就能把Google生成的结果与我添加标记的方法联系起来。(教人钓鱼,不要给人鱼。)
My方法:
@Override
public void onMapLongClick(LatLng point) {
if(marker != null){ //if marker exists (not null or whatever)
marker.setPosition(point);
}
else{
marker = map.addMarker(new MarkerOptions()
.position(point)
.title("Your Destination")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))
.draggable(true));
}
if(marker!=null){
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(point)
.zoom(10)
.build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}Google-Places-API结果的教程方法:
private void showLocations(Cursor c){
MarkerOptions markerOptions = null;
LatLng position = null;
map.clear();
while(c.moveToNext()){
markerOptions = new MarkerOptions();
position = new LatLng(Double.parseDouble(c.getString(1)),Double.parseDouble(c.getString(2)));
markerOptions.position(position);
markerOptions.title(c.getString(0));
map.addMarker(markerOptions);
}
if(position!=null){
CameraUpdate cameraPosition = CameraUpdateFactory.newLatLng(position);
map.animateCamera(cameraPosition);
}
}发布于 2014-03-14 19:03:44
可能有更好的方法来完成它,但是在稍微修改了一下之后,我能够将我的方法的代码循环到教程方法中,它看起来如下所示:
private void showLocations(Cursor c){
LatLng position = null;
while(c.moveToNext()){
position = new LatLng(Double.parseDouble(c.getString(1)),Double.parseDouble(c.getString(2)));
if(marker != null){ //if marker exists (not null)
marker.setPosition(position);
}
else{
marker = map.addMarker(new MarkerOptions()
.position(position)
.title("Your Destination")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))
.draggable(true));
}
if(position!=null){
CameraUpdate cameraPosition = CameraUpdateFactory.newLatLng(position);
map.animateCamera(cameraPosition);
}
}
}现在,不管用户是长时间单击地图上的某个位置,还是从给定的搜索结果中选择一个位置,地图上只会出现一个用户集标记。哇喔!
https://stackoverflow.com/questions/22308219
复制相似问题