我试图设置Google Maps API,以便用"map.setMyLocationEnabled(true(;"显示我的位置,但它不起作用。当我在权限检查的情况下写它时,我的应用程序启动了,什么也没发生。但当我删除权限检查时,我的应用程序崩溃了,我得到了这个错误:
java.lang.SecurityException: my location requires permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION
但在我的AndroidManifest.xml文件中,权限是注册的。AndroidManifest.xml(图片(
这是我的活动:
package fr.turtletv.mistergooddeal.geobench;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
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.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class TrouverBancFragment extends Fragment implements OnMapReadyCallback {
GoogleMap map;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.trouver_banc, container, false);
FloatingActionButton zoomOut = v.findViewById(R.id.zoomOut);
FloatingActionButton zoomIn = v.findViewById(R.id.zoomIn);
zoomOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
map.moveCamera(CameraUpdateFactory.zoomBy(-1));
}
});
zoomIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
map.moveCamera(CameraUpdateFactory.zoomBy(1));
}
});
return v;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
// Here's my problem below
if ((ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)) {
map.setMyLocationEnabled(true);
}
// Position des bancs
LatLng banc1 = new LatLng(48.861092, 2.426020);
LatLng banc2 = new LatLng(48.890093, 2.390619);
// Options bancs
MarkerOptions options1 = new MarkerOptions();
options1.position(banc1).title("Banc").snippet("Banc 1");
MarkerOptions options2 = new MarkerOptions();
options2.position(banc2).title("Banc").snippet("Banc 2");
// Ajout des épingles
map.addMarker(options1);
map.addMarker(options2);
map.moveCamera(CameraUpdateFactory.zoomTo((float) 15.0));
map.moveCamera(CameraUpdateFactory.newLatLng(banc1));
}
}
谢谢你。
我找到了问题的解决方案。我只是在设置中授予我的应用程序"位置"权限。解决方案