Cyrus the Great
Cyrus the Great

Reputation: 5942

Android: Markers is added to mapbox with super delay

I am using map box in my android application. After initializing map box I want to add marker when on longClick on map box so in order according official site I added markerview dependency to application gradle:

dependencies {
  implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-markerview-v7:0.2.0'
}

And then I implements MapboxMap.OnMapLongClickListener and override onMapLongClick.

When mapview is ready I enable enableLocationComponent and create markerViewManager and set map long click listener.

 mapView.getMapAsync(mapboxMap -> {
            this.mapboxMap = mapboxMap;
            mapboxMap.setStyle(Style.MAPBOX_STREETS, style -> {
                createCustomAnimationView();
                moveTo(home_longitude, home_latitude, home_zoom);
                enableLocationComponent();
                markerViewManager = new MarkerViewManager(mapView, mapboxMap);
                mapboxMap.addOnMapLongClickListener(this);
                createCustomAnimationView();
            });
        });

Finally in onMapLongClick overrided method I make a imageview and add to markerViewManager.

@Override
public boolean onMapLongClick(@NonNull LatLng point) {
    ImageView imageView = new ImageView(requireContext());
    imageView.setLayoutParams(new RelativeLayout.LayoutParams(
            (int) Measurement.convertDpToPixel(32, requireContext()),
            (int) Measurement.convertDpToPixel(32, requireContext())));
    imageView.setImageResource(R.drawable.location_ic);
    MarkerView markerView = new MarkerView(new LatLng(point.getLatitude(), point.getLongitude()), imageView);
    markerViewManager.addMarker(markerView);
    return false;
}

When I run application and do long click on screen:

First problem: location_ic appear on the top and left of the screen and after a second or more, icon placed in right place

Other problem: When I move map, those markers stay fixed and not moved with map but after a second or more then placed in right place.

I hope I had explained clearly but if you are not understanding I uploaded a small video !!!
My video

Upvotes: 2

Views: 1698

Answers (2)

Cyrus the Great
Cyrus the Great

Reputation: 5942

after a few days and googleing, I finally decided to use SymbolManager to add marker on mapbox:

just add :

dependencies {
  implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-annotation-v7:0.5.0'
}

to app gradle and then Initialize the plugin on onMapReady like below:

symbolManager = new SymbolManager(mapView, mapboxMap,style);
symbolManager.setIconAllowOverlap(true);
symbolManager.setTextAllowOverlap(true);

and use :

symbolManager.create(new SymbolOptions()
                .withLatLng(point)
                .withIconImage(IMAGE_MARKER_DEFAULT));

for helping look this page and this.

I hope it can be helpful.

Upvotes: 3

PottokDev
PottokDev

Reputation: 41

I'm currently working with markers in mapbox too. But they are referred as "Symbols" in the latest versions.

To use them add the mapbox android sdk dependency to your project (tutorial from mapbox here ) and follow the other tutorial to use the symbol layer ( link here ).

If you have all your markers as a GeoJSON file, you can also add to a custom map that you would style on the mapbox website and then use in your app. ( other informations here )

I hope this can help you, this is my first attempt to answer at someone.

Upvotes: 0

Related Questions