这是我的fragment
摘要:
public class MapFragmentTab extends Fragment implements OnMapReadyCallback {
private MapView mapView;
private View rootView;
private GoogleMap googleMap;
private GoogleApiClient mGoogleApiClient;
int MY_PERMISSION_LOCATION = 10;
private static final String TAG = MapFragmentTab.class.getName();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Debug.d(TAG, "onCreateView: savedInstanceState: " + AndroidUtil.bundle2String(savedInstanceState));
rootView = inflater.inflate(R.layout.map_fragment_tab, container, false);
init();
return rootView;
}
private void init() {
// Create an instance of GoogleAPIClient.
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addApi(LocationServices.API)
.build();
}
}
// Called immediately after onCreateView()
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
mapView = rootView.findViewById(R.id.mapView);
if (mapView != null) {
mapView.onCreate(null);
mapView.onResume();
mapView.getMapAsync(this);
}
}
@Override
public void onMapReady(GoogleMap map) {
Debug.d(TAG, "onMapReady: ");
googleMap = map;
setUpMap();
}
public void setUpMap() {
Debug.d(TAG, "setUpMap: ");
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
if (Build.VERSION.SDK_INT >= 23) {
marshmallowGPSPremissionCheck();
} else {
enableMyLocation();
}
}
private void marshmallowGPSPremissionCheck() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && ActivityCompat.checkSelfPermission(getActivity(),
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Debug.d(TAG, "marshmallowGPSPremissionCheck: requestPermissions");
requestPermissions(new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSION_LOCATION);
} else {
enableMyLocation();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
Debug.d(TAG, "onRequestPermissionsResult: ");
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_PERMISSION_LOCATION && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
enableMyLocation();
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
}
private void enableMyLocation() {
Debug.d(TAG, "enableMyLocation: ");
if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(getActivity(), android.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;
}
Toast.makeText(getActivity(), "setMyLocationEnabled = true", Toast.LENGTH_SHORT).show();
googleMap.setMyLocationEnabled(true);
}
}
在Android 4.3上。当地图显示和我点击我的位置时
Button
(右上角)相机成功显示我的位置。
但是当我点击此Button
时,在 Android 6.0 上,什么也没发生。
答案 0 :(得分:0)
public class MapActivity extends Activity implements OnMapReadyCallback {
// private static LatLng goodLatLng = new LatLng(37, -120);
private GoogleMap googleMap;
// private EditText et_address;
public static String TAG = MapActivity.class.getSimpleName();
Context context;
int MY_PERMISSION_LOCATION = 10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
context = MapActivity.this;
// Initial Map
try {
MapFragment mapFragment = ((MapFragment) getFragmentManager().findFragmentById(R.id.map));
mapFragment.getMapAsync(this);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onMapReady(GoogleMap map) {
googleMap = map;
setUpMap();
}
public void setUpMap() {
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
if (Build.VERSION.SDK_INT >= 23) {
marshmallowGPSPremissionCheck();
} else {
enableMyLocation();
}
}
private void marshmallowGPSPremissionCheck() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& checkSelfPermission(
android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(
new String[]{
android.Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSION_LOCATION);
} else {
enableMyLocation();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_PERMISSION_LOCATION
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
enableMyLocation();
}else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
}
private void enableMyLocation() {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.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);
}
}
这是xml文件
<fragment
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment" />