【发布时间】:2018-03-22 11:47:10
【问题描述】:
我想用 viewpager 在选项卡布局中显示地图视图。正如您在图片上方看到的那样。我得到了我的纬度和经度,但它没有在我的位置上显示我的地图和标记。 这是我的这个片段类的代码
public class MapFragment extends Fragment implements OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
com.google.android.gms.location.LocationListener {
private GoogleMap mMap;
private MapView mapView;
private static final int MY_PERMISSION_REQUEST_CODE = 0;
private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 1;
private LocationRequest mlocationRequest;
private GoogleApiClient mgoogleApiClient;
private Location mlastlocation;
private static int UPDATE_INTERVAL = 1000;
private static int FASTEST_INTERVAL = 3000;
private static int DISPLACEMENT = 10;
DatabaseReference ref;
GeoFire geofire;
Marker myCurrentMarker;
public MapFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view;
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_map, container, false);
mapView = (MapView) view.findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
ref = FirebaseDatabase.getInstance().getReference("MyLocation");
geofire = new GeoFire(ref);
setUpLocation();
return view;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[]
permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case MY_PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
if (checkPlaySercives()) {
buildGoogleApiClient();
creatLocationRequest();
displayLocation();
}
}
break;
}
}
private void setUpLocation() {
if (ActivityCompat.checkSelfPermission(getActivity(),
android.Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(getActivity(),
android.Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(), new String[]{
android.Manifest.permission.ACCESS_COARSE_LOCATION,
android.Manifest.permission.ACCESS_FINE_LOCATION
}, MY_PERMISSION_REQUEST_CODE);
} else {
if (checkPlaySercives()) {
buildGoogleApiClient();
creatLocationRequest();
displayLocation();
}
}
}
private void displayLocation() {
if (ActivityCompat.checkSelfPermission(getActivity(),
android.Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(getActivity(),
android.Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
return;
}
mlastlocation =
LocationServices.FusedLocationApi.getLastLocation(mgoogleApiClient);
if (mlastlocation != null) {
final double latitude = mlastlocation.getLatitude();
final double longitute = mlastlocation.getLongitude();
final LatLng latLng = new LatLng(latitude, longitute);
geofire.setLocation("You", new GeoLocation(latitude, longitute),
new GeoFire.CompletionListener() {
@Override
public void onComplete(String key, DatabaseError error)
{
if (myCurrentMarker != null)
myCurrentMarker.remove();
// mMap.addMarker(new
MarkerOptions().position(latLng).title("Your location"));
//
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
getLatitude(latitude);
getLongitude(getLongitude(longitute));
Toast.makeText(getActivity(), latitude + " "
+longitute, Toast.LENGTH_SHORT).show();
}
});
}
}
public static double getLatitude(double lat) {
return lat;
}
public static double getLongitude(double lng) {
return lng;
}
private void creatLocationRequest() {
mlocationRequest = new LocationRequest();
mlocationRequest.setInterval(UPDATE_INTERVAL);
mlocationRequest.setFastestInterval(FASTEST_INTERVAL);
mlocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mlocationRequest.setSmallestDisplacement(DISPLACEMENT);
}
private void buildGoogleApiClient() {
mgoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mgoogleApiClient.connect();
}
private boolean checkPlaySercives() {
int resultCode =
GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity());
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode))
GooglePlayServicesUtil.getErrorDialog(resultCode, getActivity(),
PLAY_SERVICES_RESOLUTION_REQUEST).show();
else {
Toast.makeText(getActivity(), "This device is not supported",
Toast.LENGTH_LONG).show();
}
return false;
}
return true;
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
}
@Override
public void onLocationChanged(Location location) {
mlastlocation = location;
displayLocation();
}
// @Override
// public void onStatusChanged(String provider, int status, Bundle
extras) {
//
// }
//
// @Override
// public void onProviderEnabled(String provider) {
//
// }
//
// @Override
// public void onProviderDisabled(String provider) {
//
// }
@Override
public void onConnected(@Nullable Bundle bundle) {
displayLocation();
startLocationUpdates();
}
private void startLocationUpdates() {
if (ActivityCompat.checkSelfPermission(getActivity(),
android.Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(getActivity(),
android.Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(mgoogleApiClient,
mlocationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
mgoogleApiClient.connect();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
// @Override
// public void onResume() {
// mapView.onResume();
// super.onResume();
// }
@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
}
这段代码在活动上运行得很好,但在选项卡布局视图页面中出现问题。
提前谢谢你!
【问题讨论】:
-
您在 logcat 中是否遇到任何错误?您确定您使用的是正确的 API 密钥吗?
-
是的。在使用标签布局之前,我在活动上做同样的工作然后它正在工作。
-
我在我的代码中注释了这两行 mMap.addMarker(new MarkerOptions().position(latLng).title("Your location")); mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));因为那时我得到了这个例外。尝试在一个空对象引用
-
@Liya 我已经获得了当前我想要的是标签布局内的当前用户位置。请查看我附在这篇文章顶部的 png 链接。
标签: android google-maps android-viewpager android-mapview android-tablayout