gpt4 book ai didi

android - Google Maps API 版本差异

转载 作者:IT王子 更新时间:2023-10-28 23:35:07 34 4
gpt4 key购买 nike

我正在尝试显示两地之间的路线,我想使用 Google Places API V3用于两点之间的路线步骤。


在我使用 Old Google Maps API 之前,并且以下请求给出了完美的结果:

http://maps.google.com/maps?f=d&hl=en&saddr=19.5217608,-99.2615823&daddr=19.531224,-99.248262&ie=UTF8&0&om=0&output=kml

输出:

Old Google Maps API


现在我尝试用新的 Google Maps API 替换它,并且以下请求给出了错误的结果,在这两种情况下我都使用相同的源和目标,但结果在 Google Map 上给出了不同的行为:

http://maps.googleapis.com/maps/api/directions/json?origin=19.5217608,-99.2615823&destination=19.531224,-99.248262&sensor=false

New Google Maps API

我的问题是,新的 Google Maps API 在源和目的地之间返回的步数较少,因此该路线在 Google Map 上显示不完美。

请帮助解决新的 Google Maps API v3 的这个问题。

提前致谢。

最佳答案

我已获取您的请求 URL 并将其粘贴到我的应用程序中,该应用程序使用的是较新版本,并且效果很好。问题可能在于您如何解析数据或解码接收到的 JSON 字符串。 enter image description here

String url = "http://maps.googleapis.com/maps/api/directions/json?origin=19.5217608,-99.2615823&destination=19.531224,-99.248262&sensor=false";

HttpPost httppost = new HttpPost(url);
HttpResponse 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);

//Added line:
mapView.getOverlays().add(new RoutePathOverlay(pointToDraw));

decodePoly() 方法取自 SO 中的另一个问题,我不记得作者了:

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

我还包括了我用来将叠加层添加到 map 本身的内容,我找不到它的教程。抱歉没有给予信任。 (在我发布的第一个方法中添加了对此的调用)

public class RoutePathOverlay extends Overlay {

private int _pathColor;
private final List<GeoPoint> _points;
private boolean _drawStartEnd;

public RoutePathOverlay(List<GeoPoint> points) {
this(points, Color.RED, true);
}

public RoutePathOverlay(List<GeoPoint> points, int pathColor, boolean drawStartEnd) {
_points = points;
_pathColor = pathColor;
_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);
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;
}
}

希望这对你有用。

关于android - Google Maps API 版本差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11323500/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com