我正在使用所有权限,但我正在获取谷歌地图的黑屏。以下代码用于在我的活动中显示地图。我已在manifest中设置了所有权限。我使用23 Api等级。感谢
MainActivity:
try {
// Loading map
initilizeMap();
// Changing map type
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
// googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
// googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
// googleMap.setMapType(GoogleMap.MAP_TYPE_NONE);
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
googleMap.setMyLocationEnabled(true);
} else {
// Show rationale and request permission.
}
// Enable / Disable zooming controls
googleMap.getUiSettings().setZoomControlsEnabled(false);
// Enable / Disable my location button
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
// Enable / Disable Compass icon
googleMap.getUiSettings().setCompassEnabled(true);
// Enable / Disable Rotate gesture
googleMap.getUiSettings().setRotateGesturesEnabled(true);
// Enable / Disable zooming functionality
googleMap.getUiSettings().setZoomGesturesEnabled(true);
double latitude = 17.385044;
double longitude = 78.486671;
// lets place some 10 random markers
for (int i = 0; i < 10; i++) {
// random latitude and logitude
double[] randomLocation = createRandLocation(latitude,
longitude);
// Adding a marker
MarkerOptions marker = new MarkerOptions().position(
new LatLng(randomLocation[0], randomLocation[1]))
.title("Hello Maps " + i);
Log.e("Random", "> " + randomLocation[0] + ", "
+ randomLocation[1]);
// changing marker color
if (i == 0)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
if (i == 1)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
if (i == 2)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_CYAN));
if (i == 3)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
if (i == 4)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
if (i == 5)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
if (i == 6)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED));
if (i == 7)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
if (i == 8)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_VIOLET));
if (i == 9)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
googleMap.addMarker(marker);
// Move the camera to last position with a zoom level
if (i == 9) {
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(randomLocation[0],
randomLocation[1])).zoom(15).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == MY_LOCATION_REQUEST_CODE) {
if (permissions.length == 1 &&
permissions[0] == android.Manifest.permission.ACCESS_FINE_LOCATION &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
googleMap.setMyLocationEnabled(true);
} else {
// Permission was denied. Display an error message.
}
}
}
@Override
protected void onResume() {
super.onResume();
initilizeMap();
}
/**
* function to load map If map is not created it will create it for you
* */
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
/*
* creating random postion around a location for testing purpose only
*/
private double[] createRandLocation(double latitude, double longitude) {
return new double[] { latitude + ((Math.random() - 0.5) / 500),
longitude + ((Math.random() - 0.5) / 500),
150 + ((Math.random() - 0.5) * 10) };
}
我的应用程序没有崩溃,但我在logcat中收到此错误:
05-05 15:19:39.811 129-555/? E/PHService: SysfsAccess : opening's messed up, /sys/devices/platform/galcore/gpu/gpu0/gpufreq/scaling_governor Permission denied
05-05 15:19:39.811 129-555/? E/PHService: GpuOperator : int android::GpuOperator::setScalingPolicy(android::sp<android::GpuUnitParam>&), check failed!!!!
05-05 15:19:40.032 908-908/? E/Qmage: onDecode : QmageDecodeFrame 20140421 Rev.6376
05-05 15:19:40.032 908-908/? E/Qmage: This is decoding
05-05 15:19:40.032 908-908/? E/Qmage: decoding stream->hasLength()
05-05 15:19:40.032 908-908/? E/Qmage: onDecode : QmageDecParseHeader call : QM
05-05 15:19:40.032 908-908/? E/Qmage: Qmage parsing for decoding ok
05-05 15:19:40.032 908-908/? E/Qmage: onDecode : QmageHeader.NinePatched 0
05-05 15:19:40.032 908-908/? E/Qmage: onDecode : QmageHeader Height() 99 Width() : 77 sampleSize : 1
05-05 15:19:40.032 908-908/? E/Qmage: normal image decoding
05-05 15:19:40.032 908-908/? E/Qmage: onDecode : QmageDecodeFrame call : QM
05-05 15:19:40.032 908-908/? E/Qmage: onDecode : return true QM
05-05 15:19:40.122 129-555/? E/PHService: SysfsAccess : opening's messed up, /sys/devices/platform/galcore/gpu/gpu0/gpufreq/scaling_governor Permission denied
05-05 15:19:40.122 129-555/? E/PHService: GpuOperator : int android::GpuOperator::setScalingPolicy(android::sp<android::GpuUnitParam>&), check failed!!!!
05-05 15:19:40.212 730-730/? E/Qmage: onDecode : QmageDecodeFrame 20140421 Rev.6376
05-05 15:19:40.212 730-730/? E/Qmage: This is decoding
05-05 15:19:40.212 730-730/? E/Qmage: decoding stream->hasLength()
05-05 15:19:40.212 730-730/? E/Qmage: onDecode : QmageDecParseHeader call : QM
05-05 15:19:40.212 730-730/? E/Qmage: Qmage parsing for decoding ok
05-05 15:19:40.212 730-730/? E/Qmage: onDecode : QmageHeader.NinePatched 0
05-05 15:19:40.212 730-730/? E/Qmage: onDecode : QmageHeader Height() 72 Width() : 72 sampleSize : 1
05-05 15:19:40.212 730-730/? E/Qmage: normal image decoding
05-05 15:19:40.212 730-730/? E/Qmage: onDecode : QmageDecodeFrame call : QM
05-05 15:19:40.212 730-730/? E/Qmage: onDecode : return true QM
05-05 15:19:40.232 129-555/? E/PHService: SysfsAccess : opening's messed up, /sys/devices/platform/galcore/gpu/gpu0/gpufreq/scaling_governor Permission denied
05-05 15:19:40.232 129-555/? E/PHService: GpuOperator : int android::GpuOperator::setScalingPolicy(android::sp<android::GpuUnitParam>&), check failed!!!!
05-05 15:19:40.392 129-555/? E/PHService: SysfsAccess : writing's messed up: /sys/devices/system/cpu/cpu1/online, -1, Invalid argument
05-05 15:19:40.392 129-555/? E/PHService: CpuOperator : int android::CpuOperator::setCpuOnlineStatus(unsigned int, int*), check failed!!!!
05-05 15:19:40.392 129-555/? E/PHService: SysfsAccess : opening's messed up, /sys/devices/platform/galcore/gpu/gpu0/gpufreq/scaling_governor Permission denied
05-05 15:19:40.392 129-555/? E/PHService: GpuOperator : int android::GpuOperator::setScalingPolicy(android::sp<android::GpuUnitParam>&), check failed!!!!
05-05 15:19:40.392 625-625/? E/MotionRecognitionService: mReceiver.onReceive : ACTION_USER_PRESENT :: UNLOCK SCREEN
05-05 15:19:40.422 908-908/? E/Launcher: Error finding setting, default accessibility to not found: accessibility_enabled
05-05 15:19:40.512 129-555/? E/PHService: SysfsAccess : writing's messed up: /sys/devices/system/cpu/cpu1/online, -1, Invalid argument
05-05 15:19:40.512 129-555/? E/PHService: CpuOperator : int android::CpuOperator::setCpuOnlineStatus(unsigned int, int*), check failed!!!!
05-05 15:19:40.512 129-555/? E/PHService: SysfsAccess : opening's messed up, /sys/devices/platform/galcore/gpu/gpu0/gpufreq/scaling_governor Permission denied
05-05 15:19:40.512 129-555/? E/PHService: GpuOperator : int android::GpuOperator::setScalingPolicy(android::sp<android::GpuUnitParam>&), check failed!!!!
05-05 15:19:40.562 129-555/? E/PHService: SysfsAccess : opening's messed up, /sys/devices/platform/galcore/gpu/gpu0/gpufreq/scaling_governor Permission denied
05-05 15:19:46.848 2493-2493/? E/SPPClientService: [PackageInfoChangeReceiver] android.intent.action.PACKAGE_REMOVED
05-05 15:19:46.848 2493-2493/? E/SPPClientService: [PackageInfoChangeReceiver] [handlePkgRemovedEvent] PackageName : googlemap.harry.com.googlemapintegratesample, true, false
05-05 15:19:46.848 2493-2493/? E/SPPClientService: [PackageInfoChangeReceiver] [handlePkgRemovedEvent] Ignore Replacing case
05-05 15:19:47.058 970-970/? E/NetworkScheduler.SchedulerReceiver: Invalid parameter app
05-05 15:19:47.058 970-970/? E/NetworkScheduler.SchedulerReceiver: Invalid package name : Perhaps you didn't include a PendingIntent in the extras?
05-05 15:19:47.899 970-970/? E/NetworkScheduler.SchedulerReceiver: Invalid parameter app
05-05 15:19:47.899 970-970/? E/NetworkScheduler.SchedulerReceiver: Invalid package name : Perhaps you didn't include a PendingIntent in the extras?
05-05 15:19:48.350 18757-18757/? E/memtrack: Couldn't load memtrack module (No such file or directory)
05-05 15:19:48.350 18757-18757/? E/android.os.Debug: failed to load memtrack module: -2
05-05 15:19:48.700 129-555/? E/PHService: SysfsAccess : opening's messed up, /sys/devices/platform/galcore/gpu/gpu0/gpufreq/scaling_governor Permission denied
我在Android Manifest中使用了folling权限:
package="googlemap.harry.com.googlemapintegratesample">
<permission
android:name="googlemap.harry.com.googlemapintegratesample.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="googlemap.harry.com.googlemapintegratesample.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<!-- Requires OpenGL ES version 2 -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
布局:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="googlemap.harry.com.googlemapintegratesample.MainActivity">
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
答案 0 :(得分:1)
您可能缺少API密钥的以下权限或元数据标记
${env.CHROME_PATH}/chrome.exe
<uses-library android:name="com.google.android.maps"/>
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="my api key"/>