How to move an icon on the map behind my location?

Asked

Viewed 1,179 times

2

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

I leave part of my code essential to 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)); 
}
  • I suggest you debug to understand what is happening. Know debug on Android?

  • Yes I know, but it’s gonna be tricky since inside the house it’s hard to pick up GPS signal

  • You can debug in the emulator and simulate GPS positions in the DDMS. See this tutorial.

  • Thanks for the tutorial. I will try to see if I can solve the problem.

2 answers

2

Dude, I change the position of the icon like this, this method is very simple and you can make several cool modifications. Just and pass the Latlng to him that the same does the 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));
    }

1

Only with this excerpt is very complicated to say what is happening, it would take all the code of the class that takes care of the upgrade of your Marketer.

But from what you have said, to be updating only once, look in your code if you have correctly registered the location log, which is called whenever there is a change in the location.

And remember to make the manual call to your method if the user gets stuck, because in that case you will not receive location update, so you have to force the new position of your police icon.

If you are in doubt how to receive these updates, I recommend the official documentation guide Receiving Location Updates.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.