MapBox 显示带有图钉的地图
MapBox showing a map with a pin
我正在使用 MapBox 并显示一个用于设置用户地理编码的图钉。该程序在所有 api 上都能正常工作...除了 19 是黑屏。当我按照此处的文档操作时,我不确定该怎么做:
https://www.mapbox.com/android-sdk/examples/marker/
我的一些代码如下:
Gradle:
compile ('com.mapbox.mapboxsdk:mapbox-android-sdk:4.2.0-beta.1@aar'){
transitive=true
}
compile('com.mapbox.mapboxsdk:mapbox-android-services:1.2.1@aar') {
transitive = true
}
获取地图视图
mapView = (MapView) findViewById(R.id.mapView);
mapView.setStyle(Style.MAPBOX_STREETS);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
地图准备就绪时回调:
public void onMapReady(MapboxMap mapboxMap) {
// Relevant sources shown only
mapboxMap.setStyleUrl(Style.MAPBOX_STREETS);
mapboxMap.setOnInfoWindowClickListener(this);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(geocodeLatLng)
.zoom(15)
.bearing(0)
.build();
mapboxMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
Marker position = mapboxMap.addMarker(new MarkerOptions().position(geocodeLatLng).title(location));
position.setSnippet(address);
position.showInfoWindow(mapboxMap, mapView);
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
mapbox:style_url="mapbox://styles/mapbox/streets-v9"
mapbox:zoom="11"/>
</RelativeLayout>
还有什么我想念的吗?我也有 AndroidManifest.xml 和 gradle 更改。它显然适用于较新的 API 而不是较旧的。该网站说他们支持 API 15+?
不知道为什么,但这修复了它。以前我在最顶部的 MapFragmentActivity 中这样做:
MapboxAccountManager.start(this, getString(R.string.access_token));
事实证明(仔细阅读他们的文档)它应该在 MainActivity.java 中的应用加载时完成。我这样做了并且奏效了。出于某种原因,如果您在 setContentView(R.layout.activity_main);
之前在有问题的 class 中执行此操作,但仅在较新的设备上执行此操作(不确定区别)。
我正在使用 MapBox 并显示一个用于设置用户地理编码的图钉。该程序在所有 api 上都能正常工作...除了 19 是黑屏。当我按照此处的文档操作时,我不确定该怎么做:
https://www.mapbox.com/android-sdk/examples/marker/
我的一些代码如下:
Gradle:
compile ('com.mapbox.mapboxsdk:mapbox-android-sdk:4.2.0-beta.1@aar'){
transitive=true
}
compile('com.mapbox.mapboxsdk:mapbox-android-services:1.2.1@aar') {
transitive = true
}
获取地图视图
mapView = (MapView) findViewById(R.id.mapView);
mapView.setStyle(Style.MAPBOX_STREETS);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
地图准备就绪时回调:
public void onMapReady(MapboxMap mapboxMap) {
// Relevant sources shown only
mapboxMap.setStyleUrl(Style.MAPBOX_STREETS);
mapboxMap.setOnInfoWindowClickListener(this);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(geocodeLatLng)
.zoom(15)
.bearing(0)
.build();
mapboxMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
Marker position = mapboxMap.addMarker(new MarkerOptions().position(geocodeLatLng).title(location));
position.setSnippet(address);
position.showInfoWindow(mapboxMap, mapView);
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
mapbox:style_url="mapbox://styles/mapbox/streets-v9"
mapbox:zoom="11"/>
</RelativeLayout>
还有什么我想念的吗?我也有 AndroidManifest.xml 和 gradle 更改。它显然适用于较新的 API 而不是较旧的。该网站说他们支持 API 15+?
不知道为什么,但这修复了它。以前我在最顶部的 MapFragmentActivity 中这样做:
MapboxAccountManager.start(this, getString(R.string.access_token));
事实证明(仔细阅读他们的文档)它应该在 MainActivity.java 中的应用加载时完成。我这样做了并且奏效了。出于某种原因,如果您在 setContentView(R.layout.activity_main);
之前在有问题的 class 中执行此操作,但仅在较新的设备上执行此操作(不确定区别)。