In this page

MapView Markers

这篇文档目前尚未提供译文,将以原文展示。

This example shows how to Add markers in bulk

  1. Add markers in bulk to MapView

  2. Click MapView to Add a marker with clicked point

    1. position(point)
    2. title(title)
    3. snippet(snippet);

For all code examples, refer to Android Maps SDK Code Examples

activity_marker.xml view source

1<?xml version="1.0" encoding="utf-8"?>
2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3    xmlns:app="http://schemas.android.com/apk/res-auto"
4    xmlns:tools="http://schemas.android.com/tools"
5    android:layout_width="match_parent"
6    android:layout_height="match_parent"
7    tools:context=".MainActivity">
8
9    <ai.nextbillion.maps.core.MapView
10        android:id="@+id/map_view"
11        android:layout_width="match_parent"
12        android:layout_height="match_parent"
13        app:nbmap_uiAttribution="false"
14        app:nbmap_cameraTargetLat="53.550813508267716"
15        app:nbmap_cameraTargetLng="9.992248999933745"
16        app:nbmap_cameraZoom="12" />
17
18
19    <ImageView
20        android:id="@+id/iv_back"
21        android:layout_width="40dp"
22        android:layout_height="40dp"
23        android:layout_marginLeft="16dp"
24        app:layout_constraintTop_toTopOf="parent"
25        app:layout_constraintLeft_toLeftOf="parent"
26        android:layout_marginTop="16dp"
27        android:background="@drawable/circle_white_bg"
28        android:src="@drawable/icon_back"
29        app:tint="@color/color_back_icon"/>
30
31</androidx.constraintlayout.widget.ConstraintLayout>

MarkersActivity view source

