Android Studio 3.0,Gradle 4.4,Java 1.8,Android 6.0 我尝试显示带有标记的google map。 所以。 在清单文件中:
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
这是我在Fragment中的代码:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
switch (requestCode) {
case 1: {
if (grantResults.length > 0) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Debug.d(TAG, "onRequestPermissionsResult: GRANTED ---> moveMyLocation()");
myLastLocation = getMyLastLocation();
MyApplication.setMyLocation(myLastLocation);
moveMyLastLocation();
updateMarkers();
} else {
Debug.w(TAG, "onRequestPermissionsResult: NOT_GRANTED");
}
} else {
Debug.w(TAG, "onRequestPermissionsResult: NOT GRANEED");
}
}
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
private void updateMarkers() {
for (Marker marker : markersList) {
marker.remove();
}
RealmResults<Merchant> merchantsRealmResults = MerchantService.getMerchants(realm, filter, null);
for (Merchant merchant : merchantsRealmResults) {
RealmList<PointOfSale> pointOfSaleRealmList = merchant.getPointsOfSale();
for (PointOfSale pointOfSale : pointOfSaleRealmList) {
Coordinates coordinates = pointOfSale.getCoordinates();
LatLng latLng = new LatLng(coordinates.getLatitude(), coordinates.getLongitude());
Marker marker = googleMap.addMarker(new MarkerOptions().position(latLng));
marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.ic_map_info_marker));
marker.setTag(pointOfSale);
markersList.add(marker);
}
}
Debug.d(TAG, "updateMarkers: markersList.size = " + markersList.size());
}
结果(logcat):
onMapReady:
onMapReady: ---> permission is already granted
onConnected:
onConnected: ---> permission is already granted ---> updateMarkers() and moveMyLocation()
updateMarkers: filter =
BUNDLE(0):
getMerchants: sortByFieldName = null, filter =
BUNDLE(0):
updateMarkers: current pointOfSale =
id = 238, coordinates = Coordinates{latitude = 35.3237104, longitude = 33.3149413}}
updateMarkers: current pointOfSale =
id = 227, coordinates = Coordinates{latitude = 35.126413, longitude = 33.429859}}
updateMarkers: markersList.size = 2
如您所见,我成功将所有标记添加到markersList,但是地图为空。
但未显示地图。也不会在地图上显示标记。
也在logcat中:
E/Google Maps Android API(21307): Authorization failure. Please see https://developers.google.com/maps/documentation/android-api/start for how to correctly set up the map.
E/Google Maps Android API(21307): In the Google Developer Console
(https://console.developers.google.com)
E/Google Maps Android API(21307): Ensure that the "Google Maps Android API v2" is enabled.
E/Google Maps Android API(21307): Ensure that the following Android Key exists:
E/Google Maps Android API(21307): API Key: AIzaSyxxxx1BRHy1GhlvJvj7dcAmAc-Is
E/Google Maps Android API(21307): Android Application (<cert_fingerprint>;<package_name>): xxxxxB:1A:0D:B9;com.my.projectandroid.customer.debug
为什么不显示地图和标记?
答案 0 :(得分:3)
logcat说您需要设置google maps api并获取有效的api密钥,这就是地图片段显示此空白屏幕的原因。要获取Google Maps api密钥,您需要转到Google Api并将您的项目添加到Google Maps android api。
logcat提供了链接来解决您的问题first link - step 4和second link