唉!经过大量的努力和搜索,我找不到我的问题。我想在谷歌地图中找到当前位置,但我仍然收到错误(必需的权限)。
调用需要用户可能拒绝的权限:代码应该 明确检查是否有权限(带检查) 许可)或明确处理潜在的'安全例外'
在gMap.setMyLocationEnable(true);
请有人指导我如何解决这个问题?我的代码如下。
AddMasjid.java
package com.example.saroosh.masjidnow.Tabs;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.saroosh.masjidnow.R;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class AddMasjid extends Fragment implements OnMapReadyCallback{
MapView gMapView;
GoogleMap gMap = null;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.add_masjid,container,false);
gMapView = (MapView) v.findViewById(R.id.map);
gMapView.getMapAsync(this);
gMapView.onCreate(savedInstanceState);
gMapView.onResume(); // needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
return v;
}
@Override
public void onMapReady(GoogleMap map) {
gMap = map;
gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new
LatLng(0, 0), 0));
if (gMap != null) {
gMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
gMap.setMyLocationEnabled();
}
}
@Override
public void onResume() {
super.onResume();
gMapView.onResume();
}
@Override
public void onPause() {
super.onPause();
gMapView.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
gMapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
gMapView.onLowMemory();
}
}
add_masjid.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#000000">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentStart="true"
android:text="@string/xyz"
android:textColor="#FFFFFF"
android:textSize="20sp"
/>
<Button
android:id="@+id/generalId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:background="@drawable/rect1"
android:onClick="geoLocate"
android:text="@string/abc"
android:textColor="#FFFFFF"
android:textSize="20sp" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/textView1"
android:layout_toStartOf="@+id/generalId"
android:ems="10"
android:inputType="text"
android:background="#FFFFFF"
android:labelFor="@+id/editText1"
android:layout_above="@+id/map"
android:layout_alignParentTop="true" />
<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/generalId">
</com.google.android.gms.maps.MapView>
</RelativeLayout>
答案 0 :(得分:1)
我刚刚在其中添加了一些权限来解决我的问题。
@Override
public void onMapReady(GoogleMap map) {
gMap = map;
gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new
LatLng(0, 0), 0));
//add this code...
if (gMap != null) {
gMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
if (ActivityCompat.checkSelfPermission(getActivity().getApplicationContext()
, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity().getApplicationContext()
, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
gMap.setMyLocationEnabled(true);
gMap.getUiSettings().setMyLocationButtonEnabled(true);
}
}