Android:如何在 MapBox 中添加自定义标记?

时间:2020-12-26 02:52:12

标签: java mapbox

我在一个 Android 项目中使用 Mapbox 时遇到问题。但是当我使用它时,很难添加自定义标记。

Download Cell Image

我尝试使用标记视图,但它已被弃用。

请多多指教。谢谢。

2 个答案:

答案 0 :(得分:1)

所以这个文档可以帮助你,因为在这个文档中它说这些是需要添加的代码行,我还将包括指向 documentation 的链接他们在 github 上也有一个演示项目同样,我建议您尝试该演示并查看source code,以便您有更好的想法

我建议您尝试这些代码,即使只是作为示例,看看它是否有效

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

答案 1 :(得分:0)

标记已被弃用,以支持注释插件,该插件“简化了在 Mapbox 地图上设置和调整注释的视觉属性的方式”:

https://docs.mapbox.com/android/plugins/guides/annotation/

为了让它工作,你必须在 build.gradle 中添加这个额外的依赖

 implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-annotation-v9:0.9.0'

加载地图后,您可以创建符号管理器并添加标记:

@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/

相关问题