如何保存Map v2的标记信息,以便在标记点击时进行检索 [英] How to save information of markers of Map v2 so that it can be retrieve on marker click

查看:109
本文介绍了如何保存Map v2的标记信息,以便在标记点击时进行检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作具有地图v2的应用程序,并且我正在成功显示多个引脚。我跟着一个教程,它只是在地图v2上显示引脚/标记。

通过服务,我可以获得标记的信息,显示地图上的酒店位置。该服务向我发送的信息包括酒店名称,位置,食物,服务,评级以及经纬度。所以,让我解释我迄今为止所做的事情。

什么我正在做的是在我的asynctask中获取每个酒店的规范,并且在这里我调用下面的方法来添加标记:

  addMarker(Double .parseDouble(latitude),Double.parseDouble(longitude)); 

addMarker函数如下:

public void addMarker(Double Lat,Double Long){

  if(null!= googleMap){
MyMapFragment.this .googleMap.addMarker(new MarkerOptions()
.position(new LatLng(Lat,Long))
.title(You)
.draggable(true)


);
}
}

这很好,因为它能够成功显示标记,我知道这不是正确的做法,因为我希望每个标记都能显示自己的其他特定信息,所以为此我知道必须保存标记信息,但我不知道如何去做。



**我想要什么**



我想要的是保存每个pin / marker记录,并点击的标记在新的窗口或活动中打开它的信息我怎样才能做到这一点,请分享我一个代码来获得想法或请将我重定向到一个简单的代码我知道关于github上的演示项目但我不能理解它,因为它对我来说很复杂。请帮助解决方案不幸的是,您不能将元数据添加到您的标记中。您需要做的是 - 在将标记添加到地图(< Marker,Model> )形式的某些数据结构中时保存标记,以便检索模型基于标记。

假设您有一个定义Hotel信息的 Model 类。在这种情况下,您可以使用一个对象地图来跟踪您的标记:

  Map< Marker,Model> markerMap = new HashMap< Marker,Model>(); 

现在当您添加标记时,只需将它们放入此映射中即可:

  public void addMarker(Model hotelModel){

if(null!= googleMap){
Marker hotelMarker = MyMapFragment.this .googleMap.addMarker(new MarkerOptions()
.position(new LatLng(hotelModel.getLat(),hotelModel.getLon()))
.title(hotelModel.getTitle())
) ;

markerMap.add(hotelMarker,hotelModel);
}
}

现在当点击标记时,您可以获取酒店模型基于marker对象:

  @Override 
public boolean onMarkerClick(Marker marker){
Model modelModel = markerMap.get(marker);
if(hotelModel!= null){
Log.d(test,Hotel+ hotelModel.getTitle()+is clicked);
}
}


I am making an app which has map v2 and I am displaying multiple pins successfully. I have followed an tutorial which was just displaying the pin/markers on map v2.

Through service I am getting information of Marker showing the Hotels location on map . The service is sending me the information which include Hotel Name, Location , Food , Services , rating and Latitude and longitude.so let me explain what I have done so far

what I am doing is getting each hotel specification in my asynctask and in this I am calling the following method to add marker

addMarker(Double.parseDouble(latitude), Double.parseDouble(longitude));

addMarker Function is as follows :

public void addMarker(Double Lat, Double Long) {

 if (null != googleMap) {
           MyMapFragment.this.googleMap.addMarker(new MarkerOptions()
                            .position(new LatLng(Lat, Long))
                            .title("You")
                            .draggable(true)


            );
}
}

this is good as it is showing the markers successfully but I know this is not the right thing to do as I wanted each marker to show its own other specific information , so for doing this I know I have to save the information of markers but I don't know how to do it

**What I want **

What I want is to save each pin/marker record against it and on click of that marker open its information in a new window or activity so How can I achieve this , Please share me an code to get the idea or please redirect me to an easy code I do know about the demo project on github But I am not able to understand it as it is complex for me . Please help

解决方案

Unfortunately you cannot add metadata to your markers. What you need to do instead - is to save markers as you add them to some data structure in form of map (<Marker, Model>), so you can retrieve model based on marker.

Let's assume you have a Model class which defines Hotel information. In this case, you can have a map of objects to keep track of your markers:

Map<Marker, Model> markerMap = new HashMap<Marker, Model>();

Now as you add markers, just put them in this map:

public void addMarker(Model hotelModel) {

    if (null != googleMap) {
        Marker hotelMarker = MyMapFragment.this.googleMap.addMarker(new MarkerOptions()
                        .position(new LatLng(hotelModel.getLat(), hotelModel.getLon()))
                        .title(hotelModel.getTitle())
        );

        markerMap.add(hotelMarker, hotelModel);
    }
}

Now when marker is clicked, you can get hotel model based on the marker object:

@Override
public boolean onMarkerClick (Marker marker) {
    Model hotelModel = markerMap.get(marker);
    if(hotelModel != null) {
        Log.d("test", "Hotel "+hotelModel.getTitle()+" is clicked"); 
    }
}

这篇关于如何保存Map v2的标记信息,以便在标记点击时进行检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