DistanceTo method (Select the nearest distance between markers)

Asked

Viewed 144 times

1

This may be a difficult question, I have a method in my maps application that generates a distance between the user and the marker:

inserir a descrição da imagem aqui

this distance is measured and updated smoothly as long as there is only 1 marker on the map:

private Location marcadorLatLong() {

    Location location = new Location("PONTO_MARCADO");

    LatLng posicao = new LatLng(29.9917493 , -51.0685212);

    Marker marker2 = mMap.addMarker(new MarkerOptions()
            .position(posicao)
            .title("treste")
            .visible(true));

    location.setLatitude(posicao.latitude);
    location.setLongitude(posicao.longitude);

    return location;
}

public class MyLocationListener implements LocationListener {
    public void onLocationChanged(Location location) {
        Location pointLocation = marcadorLatLong();
        float distance = location.distanceTo(pointLocation);
        Toast.makeText(MapsActivity.this, "Distância do ponto:"+distance, Toast.LENGTH_SHORT).show();
        if(distance <= 3){
            if(posicao != 0)
            {
                posicao = 0;
            }
            mp.seekTo(posicao);
            mp.start();

        }
    }
}

Now when the situation is with multiple markers:

inserir a descrição da imagem aqui

The following event occurs, the distance selection is always with the last marker inserted, and I need the distance selection to always be on that marker that is closest to the user.

2 answers

2

I’m returning to put the answer to my question and help others with that same question.

public void onLocationChanged(Location location) {

        Location target = new Location("target");
        for(int i = 0; i < markerCoords.size(); i++) {
            for (LatLng point : new LatLng[]{markerCoords.get(i)}) {
                target.setLatitude(point.latitude);
                target.setLongitude(point.longitude);


                if (location.distanceTo(target) < 100) {
                    Toast.makeText(getApplicationContext(), "Aquele marcador que estiver até 100 metros da minha loalização atual", Toast.LENGTH_LONG).show();
                }
            }
        }
    }

inserir a descrição da imagem aqui

it will always pick up the one who is inside the field and closer API google maps, this may be a gambiarra but it worked if someone has something more functional and can post I appreciate

1

See if this helps

public class MyLocationListener implements LocationListener
  {
    private ArrayList<Location> m_location;
    private Location m_pointLocation;

    public MyLocationListener()
    {
      super();

      m_location = new ArrayList<>();
      m_pointLocation = marcadorLatLong();
    }

    private float minDistance()
    {
      float result = Float.MAX_VALUE;

      for (Location location:m_location) {
        result = Math.min(m_pointLocation.distanceTo(location),result);
      }

      return result;
    }

    private Location nearLocation()
    {
      Location result = null;
      float distance = Float.MAX_VALUE;

      for (Location location:m_location) {
        if (m_pointLocation.distanceTo(location) < distance) {
          result = location;
        }
      }

      return result;
    }

    @Override
    public void onLocationChanged(Location location)
    {
      m_location.add(location);

      float distance = minDistance();

      Toast.makeText(MapsActivity.this, "Distância do ponto:"+distance, Toast.LENGTH_SHORT).show();
      if(distance <= 3){
        if(posicao != 0)
        {
          posicao = 0;
        }
        mp.seekTo(posicao);
        mp.start();

      }
    }
  }

Browser other questions tagged

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