问题
I am newbie to android development. I am learning on my own using internet and following tutorials. I am making an app which have 3 tabs
, one is for location
second is for camera
and third is for pictures
. For this i am using a tabbed activity
inside android studio
. Inside it i am using mapView
and followed this tutorial and done each step used in it. Moreover i have my apiKey
for google map.
Below are my codes
Manifest
<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;
}
Location layout
<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" />
Moreover at googleMap.setMyLocationEnabled(true);
i am getting a red line which says
I have tried to enter the permission check but couldn't succeed, as i am a newbie so i am facing difficulties finding the right solution(s)
Update 1
After updating my code look like
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
}
}
I am testing it on my android device API 16
and it's giving me build failed
with error `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`
Any help would be appreciated
回答1:
For the Android Version >= M
, the permission has to be asked at Runtime, which can not be done automatically. A general Runtime Permission
asking code. Use getActivity()
if inside Fragment
instead of 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));
}
}
Handle permission:
@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);
}
//}
}
来源:https://stackoverflow.com/questions/41875474/google-map-doesnt-load-into-my-app-device