0

I use MapBox in android studio and show a point in map. Now I want to have a function to get a LatLng variable as an input and show that point on the map.(I want to have a function outside of onMapReady, which, with the call of the function, send points to the function as input and within the function, points appear on the map.). Please guide me

    private MapView mapView;

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

    Mapbox.getInstance(this, YOUR_MAPBOX_ACCESS_TOKEN);

    setContentView(R.layout.activity_main);

    mapView = (MapView) findViewById(R.id.mapView);
    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(new OnMapReadyCallback() {
    @Override
    public void onMapReady(@NonNull MapboxMap mapboxMap) {

        mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
        @Override
        public void onStyleLoaded(@NonNull Style style) {

            // Map is set up and the style has loaded. Now you can add data or make other map adjustments



        }
    });
    }
});
Reza Dehghani
  • 15
  • 1
  • 6

1 Answers1

0

So, Simple. Write below code in onMapReady method and Use this mapboxMap variable to add the marker point on Map.

LatLng latLng = new LatLng(20.5992, 72.9342);
mapboxMap.addMarker(new MarkerOptions().position(latLng).setTitle("set title of marker point"))
Kaushal Panchal
  • 1,785
  • 1
  • 11
  • 27
  • Thank you for your answer. But I want to have a function outside of onMapReady, which, with the call of a function, send points to the function as input and within the function, points appear on the map. – Reza Dehghani Feb 10 '19 at 07:57
  • ok then check this code which i use. **public void addMarker(LatLng latlng, String markerName){ mapboxMap.addMarker(new MarkerOptions().position(latLng).setTitle(markerName)); }** .. and call this funcation like this.**addMarker(latLng, mapsModel.getName());** – Kaushal Panchal Feb 10 '19 at 14:50