gpt4 book ai didi

android - 用户行走折线

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

我正在尝试在用户行走时在 Android map 中制作折线下面是我的代码。但我只是从我当前位置到其他地方得到一条直线。

public class Map extends MapActivity {

private MapController mapController;
private MapView mapView;
private LocationManager locationManager;

private MyLocationOverlay myLocationOverlay;
GeoPoint geoPoint = null;
GeoPoint despoint = null;
int latitude;
int longitude;
GeoPoint srcGeoPoint;
public HelloItemizedOverlay routeOverlay;

public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.main2); // bind the layout to the activity
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(false);
mapController = mapView.getController();
mapController.setZoom(14); // Zoon 1 is world view
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000,
2, new GeoUpdateHandler());
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null)
{
latitude = (int) (location.getLatitude() * 1E6);
longitude = (int) (location.getLongitude() * 1E6);
}
srcGeoPoint = new GeoPoint(latitude, latitude);
geoPoint = srcGeoPoint;
myLocationOverlay = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(myLocationOverlay);
mapView.getController().setZoom(15);
//mapController.setCenter(geoPoint);
//mapView.getOverlays().add(new HelloItemizedOverlay(srcGeoPoint, srcGeoPoint));

}

@Override
protected boolean isRouteDisplayed() {
return false;
}



public class GeoUpdateHandler implements LocationListener {



@Override
public void onLocationChanged(Location location) {

int lat = (int) (location.getLatitude() * 1E6);
int lng = (int) (location.getLongitude() * 1E6);
GeoPoint changepoint = new GeoPoint(lat, lng);




mapView.getOverlays().add(new HelloItemizedOverlay(srcGeoPoint,changepoint));




}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}



@Override
protected void onResume() {
super.onResume();
myLocationOverlay.enableMyLocation();
myLocationOverlay.enableCompass();
}

@Override
protected void onPause() {
super.onResume();
myLocationOverlay.disableMyLocation();
myLocationOverlay.disableCompass();
}}

覆盖类

public class HelloItemizedOverlay extends Overlay {


private GeoPoint gp1;
private GeoPoint gp2;

public HelloItemizedOverlay(GeoPoint gp1, GeoPoint gp2) {
this.gp1 = gp1;
this.gp2 = gp2;
}
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
Projection projection = mapView.getProjection();
if (shadow == false) {

Paint paint = new Paint();
paint.setAntiAlias(true);
Point point = new Point();
projection.toPixels(gp1, point);
paint.setColor(Color.BLUE);
Point point2 = new Point();
projection.toPixels(gp2, point2);
paint.setStrokeWidth(2);
canvas.drawLine((float) point.x, (float) point.y, (float) point2.x,
(float) point2.y, paint);
}
return super.draw(canvas, mapView, shadow, when);
} }

谁能帮我画一条关于用户行走的线。上面只是画一条从当前位置到某个地方的线

最佳答案

我试图创建一个方法:

-获取一个新的地理点

-将其添加到道路上

-删除之前的道路并显示新的道路 然后每 10 秒调用一次
这就是我的编码方式:

 public void scheduleReceiveLocation(final GeoPoint newPosition) {
final int TEN_SECONDS = 10000;
handler.postDelayed(new Runnable() {
public void run () {
//remove previous point
waypoints.remove(1);
//delete the previous road
map.getOverlays().remove(0);
//add the new GeoPoint
waypoints.add(1,newPosition);
// draw the new road with the new position
new UpdateRoadTask().execute(waypoints);

// this method will contain your almost-finished HTTP calls
handler.postDelayed(this, TEN_SECONDS);
}
}, TEN_SECONDS);
}

然后调用handler.removeCallbacksAndMessages(null);停止

希望对你有帮助

关于android - 用户行走折线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10575952/

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