Method distanceTo (Select the closest distance between markers)

1

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

Thisdistanceismeasuredandupdatedsmoothlyaslongasthereisonly1markeronthemap:

privateLocationmarcadorLatLong(){Locationlocation=newLocation("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:

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

    
asked by anonymous 19.08.2016 / 17:53

2 answers

2

I'm returning to for the answer to my question and to help others with that same doubt.

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

It will always take the one that is inside the field and next API google maps, this can be a gambiarra but it worked if somebody has something more functional and to be able to post I thank

    
21.08.2016 / 21:41
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();

      }
    }
  }
    
19.08.2016 / 23:56