谷歌地图输出的kml文件出现问题?

16

全部

我在我的 iPhone 应用程序中使用谷歌地图的 KML 输出。 如果我在浏览器中输入以下内容,它曾经会给出保存 kml 文件的选项:

http://maps.google.com/maps?q=restaurant&mrt=yp&num=10&sll=37.786945,-122.406013&radius=5&output=kml

但是今天突然返回一个HTML文件,发生了什么?有任何想法吗? 我在我的iPhone应用程序中使用它,但它会抛出错误,因为返回的不是有效的XML。显然...

谢谢, mbh


我的KML地图也有同样的问题。我认为我们需要切换到XML或JSON。 - thatguy
天啊,这是我在大学的最后一次测试,现在 KML 不工作了,截止日期只有一周!! :((我该怎么办??? - sephtian
2
谷歌今天刚刚更改了设置,参见https://developers.google.com/maps/documentation/directions/页面末尾,天哪! - sephtian
问题 4321 已在 gmaps-api-issues 列表中开启,可能会有一些官方的反馈(如果你对此问题感兴趣,请为该问题加星)。 - geocodezip
试试这个,它很好用 https://dev59.com/CGgu5IYBdhLWcg3wbWo9#11357351 - sephtian
7个回答

8

自2012年7月27日起,通过解析KML文件从Google提取Google Directions的方法已不再可用(因为Google已更改了检索Google Directions的结构,现在只能通过JSON或XML获取),现在是时候将您的代码迁移到JSON而不是KML。

请参见我的问题的答案(仅适用于Android,但可能适用于iPhone并理解算法并应用)here


7

谷歌做了一些改变,现在只显示重要的转弯。但是使用JSON时,它会正确地显示路径:

public class DrivingDirectionActivity extends MapActivity {

Point p1 = new Point();
Point p2 = new Point();

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MapView mapView = (MapView) findViewById(R.id.map);
    // setting a default value
    double src_lat = 18.5535;
    double src_long = 73.7966;
    double dest_lat = 18.5535;
    double dest_long = 73.7966;

    Geocoder coder = new Geocoder(getApplicationContext(),
            Locale.getDefault());

    List<Address> address_src = null;
    List<Address> address_dest = null;

    try {
        address_src = coder
                .getFromLocationName(
                        "Deepmala Housing Complex, Pimple Saudagar, Pimpri Chinchwad",
                        1);
        if (address_src.size() > 0) {
            Address loc = address_src.get(0);
            src_lat = loc.getLatitude();
            src_long = loc.getLongitude();
        }
    } catch (IOException e) { // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        address_dest = coder.getFromLocationName(
                "Infosys Phase 2, Hinjewadi Phase II, Hinjewadi", 1);
        if (address_dest.size() > 0) {
            Address loc = address_dest.get(0);
            dest_lat = loc.getLatitude();
            dest_long = loc.getLongitude();
        }
    } catch (IOException e) { // TODO Auto-generated catch block
        e.printStackTrace();
    }

    mapView.setBuiltInZoomControls(true);
    GeoPoint srcGeoPoint = new GeoPoint((int) (src_lat * 1E6),
            (int) (src_long * 1E6));
    GeoPoint destGeoPoint = new GeoPoint((int) (dest_lat * 1E6),
            (int) (dest_long * 1E6));

    DrawPath(srcGeoPoint, destGeoPoint, Color.GREEN, mapView);

    mapView.getController().animateTo(srcGeoPoint);
    mapView.getController().setZoom(13);

}

protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

private void DrawPath(GeoPoint src, GeoPoint dest, int color,
        MapView mMapView) {
    // connect to map web service
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(makeUrl(src, dest));
    HttpResponse response;
    try {
        response = httpclient.execute(httppost);

        HttpEntity entity = response.getEntity();
        InputStream is = null;

        is = entity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        sb.append(reader.readLine() + "\n");
        String line = "0";
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        reader.close();
        String result = sb.toString();
        JSONObject jsonObject = new JSONObject(result);
        JSONArray routeArray = jsonObject.getJSONArray("routes");
        JSONObject routes = routeArray.getJSONObject(0);
        JSONObject overviewPolylines = routes
                .getJSONObject("overview_polyline");
        String encodedString = overviewPolylines.getString("points");
        List<GeoPoint> pointToDraw = decodePoly(encodedString);
        mMapView.getOverlays().add(new MyOverLay(pointToDraw));
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
        // TODO: handle exception
    }

}

private List<GeoPoint> decodePoly(String encoded) {

    List<GeoPoint> poly = new ArrayList<GeoPoint>();
    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;

        GeoPoint p = new GeoPoint((int) (((double) lat / 1E5) * 1E6),
                (int) (((double) lng / 1E5) * 1E6));
        poly.add(p);
    }

    return poly;
}

