gpt4 book ai didi

android - 使用 Geocoder (Android) 绘制路径有什么限制吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:40:55 26 4
gpt4 key购买 nike

我能够在两个位置之间绘制一条路径,但如果距离太长 - 超过 300 公里 - 则路径未完全绘制。

我正在使用下面的代码来绘制路径:

class MapOverlay extends com.google.android.maps.Overlay {
Road mRoad;
ArrayList<GeoPoint> mPoints;

public MapOverlay(Road road, MapView mv) {
mRoad = road;
if (road.mRoute.length > 0) {
mPoints = new ArrayList<GeoPoint>();
for (int i = 0; i < road.mRoute.length; i++) {
mPoints.add(new GeoPoint((int) (road.mRoute[i][1] * 1000000),
(int) (road.mRoute[i][0] * 1000000)));
}
int moveToLat = (mPoints.get(0).getLatitudeE6() + (mPoints.get(
mPoints.size() - 1).getLatitudeE6() - mPoints.get(0)
.getLatitudeE6()) / 2);
int moveToLong = (mPoints.get(0).getLongitudeE6() + (mPoints.get(
mPoints.size() - 1).getLongitudeE6() - mPoints.get(0)
.getLongitudeE6()) / 2);
GeoPoint moveTo = new GeoPoint(moveToLat, moveToLong);

MapController mapController = mv.getController();
mapController.animateTo(moveTo);
mapController.setZoom(8);
}
}

@Override
public boolean draw(Canvas canvas, MapView mv, boolean shadow, long when) {
super.draw(canvas, mv, shadow);
drawPath(mv, canvas);
return true;
}

public void drawPath(MapView mv, Canvas canvas) {
int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(3);
for (int i = 0; i < mPoints.size(); i++) {
Point point = new Point();
mv.getProjection().toPixels(mPoints.get(i), point);
x2 = point.x;
y2 = point.y;
if (i > 0) {
canvas.drawLine(x1, y1, x2, y2, paint);
}
x1 = x2;
y1 = y2;
}
}

}

最佳答案

首先,您应该使用 Google Maps API V2 而不是旧的已弃用的 V1 API

在 Activity 中,创建 Google map 和折线引用:

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback{

private GoogleMap mMap;
Polyline line;
//......

首先定义你的航点列表:

List<LatLng> latLngWaypointList = new ArrayList<>();

获取路线,为路线绘制多段线,然后绘制航路点标记:

class GetDirectionsAsync extends AsyncTask<LatLng, Void, List<LatLng>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected List<LatLng> doInBackground(LatLng... params) {
List<LatLng> route = new ArrayList<>();
//populate route......
return route;
}

@Override
protected void onPostExecute(List<LatLng> route) {

if (route == null) return;

if (line != null){
line.remove();
}

PolylineOptions options = new PolylineOptions().width(5).color(Color.MAGENTA).geodesic(true);
for (int i = 0; i < pointsList.size(); i++) {
LatLng point = route.get(i);
//If this point is a waypoint, add it to the list
if (isWaypoint(i)) {
latLngWaypointList.add(point);
}

options.add(point);
}
//draw the route:
line = mMap.addPolyline(options);

//draw waypoint markers:
for (LatLng waypoint : latLngWaypointList) {
mMap.addMarker(new MarkerOptions().position(new LatLng(waypoint.latitude, waypoint.longitude))
.title("Waypoint").icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_VIOLET)));
}

}
}

这里只是 isWaypoint() 的占位符实现:

public boolean isWaypoint(int i) {
//replace with your implementation
if (i % 17 == 0) {
return true;
}
return false;
}

从西海岸到东海岸的路线结果,大约 2500 英里:

enter image description here

较小路线的结果:

enter image description here

请注意,我在此示例中还使用了 Google Directions Api,以使路线与道路对齐。有关如何使用 Google Directions api 的完整示例,请参见此处:https://stackoverflow.com/a/32940175/4409409

关于android - 使用 Geocoder (Android) 绘制路径有什么限制吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5040333/

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