无法使用 Google 地图 API 启用 MyLocation
Can't enable MyLocation with Google Maps API
我试图设置 Google 地图 API 以便使用 "map.setMyLocationEnabled(true);" 显示我的位置,但它不起作用。当我通过权限检查编写它时,我的应用程序启动并且没有任何反应。但是当我删除权限检查时,我的应用程序崩溃了并且出现了这个错误:
java.lang.SecurityException: my location requires permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION
但在我的 AndroidManifest.xml 文件中,权限已注册。
AndroidManifest.xml (image)
这是我的 Activity:
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));
}
}
谢谢。
我找到了解决问题的办法。我只是在设置中向我的应用程序授予位置权限。
Solution
我试图设置 Google 地图 API 以便使用 "map.setMyLocationEnabled(true);" 显示我的位置,但它不起作用。当我通过权限检查编写它时,我的应用程序启动并且没有任何反应。但是当我删除权限检查时,我的应用程序崩溃了并且出现了这个错误:
java.lang.SecurityException: my location requires permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION
但在我的 AndroidManifest.xml 文件中,权限已注册。 AndroidManifest.xml (image)
这是我的 Activity:
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));
}
}
谢谢。
我找到了解决问题的办法。我只是在设置中向我的应用程序授予位置权限。 Solution