android:如何在mapbox中添加自定义标记?

lsmd5eda  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(746)

我在一个android项目中使用mapbox时遇到了一个问题。但当我使用它时,很难添加自定义标记。
https://docs.mapbox.com/android/plugins/guides/markerview/
我试着用markerview,但是已经被否决了。
请告诉我。谢谢。

kninwzqo

kninwzqo1#

因此,本文档可以帮助您,因为在本文档中,它说这些是需要添加的代码行,我还将包括到该文档的链接,他们在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);
  }
}
egmofgnx

egmofgnx2#

注解插件“简化了设置和调整mapboxMap上注解的视觉属性的方法”,而不推荐使用标记:
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/

相关问题