Google 地图无法加载到我的应用设备中
Google map doesn't load into my app device
我是 android 开发的新手。我正在使用互联网和遵循教程自学。我正在制作一个有 3 个 tabs
的应用程序,一个用于 location
,第二个用于 camera
,第三个用于 pictures
。为此,我在 android studio
中使用 tabbed activity
。我在其中使用 mapView
并遵循此 tutorial 并完成其中使用的每个步骤。此外,我有 google 地图的 apiKey
。
下面是我的代码
清单
<uses-permission android:name="com.example.accurat.faisal.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"
/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyBhhxKOLbX2I3kNbsZy8lbSXtjuAijKL94"
/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Location.java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflat and return the layout
View rootView = inflater.inflate(R.layout.my_location, container, false);
mMapView = (MapView)rootView.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();
try
{
MapsInitializer.initialize(getActivity().getApplicationContext());
}catch (Exception e)
{
e.printStackTrace();
}
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
googleMap.setMyLocationEnabled(true);
LatLng loc = new LatLng(31.492370,74.329060);
googleMap.addMarker(new MarkerOptions().position(loc).title("I am here"));
// For zooming automatically to the location of the marker
CameraPosition cameraPosition = new CameraPosition.Builder().target(loc).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
});
return rootView;
}
位置布局
<TextView
android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.google.android.gms.maps.MapView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mapView" />
此外,在 googleMap.setMyLocationEnabled(true);
我收到一条红线,上面写着
我尝试进入权限检查但未能成功,因为我是新手,所以我在寻找正确的解决方案时遇到困难
更新 1
更新我的代码后看起来像
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
requestPermissions(new String[]{
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION
},1);
}
else
{
googleMap.setMyLocationEnabled(true);
LatLng loc = new LatLng(31.492370,74.329060);
googleMap.addMarker(new MarkerOptions().position(loc).title("I am Here"));
// For zooming automatically to the location of the marker
CameraPosition cameraPosition = new CameraPosition.Builder().target(loc).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
}
});
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
//finish();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
我正在我的 android 设备 API 16
上测试它,它给了我 build failed
错误 `Error:Execution failed for task ':app:transformClassesWithDexForDebug'。
com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536`
如有任何帮助,我们将不胜感激
对于Android Version >= M
,必须在运行时询问权限,这不能自动完成。一般Runtime Permission
询问代码。如果在 Fragment
内,则使用 getActivity()
而不是 this
.
@Override
public void onMapReady(GoogleMap googleMap) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(ActivityCompat.checkSelfPermission
(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&&
ActivityCompat.checkSelfPermission
(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
requestPermissions(new String[]{
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION
}, 1);
//return;
}
else{
googleMap.setMyLocationEnabled(true);
LatLng loc = new LatLng(31.492370,74.329060);
googleMap.addMarker(new MarkerOptions().position(loc).title("I am here"));
// For zooming automatically to the location of the marker
CameraPosition cameraPosition = new CameraPosition.Builder().target(loc).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
处理权限:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
case 1:
if (grantResults[0] != PackageManager.PERMISSION_GRANTED){
// permission not granted
}
else {
// permission granted
}
break;
//default:
//super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
//}
}
我是 android 开发的新手。我正在使用互联网和遵循教程自学。我正在制作一个有 3 个 tabs
的应用程序,一个用于 location
,第二个用于 camera
,第三个用于 pictures
。为此,我在 android studio
中使用 tabbed activity
。我在其中使用 mapView
并遵循此 tutorial 并完成其中使用的每个步骤。此外,我有 google 地图的 apiKey
。
下面是我的代码
清单
<uses-permission android:name="com.example.accurat.faisal.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"
/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyBhhxKOLbX2I3kNbsZy8lbSXtjuAijKL94"
/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Location.java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflat and return the layout
View rootView = inflater.inflate(R.layout.my_location, container, false);
mMapView = (MapView)rootView.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();
try
{
MapsInitializer.initialize(getActivity().getApplicationContext());
}catch (Exception e)
{
e.printStackTrace();
}
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
googleMap.setMyLocationEnabled(true);
LatLng loc = new LatLng(31.492370,74.329060);
googleMap.addMarker(new MarkerOptions().position(loc).title("I am here"));
// For zooming automatically to the location of the marker
CameraPosition cameraPosition = new CameraPosition.Builder().target(loc).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
});
return rootView;
}
位置布局
<TextView
android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.google.android.gms.maps.MapView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mapView" />
此外,在 googleMap.setMyLocationEnabled(true);
我收到一条红线,上面写着
我尝试进入权限检查但未能成功,因为我是新手,所以我在寻找正确的解决方案时遇到困难
更新 1
更新我的代码后看起来像
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
requestPermissions(new String[]{
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION
},1);
}
else
{
googleMap.setMyLocationEnabled(true);
LatLng loc = new LatLng(31.492370,74.329060);
googleMap.addMarker(new MarkerOptions().position(loc).title("I am Here"));
// For zooming automatically to the location of the marker
CameraPosition cameraPosition = new CameraPosition.Builder().target(loc).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
}
});
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
//finish();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
我正在我的 android 设备 API 16
上测试它,它给了我 build failed
错误 `Error:Execution failed for task ':app:transformClassesWithDexForDebug'。
com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536`
如有任何帮助,我们将不胜感激
对于Android Version >= M
,必须在运行时询问权限,这不能自动完成。一般Runtime Permission
询问代码。如果在 Fragment
内,则使用 getActivity()
而不是 this
.
@Override
public void onMapReady(GoogleMap googleMap) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(ActivityCompat.checkSelfPermission
(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&&
ActivityCompat.checkSelfPermission
(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
requestPermissions(new String[]{
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION
}, 1);
//return;
}
else{
googleMap.setMyLocationEnabled(true);
LatLng loc = new LatLng(31.492370,74.329060);
googleMap.addMarker(new MarkerOptions().position(loc).title("I am here"));
// For zooming automatically to the location of the marker
CameraPosition cameraPosition = new CameraPosition.Builder().target(loc).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
处理权限:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
case 1:
if (grantResults[0] != PackageManager.PERMISSION_GRANTED){
// permission not granted
}
else {
// permission granted
}
break;
//default:
//super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
//}
}