如何从用户的位置显示最近的标记?

时间:2018-04-26 15:11:44

标签: java android

我有一个方法“onPostExecute()”,显示从JSON解析的Markers,它运行正常。现在我想从我当前的位置显示最近的Marker。我该怎么办?

以下是代码。

public void onPostExecute(String json) {

        try {
            // De-serialize the JSON string into an array of branch objects
            JSONArray jsonArray = new JSONArray(json);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObj = jsonArray.getJSONObject(i);

                LatLng latLng = new LatLng(jsonObj.getDouble("latitude"),
                        jsonObj.getDouble("longitude"));

                // Create a marker for each branch in the JSON data.
                map.addMarker(new MarkerOptions()
                        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))
                        .title(jsonObj.getString("name"))
                        // .snippet(Integer.toString(jsonObj.getInt("snippet")))
                        .snippet(jsonObj.getString("snippet"))
                        .position(latLng));
            }
        } catch (JSONException e) {
            Log.e(LOG_TAG, "Error processing JSON", e);
        }

    }

2 个答案:

答案 0 :(得分:0)

您应该阅读本指南Android Location Strategies。您需要跟踪用户位置,当您获得该位置时,您可以将每个JSON标记与之进行比较。尝试这样的事情:

public void onPostExecute(String json) {
   Location closestMark;

    try {
        // De-serialize the JSON string into an array of branch objects
        JSONArray jsonArray = new JSONArray(json);
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObj = jsonArray.getJSONObject(i);

            LatLng latLng = new LatLng(jsonObj.getDouble("latitude"),
                    jsonObj.getDouble("longitude"));

            Location currentMark = new Location("");
            currentMark.setLatitude(latLng.latitude);
            currentMark.setLongitude(latLng.longitude);

            if (closestMark == null || (userLocation.distanceTo(currentMark) 
                < userLocation.distanceTo(closestMark))
             {
               closestMark = currentMark;
             } 


            // Create a marker for each branch in the JSON data.
            map.addMarker(new MarkerOptions()
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))
                    .title(jsonObj.getString("name"))
                    // .snippet(Integer.toString(jsonObj.getInt("snippet")))
                    .snippet(jsonObj.getString("snippet"))
                    .position(latLng));
        }
    } catch (JSONException e) {
        Log.e(LOG_TAG, "Error processing JSON", e);
    }

}

希望这有帮助。

答案 1 :(得分:0)

以下是我解决问题的方法。它也可能对其他人有帮助。

public void onPostExecute(String json) {

            try {
                // De-serialize the JSON string into an array of branch objects
                JSONArray jsonArray = new JSONArray(json);
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObj = jsonArray.getJSONObject(i);

                    LatLng latLng = new LatLng(jsonObj.getDouble("latitude"), jsonObj.getDouble("longitude"));

                    float[] distance = new float[1];
                    LatLng currentLatLng = mCurrLocationMarker.getPosition();

                    //move CameraPosition on user Location
                    if (i == 0) {
                        CameraPosition cameraPosition = new CameraPosition.Builder()
                                .target(currentLatLng).zoom(13).build();

                        map.animateCamera(CameraUpdateFactory
                                .newCameraPosition(cameraPosition));
                    }

                    // Create a marker for each branch in the JSON data.
                    Marker currentMarker = map.addMarker(new MarkerOptions()
                            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))
                            .title(jsonObj.getString("name"))
                            .snippet(jsonObj.getString("snippet"))
                            .position(latLng)
                            .visible(false)
                    );

                    Location.distanceBetween(currentLatLng.latitude, currentLatLng.longitude, latLng.latitude, latLng.longitude, distance);

                    if (i == 0) {
                        minDist = distance[0];
                    } else if (minDist > distance[0]) {
                        minDist = distance[0];
                        mClosestMarker = currentMarker;
                    }
                }
                Toast.makeText(NearestBranch.this, "Nearest Branch Distance: "+ mClosestMarker.getTitle() + " " + minDist, Toast.LENGTH_LONG).show();
                map.addMarker(new MarkerOptions()
                        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))
                        .title(mClosestMarker.getTitle())
                        .snippet(mClosestMarker.getSnippet())
                        .position(mClosestMarker.getPosition())
                );

            }
            catch (JSONException e) {
                Log.e(LOG_TAG, "Error processing JSON", e);
            }
    }