import com.google.android.gms.maps.SupportMapFragment;
SupportMapFragment mMapFragment = SupportMapFragment.newInstance();
FragmentManager fm = fragment.getChildFragmentManager();
fm.beginTransaction().add(R.id.map, mMapFragment).commit();
mMapFragment.getMapAsync(this);
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), 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;
}
mGoogleMap.setMyLocationEnabled(true);
buildGoogleApiClient();
mGoogleApiClient.connect();
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(getContext())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
public void onConnected(Bundle bundle) {
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), 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;
}
Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
//place marker at current position
mGoogleMap.clear();
//setLocation(mLastLocation.getLatitude(), mLastLocation.getLongitude());
}
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(50000); //50 seconds
mLocationRequest.setFastestInterval(30000); //30 seconds
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
//mLocationRequest.setSmallestDisplacement(0.1F); //1/10 meter
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
Toast.makeText(customAdapter.getContext(), "onConnectionSuspended", Toast.LENGTH_SHORT).show();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Toast.makeText(customAdapter.getContext(), "onConnectionFailed", Toast.LENGTH_SHORT).show();
}
@Override
public void onLocationChanged(Location location) {
sourceLat = location.getLatitude();
sourceLng = location.getLongitude();
getRestaurantDetails();
latLng = new LatLng(location.getLatitude(), location.getLongitude());
//latLng = new LatLng(lat, lng);
//Log.wtf("Lat lng", lat + " " + lng);
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18));
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
其中片段是你viewPagerFragment
的情況下在XML
<LinearLayout
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="250dp" />
這是一個已知的問題,借這個鏈接看看。 http://stackoverflow.com/questions/14419521/moving-mapfragment-surfaceview-causes-black-background-flickering – osayilgan
我有同樣的問題,看看這個鏈接。這解決了這個問題。 https://github.com/jfeinstein10/SlidingMenu/issues/168#issuecomment-11531681 – osayilgan
假設,我有4個片段。在第二和第三個片段中,我顯示地圖,剩下的兩個片斷中沒有顯示地圖.Senerio是:當我從第一個片段滾動到第二個時,此時在地圖上顯示黑屏。然後再次從第2個到第1個和第1個到第2個滾動,顯示地圖。 –