简体   繁体   English

将JSON数据解析为Map标记,所有标记显示相同的信息,我该如何解决这个问题?

[英]Parsing JSON data to Map markers, all markers showing the same info, how can I fix this?

need a bit of help with this one. 需要一点帮助。

I have JSON of a some shops and the shops information, like their names, ids and locations. 我有一些商店的JSON和商店信息,比如他们的名字,ID和位置。 I parsed it into an ArrayList. 我将其解析为ArrayList。

Through an AsyncTask I plotted them on a map. 通过AsyncTask我将它们绘制在地图上。 Currently, that's 4 shops. 目前,这是4家商店。 I Overrided the marker onClick to show the data the way I wanted. 我覆盖了标记onClick以我想要的方式显示数据。 The problem I'm having is that although the markers are in the correct places based on the longitude and latitude, the other information is not. 我遇到的问题是,尽管基于经度和纬度标记位于正确的位置,但其他信息则不然。

When I click on a marker, the same shop name is listed on all of them. 当我点击标记时,所有这些都列出了相同的商店名称。 It's only showing the last shop in my JSON. 它只显示我的JSON中的最后一个商店。 All the data is coming from the last shop in my JSON. 所有数据都来自我JSON中的最后一个商店。 Here is my onPostExecute() code: 这是我的onPostExecute()代码:

@Override
protected void onPostExecute(ArrayList<ShopArray> shopArray) {
    super.onPostExecute(shopArray);

    for (ShopArray shopArrayOb : shopArray) {
        double lat = shopArrayOb.geo_lat;
        double lng = shopArrayOb.geo_lng;

        final String barName = shopArrayOb.bar_name;

        LatLng shopLocations = new LatLng(lat, lng);

        MarkerOptions marker = new MarkerOptions()
            .position(shopLocations)
            gMap.addMarker(marker);

        gMap.setOnMarkerClickListener(new OnMarkerClickListener() {
                @Override
            public boolean onMarkerClick(Marker marker) {

                testText.setText(barName);

                return false;
            }

        });
    }

I don't really know how to fix this. 我真的不知道如何解决这个问题。 I just want it to show the correct names for the markers, amongst other data. 我只是想让它显示标记的正确名称,以及其他数据。 It keeps showing data from the last shop in my JSON and nothing else. 它不断显示我JSON中最后一个商店的数据。 If you need to see more of my code then let me know and I will update my post. 如果您需要查看更多我的代码,请告诉我,我会更新我的帖子。

I would really appreciate some help on this, thanks in advance! 我非常感谢你的帮助,提前谢谢!

EDIT: I've just tested with the default marker onClick. 编辑:我刚刚使用默认标记onClick进行了测试。 (.title and .snippet) and it worked! (.title和.snippet)并且它有效! It's showing the correct info! 它显示正确的信息! Is there a way to get a custom onClick then? 有没有办法获得自定义onClick呢? Looks like setOnMarkerListener is causing the problem. 看起来像setOnMarkerListener导致了这个问题。

It's because you're calling setOnMarkerClickListener() on the SAME gMap object multiple times. 这是因为你多次在SAME gMap对象上调用setOnMarkerClickListener() In this way it's not strange that the last call will take effect, and the last shop's info will be used. 通过这种方式,最后一次通话生效并不奇怪,最后一个商店的信息将被使用。 You should set OnMarkerClickListener only once. 您应该只设置一次OnMarkerClickListener Of course, then you have to identify your markers in its onMarkerClick() method to decide which shop was clicked. 当然,您必须在其onMarkerClick()方法中识别您的标记,以确定点击了哪个商店。

You can put your all markers in HashMap: 您可以将所有标记放在HashMap中:

//somewhere befor async task    
HashMap<LatLng, String> map = new HashMap<LatLng, String>();

Then add markers to it: 然后添加标记:

//in your post execute
map.put(shopLocations, barName);

Then in your listener: 然后在你的听众中:

gMap.setOnMarkerClickListener(new OnMarkerClickListener() {
            @Override
        public boolean onMarkerClick(Marker marker) {

            testText.setText(map.get(marker.getPosition()));

            return false;
        }

    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM