首页 > 解决方案 > 想要在现有的谷歌地图上添加区域信息并与用户的触摸交互

问题描述

我有一个移动应用程序,它使用 Android GoogleMap Api 在 GoogleMap 上显示路线,然后沿路线添加标记。我让用户使用 start/end/viapoints 定义路线。我真的很想让我的应用程序让用户使用 GoogleMaps 应用程序用户界面定义路线,然后让我的应用程序获取生成的折线,以便我可以显示路线并沿路线添加唯一标记。本质上,我试图不重新创建 GoogleMap 应用程序用户界面让用户使用所有可能的路线选项定义路线的令人敬畏的方式。是否有 api 来显示 GoogleMap 用户界面以定义路线并允许我的应用程序获取生成的折线数据?

我是 Android 开发的新手。我没有清楚地描述我的要求。我创建的应用程序当前有效。它提示用户输入路线开始/结束/通过点。然后,它使用地理编码器将路线起点/终点/途经点(街道、城市、州、邮政编码)转换为 GPS 位置。然后它发出一个 HTTP 请求(“ https://maps.googleapis.com/maps/api/ ...”),然后反序列化 JSON 响应以获取路由 OverviewPolyline.Points 并保存它们。它将使用此信息通过 Google 地图控件显示路线。我添加标记以沿地图控件显示的路线显示其他信息。我只使用路线信息,因此我可以使用沿路线的标记添加我的扩展信息。如果有办法我可以使用内置的地图应用程序(即“http://maps.google.com/?daddr=San+Francisco,+CA&saddr=Mountain+View ") 让用户定义路线,然后我从中获取路线折线信息。

更好的是设计应用程序以添加将这些标记添加到现有谷歌地图的能力。我只想在 Google 地图提供的内容之上提供截然不同的信息。我想让用户通过触摸地图来指示在哪里显示这个及时的区域信息。我想让用户也可以打开或关闭此信息(标记/广告牌)的显示,以显示今天的信息、明天的信息、接下来的几天等。此信息在时间较长的路线上最有用旅行是一个多小时,也许是几天。那么,哪些 API 可用于在 Google 地图之上添加功能,而无需提供与 Google 地图已有功能相同的功能?

标签: c#androidgoogle-maps

解决方案


以下是一些可能对您有所帮助的代码片段。我还没有完成记录用户单击的地图上某个点的位置的部分,但这应该相对容易。这里主要说明保存和绘制折线信息。

    // Some imports
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
...
// Declare the pathDynamic
private PolylineOptions pathDynamic;
...
pathDynamic = new PolylineOptions().width(15).color(Color.RED);
...
// Whenever the user clicks the map to add a point
LatLng theLatLng = new LatLng(location.getLatitude(), location.getLongitude());         
pathDynamic.add(theLatLng);
...
// Add the polyline to the map
if (mMap != null) {
    mMap.addPolyline(pathDynamic);
}
...
//If you want to save the points to an encoded string to store in a database
String encoded_path = encode(pathDynamic.getPoints());
...
//If you want to put an encoded_path into a pathDynamic
pathDynamic = new PolylineOptions().width(15).color(Color.RED);
pathDynamic.addAll(decode(encoded_path));
...
    /**
 * Decodes an encoded path string into a sequence of LatLngs.
 */
public static List<LatLng> decode(final String encodedPath) {
    int len = encodedPath.length();

    // For speed we preallocate to an upper bound on the final length, then
    // truncate the array before returning.
    final List<LatLng> path = new ArrayList<LatLng>();
    int index = 0;
    int lat = 0;
    int lng = 0;

    while (index < len) {
        int result = 1;
        int shift = 0;
        int b;
        do {
            b = encodedPath.charAt(index++) - 63 - 1;
            result += b << shift;
            shift += 5;
        } while (b >= 0x1f);
        lat += (result & 1) != 0 ? ~(result >> 1) : (result >> 1);

        result = 1;
        shift = 0;
        do {
            b = encodedPath.charAt(index++) - 63 - 1;
            result += b << shift;
            shift += 5;
        } while (b >= 0x1f);
        lng += (result & 1) != 0 ? ~(result >> 1) : (result >> 1);

        path.add(new LatLng(lat * 1e-5, lng * 1e-5));
    }

    return path;
}

/**
 * Encodes a sequence of LatLngs into an encoded path string.
 */
public static String encode(final List<LatLng> path) {
    long lastLat = 0;
    long lastLng = 0;

    final StringBuffer result = new StringBuffer();

    for (final LatLng point : path) {
        long lat = Math.round(point.latitude * 1e5);
        long lng = Math.round(point.longitude * 1e5);

        long dLat = lat - lastLat;
        long dLng = lng - lastLng;

        encode(dLat, result);
        encode(dLng, result);

        lastLat = lat;
        lastLng = lng;
    }
    return result.toString();
}

private static void encode(long v, StringBuffer result) {
    v = v < 0 ? ~(v << 1) : v << 1;
    while (v >= 0x20) {
        result.append(Character.toChars((int) ((0x20 | (v & 0x1f)) + 63)));
        v >>= 5;
    }
    result.append(Character.toChars((int) (v + 63)));
}

推荐阅读