1
我在這裏搜索了類似的問題,但沒有人幫助我。 我有一個應用程序在地圖上運行,顯示用戶所採用的路徑和「警察」形式的圖標。我現在想要的是這個圖標將地圖移到我身後。所以我創建了一個方法(莖),根據用戶的當前位置,圖標的位置和地圖計算新點。該方法的工作原理,但問題是,當我運行我的應用程序圖標只移動一次,因爲他的位置不更新。在Google地圖上移動圖標
這裏是我的代碼部分:
public void onMyLocationChange(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
lat = String.valueOf(latitude);
longi = String.valueOf(longitude);
posCurrent = new LatLng(latitude, longitude);
posAtuais.add(posCurrent);
posInicial = posAtuais.get(0);
Marker marker = map.addMarker(new MarkerOptions().position(posInicial));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(posCurrent, 19));
PolylineOptions options = new PolylineOptions().width(5).color(mudaLinhaCor(heart_rate)).geodesic(true);
for (int z = 0; z < posAtuais.size(); z++) {
LatLng point = posAtuais.get(z);
options.add(point);
}
line = map.addPolyline(options);
LatLng posPolice = stalk(posCurrent, POLICE, map);
Marker init = map.addMarker(new MarkerOptions().position(posPolice)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.police)));
}
//A method that computes a new position
public LatLng stalk(LatLng player,LatLng police,GoogleMap mapView){
Projection projection = mapView.getProjection();
Point pointInicial = new Point(); //police
pointInicial = projection.toScreenLocation(police);
Point pointFinal = new Point(); //player
pointFinal = projection.toScreenLocation(player);
double y=0.2;
int x=0;
if((pointInicial.x==pointFinal.x)){
y=pointInicial.y+1;
}else{
double m=(pointFinal.y-pointInicial.y)/(pointFinal.x-pointInicial.x);
double b=pointInicial.y-(m*pointInicial.x);
int i=1;
while(y != (int)y){
if(pointInicial.x<pointFinal.x){
x=pointInicial.x+i;
//System.out.println("entrou no x<xfnal: "+x);
}
else if(pointInicial.x>pointFinal.x){
//System.out.println("entrou no x>xfnal");
x=pointInicial.x-i;
}
y=m*x+b;
//System.out.println("y: : "+y);
i++;
}
}
return projection.fromScreenLocation(new Point(x, (int) y));
}
有人能幫助我。
我要叫onMarkerDragStart和onMarkerDragEnd在onMyLocationChange?我不明白這是什麼... –