1package ai.nextbillion;
2
3import android.app.ProgressDialog;
4import android.graphics.PointF;
5import android.os.AsyncTask;
6import android.os.Bundle;
7import android.view.Menu;
8import android.view.MenuItem;
9
10import java.io.IOException;
11import java.lang.ref.WeakReference;
12import java.text.DecimalFormat;
13import java.util.ArrayList;
14import java.util.List;
15import java.util.Random;
16
17import ai.nextbillion.maps.annotations.MarkerOptions;
18import ai.nextbillion.maps.core.MapView;
19import ai.nextbillion.maps.core.NextbillionMap;
20import ai.nextbillion.maps.geometry.LatLng;
21import ai.nextbillion.utils.GeoParseUtil;
22import androidx.appcompat.app.AppCompatActivity;
23
24/**
25 * Test activity showcasing adding a large amount of Markers.
26 */
27public class MarkersActivity extends AppCompatActivity {
28
29    private NextbillionMap nextbillionMap;
30    private MapView mapView;
31    private List<LatLng> locations;
32    private ProgressDialog progressDialog;
33    private static final DecimalFormat LAT_LON_FORMATTER = new DecimalFormat("#.#####");
34    List<MarkerOptions> markerOptionsList = new ArrayList<>();
35
36    @Override
37    protected void onCreate(Bundle savedInstanceState) {
38        super.onCreate(savedInstanceState);
39        setContentView(R.layout.activity_marker);
40
41        mapView = findViewById(R.id.mapView);
42        mapView.onCreate(savedInstanceState);
43        mapView.getMapAsync(this::initMap);
44    }
45
46
47    private void initMap(NextbillionMap nextbillionMap) {
48        this.nextbillionMap =  nextbillionMap;
49        nextbillionMap.setStyle(StyleConstants.NBMAP_STREETS);
50        if (locations == null) {
51            progressDialog = ProgressDialog.show(this, "Loading", "Fetching markers", false);
52            new LoadLocationTask(this, 100).execute();
53        } else {
54            showMarkers(100);
55        }
56        nextbillionMap.addOnMapLongClickListener(point -> {
57            addMarker(point);
58            return false;
59        });
60
61        nextbillionMap.addOnMapClickListener(point -> {
62            addMarker(point);
63            return false;
64        });
65    }
66
67    @Override
68    public boolean onCreateOptionsMenu(Menu menu) {
69        getMenuInflater().inflate(R.menu.menu_press_for_marker, menu);
70        return true;
71    }
72
73    private void addMarker(LatLng point) {
74        final PointF pixel = nextbillionMap.getProjection().toScreenLocation(point);
75
76        String title = LAT_LON_FORMATTER.format(point.getLatitude()) + ", "
77                + LAT_LON_FORMATTER.format(point.getLongitude());
78        String snippet = "X = " + (int) pixel.x + ", Y = " + (int) pixel.y;
79
80        MarkerOptions marker = new MarkerOptions()
81                .position(point)
82                .title(title)
83                .snippet(snippet);
84
85        markerOptionsList.add(marker);
86        nextbillionMap.addMarker(marker);
87
88    }
89
90    private void onLatLngListLoaded(List<LatLng> latLngs, int amount) {
91        progressDialog.hide();
92        locations = latLngs;
93        showMarkers(amount);
94    }
95
96    private void showMarkers(int amount) {
97        if (nextbillionMap == null || locations == null || mapView.isDestroyed()) {
98            return;
99        }
100
101        nextbillionMap.clear();
102        if (locations.size() < amount) {
103            amount = locations.size();
104        }
105
106        showGlMarkers(amount);
107    }
108
109    private void showGlMarkers(int amount) {
110        DecimalFormat formatter = new DecimalFormat("#.#####");
111        Random random = new Random();
112        int randomIndex;
113
114        for (int i = 0; i < amount; i++) {
115            randomIndex = random.nextInt(locations.size());
116            LatLng latLng = locations.get(randomIndex);
117            markerOptionsList.add(new MarkerOptions()
118                    .position(latLng)
119                    .title(String.valueOf(i))
120                    .snippet(formatter.format(latLng.getLatitude()) + ", " + formatter.format(latLng.getLongitude())));
121        }
122
123        nextbillionMap.addMarkers(markerOptionsList);
124    }
125
126    private void resetMap() {
127        if (nextbillionMap == null) {
128            return;
129        }
130        markerOptionsList.clear();
131        nextbillionMap.removeAnnotations();
132    }
133
134    @Override
135    protected void onStart() {
136        super.onStart();
137        mapView.onStart();
138    }
139
140    @Override
141    protected void onResume() {
142        super.onResume();
143        mapView.onResume();
144    }
145
146    @Override
147    protected void onPause() {
148        super.onPause();
149        mapView.onPause();
150    }
151
152    @Override
153    protected void onStop() {
154        super.onStop();
155        mapView.onStop();
156    }
157
158    @Override
159    protected void onSaveInstanceState(Bundle outState) {
160        super.onSaveInstanceState(outState);
161        mapView.onSaveInstanceState(outState);
162    }
163
164    @Override
165    protected void onDestroy() {
166        super.onDestroy();
167        if (progressDialog != null) {
168            progressDialog.dismiss();
169        }
170        mapView.onDestroy();
171    }
172
173    @Override
174    public void onLowMemory() {
175        super.onLowMemory();
176        mapView.onLowMemory();
177    }
178
179    private static class LoadLocationTask extends AsyncTask<Void, Integer, List<LatLng>> {
180
181        private WeakReference<MarkersActivity> activity;
182        private int amount;
183
184        private LoadLocationTask(MarkersActivity activity, int amount) {
185            this.amount = amount;
186            this.activity = new WeakReference<>(activity);
187        }
188
189        @Override
190        protected List<LatLng> doInBackground(Void... params) {
191            MarkersActivity activity = this.activity.get();
192            if (activity != null) {
193                String json = null;
194                try {
195                    json = GeoParseUtil.loadStringFromAssets(activity.getApplicationContext(), "points.geojson");
196                } catch (IOException exception) {
197                }
198
199                if (json != null) {
200                    return GeoParseUtil.parseGeoJsonCoordinates(json);
201                }
202            }
203            return null;
204        }
205
206        @Override
207        protected void onPostExecute(List<LatLng> locations) {
208            super.onPostExecute(locations);
209            MarkersActivity activity = this.activity.get();
210            if (activity != null) {
211                activity.onLatLngListLoaded(locations, amount);
212            }
213        }
214    }
215
216    @Override
217    public boolean onOptionsItemSelected(MenuItem item) {
218        switch (item.getItemId()) {
219            case R.id.menuItemReset:
220                resetMap();
221                return true;
222            default:
223                return super.onOptionsItemSelected(item);
224        }
225    }
226}

The given code is an Android activity that demonstrates different functionalities related to working with markers on a map using the Nextbillion Maps SDK. The activity allows users to add markers in bulk, add markers by clicking on the map, and reset the map.

initMapView:

  1. The onCreate method initializes the MapView by setting the content view and obtaining a reference to it using findViewById. Then, the mapView.onCreate(savedInstanceState) method is called to initialize the MapView and pass the saved instance state.

add markers in bulk to MapView:

  1. The showGlMarkers method is responsible for adding markers in bulk to the NextbillionMap.

  2. Generates random indexes and retrieves corresponding LatLng coordinates from the locations list.

  3. Creates a MarkerOptions object for each coordinate, including a title and snippet, and adds them to the markerOptionsList.

  4. Finally, the nextbillionMap.addMarkers(markerOptionsList) method is called to add all the markers to the map.

click MapView to add a marker:

  1. The addOnMapLongClickListener and addOnMapClickListener methods are used to listen for long click and click events on the map, respectively. When a long click or click occurs, the addMarker method is called with the clicked LatLng point.

  2. The method converts the LatLng to a PointF using nextbillionMap.getProjection().toScreenLocation(point).

  3. It creates a MarkerOptions object with the position, title, and snippet based on the clicked point and adds it to both the markerOptionsList and the nextbillionMap using nextbillionMap.addMarker(marker).

Overall, the code demonstrates how to initialize the MapView, add markers in bulk to the NextbillionMap, and add markers by clicking on the map.

没找到你要找的内容?