嗯,对我来说,在 web 视图中使用谷歌地图并不是最好的主意(除非是你的应用程序的要求)
如果你使用谷歌地图 API 会更好。我将向您展示的代码用于在片段中使用它。实际上,我正在我正在开发的当前应用程序中使用它(并且运行良好:D)我希望这对您有帮助,如果出现问题,请告诉我
fragment_map_layout.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/linearLayout" />
现在在您的 MapFragment
中
public class MapFragment extends Fragment implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private GoogleMap mGoogleMap;
private GoogleApiClient mGoogleApiClient;
private static final int MY_LOCATION_PERMISSION = 100;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map, container, false);
createGoogleClient();
return view;
}
@Override
public void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
public void onStop() {
super.onStop();
try {
mGoogleApiClient.disconnect();
}catch (Exception e){
Log.wtf("onStop: ", "error " + e.getMessage() );
}
}
@Override
public void onResume() {
super.onResume();
setupUI();
}
// remember since Android 6 we have to ask for permissions to the user: this is the callback
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_LOCATION_PERMISSION)
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
// TODO: your code here
}
}
// Here I setup the map and call getMapAsync(); method
private void setupUI(){
SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
// here I init the GoogleApiClient that help me to get the current location and set the callback
private void createGoogleClient(){
if (mGoogleApiClient == null){
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
}
// this method is used to ask for permissions in run time, the validations here is only to know the current device Android version if is > Android 6 ask for permissions
@TargetApi(Build.VERSION_CODES.M)
private boolean findLocation() {
if ((Build.VERSION.SDK_INT < Build.VERSION_CODES.M) || getActivity().checkSelfPermission(ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mGoogleMap.setMyLocationEnabled(true);
mGoogleMap.clear();
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
setCurrentLocation(location);
} else if (shouldShowRequestPermissionRationale(ACCESS_FINE_LOCATION)) {
Toast.makeText(getActivity(), "message for the user", Toast.LENGTH_LONG).show();
} else {
requestPermissions(new String[]{ACCESS_FINE_LOCATION}, MY_LOCATION_PERMISSION);
}
return false;
}
// set the current location in the google map
private void setCurrentLocation(Location location) {
if (location != null) {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 17);
mGoogleMap.animateCamera(cameraUpdate);
}else
mGoogleApiClient.reconnect();
}
private boolean isGpsEnabled() {
LocationManager lm = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);;
boolean isEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!isEnabled) {
android.app.AlertDialog.Builder dialog = new android.app.AlertDialog.Builder(getActivity())
.setMessage("Please, turn up your GPS")
.setPositiveButton("Open settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(myIntent);
}
})
.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
//finish();
Toast.makeText(getActivity().getApplicationContext(), "Es necesario que habilites tu GPS", Toast.LENGTH_SHORT).show();
}
});
dialog.show();
}
return isEnabled;
}
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(19.42499, -99.18644)));
}
@Override
public void onConnected(@Nullable Bundle bundle) {
if (isGpsEnabled())
findLocation();
}
@Override
public void onConnectionSuspended(int i) {
// TODO: your code here
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
// TODO: your code here
}
}
不要忘记在您的buil.gradle 应用中设置compile 'com.google.android.gms:play-services-maps:10.0.1'