gpt4 book ai didi

android - 在谷歌地图上绘制路线

转载 作者:行者123 更新时间:2023-11-29 02:04:30 25 4
gpt4 key购买 nike

我想在谷歌地图上绘制一条路线,并使用 GPS 改变我的位置。随着我的位置变化(当创建新的地理点时),谷歌地图上的点会移动,但我无法在 map 上画线。

请帮助在谷歌地图上绘制路线。下面是我的代码

`

LocationManager locman;
LocationListener loclis;
Location location;
private MapView map;

List<GeoPoint> geoPointsArray = new ArrayList<GeoPoint>();
private MapController controller;
String provider = LocationManager.GPS_PROVIDER;
double lat;
double lon;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initMapView();
initMyLocation();
locman = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
//locman.requestLocationUpdates(provider,60000, 100,loclis);
//Location = locman.getLastKnownLocation(provider);

}
/** Find and initialize the map view. */
private void initMapView() {
map = (MapView) findViewById(R.id.mapView);
controller = map.getController();
map.setSatellite(false);
map.setBuiltInZoomControls(true);
}

/** Find Current Position on Map. */
private void initMyLocation() {
final MyLocationOverlay overlay = new MyLocationOverlay(this, map);
overlay.enableMyLocation();
overlay.enableCompass(); // does not work in emulator
overlay.runOnFirstFix(new Runnable() {
public void run() {
// Zoom in to current location
controller.setZoom(16);
controller.animateTo(overlay.getMyLocation());
}
});
map.getOverlays().add(overlay);
}

public void onLocationChanged(Location location) {
if (location != null){
lat = location.getLatitude();
lon = location.getLongitude();
GeoPoint New_geopoint = new GeoPoint((int)(lat*1e6),(int)(lon*1e6));
controller.animateTo(New_geopoint);

}

}
class MyOverlay extends Overlay{
public MyOverlay(){
}
public void draw(Canvas canvas, MapView mapv, boolean shadow){
super.draw(canvas, mapv, shadow);
Paint paint;
paint = new Paint();
paint.setColor(Color.GREEN);

paint.setAntiAlias(true);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(3);

Projection projection = map.getProjection();
Path p = new Path();
for (int i = 0; i < geoPointsArray.size(); i++) {
if (i == geoPointsArray.size() - 1) {
break;
}
Point from = new Point();
Point to = new Point();
projection.toPixels(geoPointsArray.get(i), from);
projection.toPixels(geoPointsArray.get(i + 1), to);
p.moveTo(from.x, from.y);

canvas.drawLine(from.x, from.y, to.x, to.y, paint);

//p.lineTo(to.x, to.y);
}
}
}

`

最佳答案

你为什么不画一条折线呢?您只需要 LatLng 实例。

var flightPlanCoordinates = [
new google.maps.LatLng(43.290307,-2.884174),
new google.maps.LatLng(41.3973,2.158964),
new google.maps.LatLng(40.462046,-3.809694),
new google.maps.LatLng(38.976895,-1.858366)
];

var flightPath = new google.maps.Polyline({
path: flightPlanCoordinates,
strokeColor: "#FF0000",
strokeOpacity: 1.0,
strokeWeight: 2
});
flightPath.setMap( map );

关于android - 在谷歌地图上绘制路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10786500/

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