java - 如何在 Android Studio 中添加 Snap to Roads Google Map

标签 java google-maps android-studio snapshot

你好我想问一下当我有google map API给出的路线时如何添加Snap to Road。 我有一堆从 A 点线到 B 点线的 Lat lang 并画了一条像折线一样的线,但我想要的是如何从给定路线将此代码捕捉添加到 Road? 这是从 A 点到 B 点添加更多点的方法, 这是我想补充的, https://developers.google.com/maps/documentation/roads/snap

我的项目是这样的 enter image description here

最佳答案

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

    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.添加到 map :

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

关于java - 如何在 Android Studio 中添加 Snap to Roads Google Map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35472005/

相关文章:

java - 当 Cassandra 中特定数据中心的所有节点都关闭时执行读写操作

java - 如何在结果中捕获定界符作为标记

java - 根据配置文件中的变量使用不同的bean实现

google-maps - AngularDart View完成加载

android-studio - 如何在 Android Studio 中终止数据库检查器

java - Scala - Java 互操作 : can Scala emit enums in bytecode for Java to consume?

javascript - Oracle Apex 中的反向地理编码(来自可拖动标记)

java - 在我的应用程序中出现错误 "java.lang.RuntimeException: Unable to start activity"

android - 是否可以在没有应用程序源代码的情况下查看 log cat 的输出?

android - 如何在 Android Studio 中构建 AOSP 项目