我在我的项目中将google maps api作为片段实现,但是在添加setmylocationenabled(true)时出现错误。请帮我解决一下这个。 错误是: - “调用需要权限,可能被用户拒绝。代码应明确检查权限是否可用(使用'checkPermission')或显式处理潜在的'安全例外'
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
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.OnMapReadyCallback;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.testgmaps.R;
public class GmapFragment extends Fragment implements OnMapReadyCallback {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_gmaps, container,false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
MapFragment fragment = (MapFragment)getChildFragmentManager().findFragmentById(R.id.map);
fragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap mMap) {
mMap.setMyLocationEnabled(true);
LatLng marker = new LatLng(-33.867, 151.206);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 13));
mMap.addMarker(new MarkerOptions().title("Google Maps Test Marker").position(marker));
}
}