How do I move an icon on the map behind my location?

2

I'm doing a project that uses the Maps v2 API. In this map my location appears, which is being updated as soon as I move. I put an icon with the police image, and I wanted this icon to come after me every time I move, or if I'm standing still behind me in the same position. I created a stalK method that returns a new point based on my location and the location of the police. I tried to implement it, but when I move the cop moves too, but only once.

I leave part of my code essential for this:

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);

Marker marker1 = map.addMarker(new MarkerOptions().position(POLICE)
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.police)));
    posAtuaisPolice.add(0, POLICE);

    policia = stalk(posCurrent,POLICE, map);
    posAtuaisPolice.add(policia);
    for(int i = 2; i< posAtuaisPolice.size(); i++){
        policia = stalk(posCurrent, posAtuaisPolice.get(i-1), map);
        map.addMarker(new MarkerOptions().position(policia)
                .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)); 
}
    
asked by anonymous 30.01.2014 / 10:33

2 answers

2

Man, I change the position of the icon so, this method is very simple and you can make several legal modifications. Just and pass the LatLng to him who does the same magic. I hope it helps you.

Note: CircleOptions gives a differential in presentation. ;)

private MarkerOptions mMarkerOptions;
private CircleOptions mCircleOptions;
private GoogleMap mGoogleMap;

private void updateMaps(LatLng latLng) {

        if (mMarkerOptions == null && mCircleOptions == null) {
            mMarkerOptions = new MarkerOptions()
                    .position(latLng)
                    .icon(BitmapDescriptorFactory
                            .fromResource(R.drawable.img_mapa_ponto))
                    .title( "Titulo")
                    .snippet("Mensagem");

            mCircleOptions = new CircleOptions().center(latLng).radius(100)
                    .strokeWidth(3.0F)
                    .strokeColor(Color.parseColor("#700000FF"))
                    .fillColor(Color.parseColor("#300000FF"));

        } else {
            mMarkerOptions.position(latLng);
            mMarkerOptions.snippet("Mensagem");
            mCircleOptions.center(latLng);
            mCircleOptions.radius(100);

            mGoogleMap.clear();
        }

        mGoogleMap.addMarker(mMarkerOptions).showInfoWindow();
        mGoogleMap.addCircle(mCircleOptions);

        mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17.0f));
    }
    
11.06.2014 / 22:53
1

Only with this snippet is it too complicated to say what is happening, you would need all the class code that takes care of updating your marker.

But from what you have said, if you are updating only once, look in your code if you have correctly tracked the location listener, which is called whenever there is a change in location.

And remember to make the manual call to your method in case the user is stopped, because in this case you will not receive a local update, then you have to force the new position of your police icon.

If you are in doubt about receiving these updates, I recommend the official documentation guide Receiving Location Updates .

    
10.02.2014 / 04:13