Java 使用谷歌地图的不兼容类型

Java 使用谷歌地图的不兼容类型,java,android,google-maps,android-studio,Java,Android,Google Maps,Android Studio,第一次制作android应用程序,并试图将谷歌方向的多段线添加到谷歌地图上 已输入: com.google.android.gms.maps.model.LatLng 我要解码多段线点。 试图从PolylineEncoding类中使用解码,但这将从以下位置导入: com.google.maps.model.LatLng 这会导致不兼容的类型,那么如何确保使用兼容的类型呢?或者用其他方法将此多段线解码为特定的LatLng类型,而不重写算法 尝试在drawPath()方法中传递结果 public v

第一次制作android应用程序,并试图将谷歌方向的多段线添加到谷歌地图上

已输入:
com.google.android.gms.maps.model.LatLng

我要解码多段线点。 试图从PolylineEncoding类中使用
解码
,但这将从以下位置导入:
com.google.maps.model.LatLng


这会导致不兼容的类型,那么如何确保使用兼容的类型呢?或者用其他方法将此多段线解码为特定的LatLng类型,而不重写算法

尝试在drawPath()方法中传递结果

public void drawPath(字符串结果){
如果(行!=null){
googleMap.clear();
}
addMarker(新的MarkerOptions().position(Dloca));
//addMarker(新的MarkerOptions().position(loc));
试一试{
//将字符串转换为json对象
最终JSONObject json=新JSONObject(结果);
JSONArray routarray=json.getJSONArray(“路由”);
JSONObject routes=routeArray.getJSONObject(0);
JSONObject概览多段线=管线
.getJSONObject(“概述”和“多段线”);
字符串编码字符串=概览多段线。获取字符串(“点”);
列表=解码多边形(编码字符串);
对于(intz=0;z>1):(结果>>1));
lat+=dlat;
移位=0;
结果=0;
做{
b=编码的.charAt(索引++)-63;
结果|=(b&0x1f)=0x20);
int-dlng=((结果&1)!=0?~(结果>>1):(结果>>1));
液化天然气+=液化天然气;
LatLng p=新LatLng(((双)lat/1E5)),
((双)液化天然气/1E5));
poly.add(p);
}
返回多边形;
}

此代码将根据您的路线来源和目的地位置在谷歌地图中返回您的路线。

您是否正在尝试从一个地方到另一个地方的路线place@SSALPHAX是的!我在想我应该用什么来做这个。现在我用PolyUtil解码了它,所以nSo我想避免重新编写这个方法,因为这有什么意义呢当它成为地图api的一部分时。我想我的问题并不清楚,但我最初的问题是理解为什么谷歌软件包使用不同的LatLng类型。最后我使用了类似的东西:
import com.google.maps.android.PolyUtil import com.google.android.gms.maps.model.LatLng…List directions=PolyUtil.decode(polylineString);PolylineOptions p=新的PolylineOptions();p.addAll(方向);googleMap.addPolyline(p);…
 public void drawPath(String result) {
    if (line != null) {
        googleMap.clear();
    }
    googleMap.addMarker(new MarkerOptions().position(Dloca));
    //googleMap.addMarker(new MarkerOptions().position(loc));
    try {
        // Tranform the string into a json object
        final JSONObject json = new JSONObject(result);
        JSONArray routeArray = json.getJSONArray("routes");
        JSONObject routes = routeArray.getJSONObject(0);
        JSONObject overviewPolylines = routes
                .getJSONObject("overview_polyline");
        String encodedString = overviewPolylines.getString("points");
        List<LatLng> list = decodePoly(encodedString);

        for (int z = 0; z < list.size() - 1; z++) {
            LatLng src = list.get(z);
            LatLng dest = list.get(z + 1);
            line = googleMap.addPolyline(new PolylineOptions()
                    .add(new LatLng(src.latitude, src.longitude),
                            new LatLng(dest.latitude, dest.longitude))
                    .width(5).color(Color.BLUE).geodesic(true));
        }

        dialog.dismiss();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

private List<LatLng> decodePoly(String encoded) {

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