2014-01-24 134 views
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)); 
} 

有人能帮助我。

回答

0

在活动实施OnMarkerDragListener然后

@Override 
public void onMarkerDragStart(Marker arg0) { 
    markerOptions.position(latLng); 
} 
@Override 
public void onMarkerDragEnd(Marker arg0) { 
    myMap.clear(); 
    latLng = arg0.getPosition(); 
    markerOptions.position(latLng); 
    Marker marker = myMap.addMarker(markerOptions); 


} 
+0

我要叫onMarkerDragStart和onMarkerDragEnd在onMyLocationChange?我不明白这是什么... –

相关问题