简体   繁体   中英

I am using google map nearby places API in android. My code only shows the near by places

But i can not select each of them separately.
How can I select each of them separately by touching their marker and get their details. A in this function below a json array is used of 0-19(20) elements.

I dont know how to use them to select each marker separately.

private void parseLocationResult(final JSONObject result) {


    String id, place_id, placeName = null, reference, icon, vicinity = null;
    JSONArray rev;
    double latitude;
    double longitude;

    try {
        final JSONArray jsonArray = result.getJSONArray(RESULTS);//JavaScript Object Notation

        if (result.getString(STATUS).equalsIgnoreCase(OK)) {

            mMap.clear();

            for (int i = 0; i < jsonArray.length(); i++) {
                final JSONObject place = jsonArray.getJSONObject(i);

                id = place.getString(SUPERMARKET_ID);
                place_id = place.getString(PLACE_ID);
                if (!place.isNull(NAME)) {
                    placeName = place.getString(NAME);
                }
                if (!place.isNull(VICINITY)) {
                    vicinity = place.getString(VICINITY);
                }
                latitude = place.getJSONObject(GEOMETRY).getJSONObject(LOCATION)
                        .getDouble(LATITUDE);
                longitude = place.getJSONObject(GEOMETRY).getJSONObject(LOCATION)
                        .getDouble(LONGITUDE);
                reference = place.getString(REFERENCE);
                icon = place.getString(ICON);


                MarkerOptions markerOptions = new MarkerOptions();
                final LatLng latLng = new LatLng(latitude, longitude);
                markerOptions
                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.bluemarker))
                        .position(latLng);
                markerOptions.title(placeName + " : " + vicinity);


                mMap.addMarker(markerOptions);



            }

            Toast.makeText(getBaseContext(), jsonArray.length() + " found!",
                    Toast.LENGTH_LONG).show();
        } else if (result.getString(STATUS).equalsIgnoreCase(ZERO_RESULTS)) {
            Toast.makeText(getBaseContext(), "Not found in 5KM radius!!!",
                    Toast.LENGTH_LONG).show();
        }

    } catch (JSONException e) {

        e.printStackTrace();
        Log.e(TAG, "parseLocationResult: Error=" + e.getMessage());
    }
}

implement GoogleMap.OnMarkerClickListener and Override onMarkerClick to detect click on marker

@Override
public boolean onMarkerClick(Marker marker) {

    // on marker clicked

    if (marker.equals(this.marker)) {
         // on specific marker clicked
    }
    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