如何在 Android Studio 中向道路谷歌地图添加对齐 [英] How to Add Snap to Roads Google Map in Android Studio

查看:35
本文介绍了如何在 Android Studio 中向道路谷歌地图添加对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我想问一下,当我有谷歌地图API给出的路线时,如何添加Snap to Road.我有一堆从 A 点线到 B 点线的 Lat lang 并绘制了一条类似折线的线,但我想要的是如何将此代码从给定路线添加到 Road?这是如何从 A 点到 B 点添加更多点,这是我想补充的,

解决方案

  1. 通过 Json 和 Gson 获取overview_polyline(应该使用https://maps.googleapis.com/maps/api/directions/json?origin=...&destination=place_id:...&mode=DRIVING&key=...)
  2. 按函数解码为List

    public ListdecodePoly(字符串编码){//编码是overview_polyline.points;列表<LatLng>poly = new ArrayList();int index = 0, len = encoding.length();int lat = 0, lng = 0;而(索引 < len){int b, shift = 0, 结果 = 0;做 {b = encoding.charAt(index++) - 63;结果 |= (b & 0x1f) <<转移;移位 += 5;} while (b >= 0x20);int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));纬度 += 纬度;移位 = 0;结果 = 0;做 {b = encoding.charAt(index++) - 63;结果 |= (b & 0x1f) <<转移;移位 += 5;} while (b >= 0x20);int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));lng += dlng;LatLng p = new LatLng((((double) lat/1E5)),(((双) lng/1E5)));poly.add(p);}返回聚;}

    3.添加到地图:

    PolylineOptions polylineOptions= new PolylineOptions();polylineOptions.addAll(decodePoly(overview_polyline.points));mGoogleMap.addPolyline(polylineOptions.width(5).color(Color.BLUE).geodesic(false));

Hello I would like to ask how to add Snap to Road when I have the route given by google map API. I have a bunch of Lat lang from point A line to point B line and draw a lines like Polylines, but what i want is how to add this code snap to Road from given route? this is how to add more points from the Point A to Point B, here is what i want to add, https://developers.google.com/maps/documentation/roads/snap

my project is look like this

解决方案

  1. Get overview_polyline by Json and Gson (should use https://maps.googleapis.com/maps/api/directions/json?origin=...&destination=place_id:...&mode=DRIVING&key=...)
  2. Decode it to List by function

    public List<LatLng> decodePoly(String encoded) {
    // encoded is overview_polyline.points; 
    List<LatLng> poly = new ArrayList<LatLng>();
    int index = 0, len = encoded.length();
    int lat = 0, lng = 0;
    while (index < len) {
        int b, shift = 0, result = 0;
        do {
            b = encoded.charAt(index++) - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
        lat += dlat;
    
        shift = 0;
        result = 0;
        do {
            b = encoded.charAt(index++) - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
        lng += dlng;
    
        LatLng p = new LatLng((((double) lat / 1E5)),
                (((double) lng / 1E5)));
        poly.add(p);
    }
    return poly;
    }
    

    3.Add to map:

    PolylineOptions polylineOptions= new PolylineOptions();
    polylineOptions.addAll(decodePoly(overview_polyline.points));
    mGoogleMap.addPolyline(polylineOptions.width(5).color(Color.BLUE).geodesic(false));
    

这篇关于如何在 Android Studio 中向道路谷歌地图添加对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