简体   繁体   中英

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. I parsed it into an ArrayList.

Through an AsyncTask I plotted them on a map. Currently, that's 4 shops. I Overrided the marker onClick to show the data the way I wanted. 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. All the data is coming from the last shop in my JSON. Here is my onPostExecute() code:

@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. 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. (.title and .snippet) and it worked! It's showing the correct info! Is there a way to get a custom onClick then? Looks like setOnMarkerListener is causing the problem.

It's because you're calling setOnMarkerClickListener() on the SAME gMap object multiple times. 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. Of course, then you have to identify your markers in its onMarkerClick() method to decide which shop was clicked.

You can put your all markers in 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;
        }

    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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