private String makeUrl(GeoPoint src, GeoPoint dest) {
    // TODO Auto-generated method stub

    StringBuilder urlString = new StringBuilder();

    urlString.append("http://maps.googleapis.com/maps/api/directions/json");
    urlString.append("?origin=");// from
    urlString.append(Double.toString((double) src.getLatitudeE6() / 1.0E6));
    urlString.append(",");
    urlString
            .append(Double.toString((double) src.getLongitudeE6() / 1.0E6));
    urlString.append("&destination=");// to
    urlString
            .append(Double.toString((double) dest.getLatitudeE6() / 1.0E6));
    urlString.append(",");
    urlString
            .append(Double.toString((double) dest.getLongitudeE6() / 1.0E6));
    urlString.append("&sensor=false");

    Log.d("xxx", "URL=" + urlString.toString());
    return urlString.toString();
}

class MyOverLay extends Overlay {
    private int pathColor;
    private final List<GeoPoint> points;
    private boolean drawStartEnd;

    public MyOverLay(List<GeoPoint> pointToDraw) {
        // TODO Auto-generated constructor stub
        this(pointToDraw, Color.GREEN, true);
    }

    public MyOverLay(List<GeoPoint> points, int pathColor,
            boolean drawStartEnd) {
        this.points = points;
        this.pathColor = pathColor;
        this.drawStartEnd = drawStartEnd;
    }

    private void drawOval(Canvas canvas, Paint paint, Point point) {
        Paint ovalPaint = new Paint(paint);
        ovalPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        ovalPaint.setStrokeWidth(2);
        ovalPaint.setColor(Color.BLUE);
        int _radius = 6;
        RectF oval = new RectF(point.x - _radius, point.y - _radius,
                point.x + _radius, point.y + _radius);
        canvas.drawOval(oval, ovalPaint);
    }

    public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
            long when) {
        Projection projection = mapView.getProjection();
        if (shadow == false && points != null) {
            Point startPoint = null, endPoint = null;
            Path path = new Path();
            // We are creating the path
            for (int i = 0; i < points.size(); i++) {
                GeoPoint gPointA = points.get(i);
                Point pointA = new Point();
                projection.toPixels(gPointA, pointA);
                if (i == 0) { // This is the start point
                    startPoint = pointA;
                    path.moveTo(pointA.x, pointA.y);
                } else {
                    if (i == points.size() - 1)// This is the end point
                        endPoint = pointA;
                    path.lineTo(pointA.x, pointA.y);
                }
            }

            Paint paint = new Paint();
            paint.setAntiAlias(true);
            paint.setColor(pathColor);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(5);
            paint.setAlpha(90);
            if (getDrawStartEnd()) {
                if (startPoint != null) {
                    drawOval(canvas, paint, startPoint);
                }
                if (endPoint != null) {
                    drawOval(canvas, paint, endPoint);
                }
            }
            if (!path.isEmpty())
                canvas.drawPath(path, paint);
        }
        return super.draw(canvas, mapView, shadow, when);
    }

    public boolean getDrawStartEnd() {
        return drawStartEnd;
    }

    public void setDrawStartEnd(boolean markStartEnd) {
        drawStartEnd = markStartEnd;
    }
}
}

嗯,它只显示主要转弯点..我正在寻找一些解决方案..并已向谷歌发送了邮件..http://code.google.com/p/gmaps-api-issues/issues/detail?id=4321 - Pankaj Kushwaha
嗯...我刚刚编辑了我的代码,现在使用JSON,它正确地显示了路径。 - Pankaj Kushwaha
什么是GeoPoint?我得到了GeoPoint cannot be resolved to a type的错误。我的项目不是Android,而是Java Google地图Web应用程序。请帮帮我。之前它可以使用kml工作,但当我转换为JSON时,出现了相同的问题。 - Piraba
非常抱歉,我还没有从事过Web应用程序开发,目前正在使用谷歌提供的API开发Android应用。 - Pankaj Kushwaha

3

有一个页面,上面清楚地记录了。我最后一次看到它是在2011年初。该页面现在已不可用。 - mbh
它是在mapki.com上吗?那不是官方文档,只是人们能够反向工程的内容。 - barryhunter
我认为Google Places不再支持KML格式(只支持XML和JSON),Google在8月1日更改了他们的文档。很高兴你选择了XML。 - Hesham Saeed

1

-嗯,我刚编辑了我的答案-

让我们面对现实,谷歌已经改变了他们的系统,我们必须跟随他们

那么让我们使用JSON或XML

:)

--第二部分编辑--

我刚找到了最好的解决办法,它使用JSON并解析成折线,所以我们可以做到!

{{link1:Google地图API版本差异}}


0

至于Android,我现在正在使用:

Intent myIntent = 
new Intent( android.content.Intent.ACTION_VIEW,
 Uri.parse( "geo:0,0?q="+ lat +","+ lon ) );                
startActivity(myIntent);

我认为在iOS中应该有类似的东西。



0

我找到了以前使用标准谷歌地图链接获取 KML 输出的方法。

看起来谷歌会分析此类链接的引用者,如果是 https://code.google.com,则会生成 KML 附件而不是显示地图。

因此,首先需要在 https://code.google.com 上创建一个项目。 然后在评论中使用您的路线链接创建一个问题。

现在您可以点击链接并获取 KML 附件。

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接