简体   繁体   English

Google Map v2 缩放以适应所有标记和折线

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

So, I am showing a route from point A to point B on a Google Map on my Android App.因此,我在我的 Android 应用程序上的 Google 地图上显示了从 A 点到 B 点的路线。 This is all well.这一切都很好。 What I am doing now is zooming-to-fit both of the markers on the map by doing this:我现在正在做的是通过执行以下操作来缩放以适应地图上的两个标记:

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);

The problem is that this doesn't include poly lines, so if the route goes outside of the screen, it cuts off the poly line, like this:问题是这不包括折线,所以如果路线超出屏幕,它会切断折线,如下所示:

在此处输入图片说明

Any ideas on how I can zoom-to-fit the poly lines?关于如何缩放以适应折线的任何想法? Thanks!谢谢!

I tried this code and its works good for me and also i attached output screen here.我试过这段代码,它对我很有用,我还在这里附上了输出屏幕。

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);

Output screen :-输出屏幕:- 在此处输入图片说明

I got it.我知道了。 I just kept track of every LatLng within my Poly Line and added them to the LatLng builder我只是跟踪我的 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);
    }
}

I would say that while you're building the polyline you keep track of the extents of the line.我想说的是,当您构建折线时,您会跟踪线的范围。 Then when you zoom use those extents not the points.然后在缩放时使用这些范围而不是点。

I copied from Premkumar's for mine.我从 Premkumar 那里复制了我的。 If you've got more than 2 points and you want to make sure everything fits (maybe you've got two points in the middle, and lots of stuff circling around).如果你有超过 2 分,并且你想确保一切都合适(也许你有两分在中间,还有很多东西在盘旋)。 This solution works by creating a minLatLng and a maxLatLng and then using the newLatLngBounds solution from Premkumar's answer:该解决方案的工作原理是创建一个 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))

Obviously, if you need this in real time, it would be far more efficient to keep a tally of your mins and maxes every time you get a new location.显然,如果您需要实时执行此操作,则每次到达新位置时记录您的最小值和最大值会更有效率。

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

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