![](/img/trans.png)
[英]How to add custom marker on Mapbox in Android with title and description?
[英]Android: How to add custom marker in MapBox?
我在將 Mapbox 用於一個 android 項目時遇到問題。 但是當我使用它時,很難添加自定義標記。
https://docs.mapbox.com/android/plugins/guides/markerview/
我嘗試使用markerview,但它已被棄用。
請給我提意見。 謝謝。
PS 你能分享一下你使用 Mapbox 10 和 Java 的經驗嗎? Mapbox v 10 文檔僅支持 Kotlin。 謝謝。
因此,本文檔可以幫助您,因為在本文檔中它說這些是需要添加的代碼行,我還將包括該文檔的鏈接 他們在 github 上也有一個演示項目,所以我建議試用該演示並查看源代碼,以便您有更好的想法
我建議您嘗試這些代碼,甚至只是一個示例,看看它是否有效
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".examples.styles.BasicSymbolLayerActivity">
<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
mapbox:mapbox_cameraTargetLat="-32.557013"
mapbox:mapbox_cameraTargetLng="-56.149056"
mapbox:mapbox_cameraZoom="5.526846"/>
</FrameLayout>
BasicSymbolLayerActivity.java
package com.mapbox.mapboxandroiddemo.examples.styles;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.mapbox.geojson.Feature;
import com.mapbox.geojson.FeatureCollection;
import com.mapbox.geojson.Point;
import com.mapbox.mapboxandroiddemo.R;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.maps.Style;
import com.mapbox.mapboxsdk.style.layers.SymbolLayer;
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
import java.util.ArrayList;
import java.util.List;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconAllowOverlap;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconIgnorePlacement;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconImage;
/**
* Display {@link SymbolLayer} icons on the map.
*/
public class BasicSymbolLayerActivity extends AppCompatActivity implements
OnMapReadyCallback {
private static final String SOURCE_ID = "SOURCE_ID";
private static final String ICON_ID = "ICON_ID";
private static final String LAYER_ID = "LAYER_ID";
private MapView mapView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Mapbox access token is configured here. This needs to be called either in your application
// object or in the same activity which contains the mapview.
Mapbox.getInstance(this, getString(R.string.access_token));
// This contains the MapView in XML and needs to be called after the access token is configured.
setContentView(R.layout.activity_style_basic_symbol_layer);
mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
}
@Override
public void onMapReady(@NonNull final MapboxMap mapboxMap) {
List<Feature> symbolLayerIconFeatureList = new ArrayList<>();
symbolLayerIconFeatureList.add(Feature.fromGeometry(
Point.fromLngLat(-57.225365, -33.213144)));
symbolLayerIconFeatureList.add(Feature.fromGeometry(
Point.fromLngLat(-54.14164, -33.981818)));
symbolLayerIconFeatureList.add(Feature.fromGeometry(
Point.fromLngLat(-56.990533, -30.583266)));
mapboxMap.setStyle(new Style.Builder().fromUri("mapbox://styles/mapbox/cjf4m44iw0uza2spb3q0a7s41")
// Add the SymbolLayer icon image to the map style
.withImage(ICON_ID, BitmapFactory.decodeResource(
BasicSymbolLayerActivity.this.getResources(), R.drawable.mapbox_marker_icon_default))
// Adding a GeoJson source for the SymbolLayer icons.
.withSource(new GeoJsonSource(SOURCE_ID,
FeatureCollection.fromFeatures(symbolLayerIconFeatureList)))
// Adding the actual SymbolLayer to the map style. An offset is added that the bottom of the red
// marker icon gets fixed to the coordinate, rather than the middle of the icon being fixed to
// the coordinate point. This is offset is not always needed and is dependent on the image
// that you use for the SymbolLayer icon.
.withLayer(new SymbolLayer(LAYER_ID, SOURCE_ID)
.withProperties(
iconImage(ICON_ID),
iconAllowOverlap(true),
iconIgnorePlacement(true)
)
), new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
// Map is set up and the style has loaded. Now you can add additional data or make other map adjustments.
}
});
}
@Override
public void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onStart() {
super.onStart();
mapView.onStart();
}
@Override
protected void onStop() {
super.onStop();
mapView.onStop();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
}
標記被棄用,取而代之的是注釋插件,它“簡化了在 Mapbox 地圖上設置和調整注釋的視覺屬性的方式”:
https://docs.mapbox.com/android/plugins/guides/annotation/
為了讓它工作,你必須在你的 build.gradle 中添加這個額外的依賴項
implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-annotation-v9:0.9.0'
加載 map 后,您可以創建符號管理器並添加標記:
@Override public void onMapReady(@NonNull final MapboxMap mapboxMap) { mapboxMap.setStyle(Style.DARK, new Style.OnStyleLoaded() { @Override public void onStyleLoaded(@NonNull Style style) { // Set up a SymbolManager instance symbolManager = new SymbolManager(mapView, mapboxMap, style); symbolManager.setIconAllowOverlap(true); symbolManager.setTextAllowOverlap(true); // Add symbol at specified lat/lon symbol = symbolManager.create(new SymbolOptions().withLatLng(new LatLng(60.169091, 24.939876)).withIconImage(MAKI_ICON_HARBOR).withIconSize(2.0f).withDraggable(true)); } }
另請參閱此示例: https://docs.mapbox.com/android/plugins/examples/symbol-listener/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.