我正在嘗試向我的應用內的Google地圖片段添加「我的位置」按鈕。這是到目前爲止我的代碼:將「我的位置」添加到Google地圖片段
package com.example.sander.app;
公共類的GoogleMaps擴展片段實現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);
MapView mapView = (MapView) view.findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
mapView.onResume(); // needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
mapView.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
LatLng marker = new LatLng(51.9244201, 4.4777325);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 12));
googleMap.addMarker(new MarkerOptions().title("Testing").position(new LatLng(53.92, 4.47)));
googleMap.addMarker(new MarkerOptions().title("Hello Google Maps!").position(marker));
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), 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;
}
googleMap.setMyLocationEnabled(true); // This is how I had implemented the setMyLocationEnabled method
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
}
}
我已經添加權限,以我的AndroidManifest.xml,和GoogleMap的。 setMyLocationEnable(真);不起作用。
誰能幫我到按鈕添加到我的谷歌地圖片段
用於位置按鈕googleMap.setMyLocationEnabled(真);應該可以工作 –
可以請你發佈你如何實現setMyLocationEnable(true)?您爲Manifest添加了什麼樣的權限? –
我已經添加到代碼 –