Search code examples
androidpositionmapboxtrackingmarker

How to get position from markers for tracking? (Android Mapbox)


Here is the problem:

I want to show the user the best route to reach a marker after the user presses a button inside the infoWindow. The problem is, I can't get the marker's Location data due to some problem with Latlng and Position classes. (I am using the Mapbox example to get the route, so I need two Position values)

So basically, I need to update the variable Destination with the marker's position by clicking a button inside the custom infowindow. Yet I have no idea how can I do that even though going through a lot on searching Google and Stack Overflow. Can someone help me? (Cammack!) Thanks a bunch for the help!


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //...

    // Origem: Rodoviaria
    final Position origin = Position.fromCoordinates(-47.882645, -15.794082);
    // Destino: Reitoria
    final Position destination = Position.fromCoordinates(-47.866611, -15.762604);

    //...

        mapboxMap.setInfoWindowAdapter(new MapboxMap.InfoWindowAdapter() {
                @Nullable
                @Override
                public View getInfoWindow(@NonNull Marker marker) {
                    //...

                    final Marker marcador = marker;

                    botaoIr = (Button)v.findViewById(R.id.botaoIr);
                    botaoIr.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {

                            //I have been trying the method below, but I am having trouble with LatLng and Position classes!
                            //LatLng ponto = marcador.getPosition();
                            //destination = new Position(ponto.getLongitude(),ponto.getLatitude());

                            try {
                                getRoute(origin, destination);
                            } catch (ServicesException servicesException) {
                                servicesException.printStackTrace();
                            }
                        }
                    });
                }
            });
    //...

}


Solution

  • To create a position call fromCoordinates().

    destination = Position.fromCoordinates(ponto.getLongitude(),ponto.getLatitude());