簡體   English   中英

Google Map v2 縮放以適應所有標記和折線

[英]Google Map v2 zoom-to-fit all markers AND Poly Lines

因此,我在我的 Android 應用程序上的 Google 地圖上顯示了從 A 點到 B 點的路線。 這一切都很好。 我現在正在做的是通過執行以下操作來縮放以適應地圖上的兩個標記:

LatLngBounds.Builder builder = new LatLngBounds.Builder();

for (LatLng marker : markerPoints) {
    builder.include(marker);
}

LatLngBounds bounds = builder.build();
int padding = 20; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds,
        padding);
map.moveCamera(cu);
map.animateCamera(cu);

問題是這不包括折線,所以如果路線超出屏幕,它會切斷折線,如下所示:

在此處輸入圖片說明

關於如何縮放以適應折線的任何想法? 謝謝!

我試過這段代碼,它對我很有用,我還在這里附上了輸出屏幕。

LatLng from_Latlng=new LatLng(11.9464816,79.8091988);
LatLng to_Latlong=new LatLng(12.9876327,80.1260089);

LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(from_Latlng);
builder.include(to_Latlong);
LatLngBounds bounds = builder.build();
GoogleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100), 2000, null);

輸出屏幕:- 在此處輸入圖片說明

我知道了。 我只是跟蹤我的 Poly Line 中的每個 LatLng 並將它們添加到 LatLng 構建器

private class ParserTask extends
        AsyncTask<String, Integer, List<List<HashMap<String, String>>>> {

    // Parsing the data in non-ui thread
    @Override
    protected List<List<HashMap<String, String>>> doInBackground(
            String... jsonData) {

        JSONObject jObject;
        List<List<HashMap<String, String>>> routes = null;

        try {
            jObject = new JSONObject(jsonData[0]);
            DirectionsJSONParser parser = new DirectionsJSONParser();

            // Starts parsing data
            routes = parser.parse(jObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return routes;
    }

    // Executes in UI thread, after the parsing process
    @Override
    protected void onPostExecute(List<List<HashMap<String, String>>> result) {
        ArrayList<LatLng> points = null;
        PolylineOptions lineOptions = null;
        MarkerOptions markerOptions = new MarkerOptions();
        String distance = "";
        String duration = "";

        if (result.size() < 1) {
            Toast.makeText(getBaseContext(), "No Points",
                    Toast.LENGTH_SHORT).show();
            return;
        }

        // Traversing through all the routes
        for (int i = 0; i < result.size(); i++) {
            points = new ArrayList<LatLng>();
            lineOptions = new PolylineOptions();

            // Fetching i-th route
            List<HashMap<String, String>> path = result.get(i);

            // Fetching all the points in i-th route
            for (int j = 0; j < path.size(); j++) {
                HashMap<String, String> point = path.get(j);

                if (j == 0) { // Get distance from the list
                    distance = (String) point.get("distance");
                    continue;
                } else if (j == 1) { // Get duration from the list
                    duration = (String) point.get("duration");
                    continue;
                }

                double lat = Double.parseDouble(point.get("lat"));
                double lng = Double.parseDouble(point.get("lng"));
                LatLng position = new LatLng(lat, lng);

                points.add(position);
            }

            // Adding all the points in the route to LineOptions
            lineOptions.addAll(points);
            lineOptions.width(8);
            lineOptions.color(Color.BLUE);

        }

        tvDistanceDuration.setText("Distance:" + distance + ", Duration:"
                + duration);

        // Drawing polyline in the Google Map for the i-th route
        map.addPolyline(lineOptions);

        LatLngBounds.Builder builder = new LatLngBounds.Builder();
        /*
         * for (Marker marker : markers) {
         * builder.include(marker.getPosition()); }
         */
        for (LatLng point : points) {
            builder.include(point);
        }

        LatLngBounds bounds = builder.build();
        int padding = 20; // offset from edges of the map in pixels
        CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds,
                padding);
        map.moveCamera(cu);
        map.animateCamera(cu, 2000, null);
    }
}

我想說的是,當您構建折線時,您會跟蹤線的范圍。 然后在縮放時使用這些范圍而不是點。

我從 Premkumar 那里復制了我的。 如果你有超過 2 分,並且你想確保一切都合適(也許你有兩分在中間,還有很多東西在盤旋)。 該解決方案的工作原理是創建一個 minLatLng 和一個 maxLatLng,然后使用 Premkumar 的回答中的 newLatLngBounds 解決方案:

var minLat: Double? = null
var minLng: Double? = null
var maxLat: Double? = null
var maxLng: Double? = null
allLocs!!.forEach {
    if (minLat == null) {
        minLat = it.latitude
        maxLat = it.latitude
        minLng = it.longitude
        maxLng = it.longitude
    } else {
        minLat = min(it.latitude, minLat!!)
        maxLat = max(it.latitude, maxLat!!)
        minLng = min(it.longitude, minLng!!)
        maxLng = max(it.longitude, maxLng!!)
    }
}
val builder = LatLngBounds.builder()
builder.include(LatLng(minLat!!, minLng!!))
builder.include(LatLng(maxLat!!, maxLng!!))
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 100))

顯然,如果您需要實時執行此操作,則每次到達新位置時記錄您的最小值和最大值會更有效率。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM