我在我的手机中使用mapbox
来获取当前位置,但有时它会显示标记,而在logcat中它显示为java.lang.IllegalStateException:GoogleApiClient尚未连接。
注意当我在logcat中单击该行时,它将转到此代码
@Override
public void onConnected(@Nullable Bundle bundle) {
if (ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient,
REQUEST,
this); // LocationListener
}
这是我的java代码:
public class ClockInDetails extends Fragment implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener, LocationListener {
private MapboxMap mMapBoxMap;
private GoogleApiClient mGoogleApiClient = null;
private LatLng gs_var_mLatLng;
private MarkerOptions mMarker;
LocationRequest mLocationRequest;
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(5000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
private static final LocationRequest REQUEST = LocationRequest.create()
.setInterval(5000)
.setFastestInterval(5000)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
public ClockInDetails() {
// Required empty public constructor
}
public static boolean isGPSEnabled = false;
public static boolean isNetworkEnabled = false;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Mapbox.getInstance(getContext(), getString(R.string.access_token));
View view = inflater.inflate(R.layout.fragment_clock_in_details, container, false);
try {
gs_mapView = (MapView)view.findViewById(R.id.gs_mapView);
//to check gps is enabled or not
gs_mapView.onCreate(savedInstanceState);
//mapbox view to display current locations by calling buildgooglepaiclient
gs_mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(MapboxMap mapboxMap) {
mMapBoxMap = mapboxMap;
buildGoogleApiClient();
mapboxMap.setMyLocationEnabled(true);
}
});
locationManager = (LocationManager) getContext().getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
final Dialog dialog = new Dialog(getContext());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.layout_confirmation);
TextView tv_alert_Message = (TextView) dialog.findViewById(R.id.message);
tv_alert_Message.setText("GPS has been disabled in your device. Please enable it?");
Button btn_submit = (Button) dialog.findViewById(R.id.ok);
Button btn_cancel = (Button) dialog.findViewById(R.id.cancel);
btn_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
btn_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent callGPSSettingIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
getContext().startActivity(callGPSSettingIntent);
buildGoogleApiClient();
dialog.dismiss();
}
});
dialog.show();
} else {
mGoogleApiClient = new GoogleApiClient.Builder(getContext())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
mGoogleApiClient = new GoogleApiClient.Builder(getContext())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
} catch (Exception e) {
e.printStackTrace();
}
return view;
}
@Override
public void onStart() {
super.onStart();
gs_mapView.onStart();
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
}
@Override
public void onResume() {
super.onResume();
gs_mapView.onResume();
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
}
@Override
public void onPause() {
try {
super.onPause();
gs_mapView.onPause();
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onStop() {
super.onStop();
gs_mapView.onStop();
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
@Override
public void onLowMemory() {
super.onLowMemory();
gs_mapView.onLowMemory();
}
@Override
public void onDestroy() {
super.onDestroy();
gs_mapView.onDestroy();
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
gs_mapView.onSaveInstanceState(outState);
}
private synchronized void buildGoogleApiClient() {
try {
mGoogleApiClient = new GoogleApiClient.Builder(getContext())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
} catch (Exception e) {
e.printStackTrace();
}
}
private void GS_Funtcion_updateCamera() {
try {
CameraPosition position = new CameraPosition.Builder()
.target(gs_var_mLatLng) // Sets the new camera position
.zoom(16) // Sets the zoom
.bearing(0) // Rotate the camera
.tilt(0) // Set the camera tilt
.build(); // Creates a CameraPosition from the builder
mMapBoxMap.animateCamera(CameraUpdateFactory
.newCameraPosition(position), 1000);
} catch (Exception e) {
e.printStackTrace();
}
}
private void GS_Funtcion_updateMarker() {
try {
mMapBoxMap.clear();
mMarker = new MarkerOptions()
.position(gs_var_mLatLng)
.title("Location")
.snippet("Welcome to you")
.icon(drawableToIcon(getContext(),ServerUtils.Gs_ImagePath+gs_var_user_image, 1));
mMapBoxMap.addMarker(mMarker);
} catch (Exception e) {
e.printStackTrace();
}
}
private void GS_Funtcion_addMarker() {
try {
gs_var_latitude = gs_var_mLatLng.getLatitude();
gs_var_longitude = gs_var_mLatLng.getLongitude();
mMarker = new MarkerOptions()
.position(gs_var_mLatLng)
.title("Location")
.snippet("Welcome to you")
.icon(drawableToIcon(getContext(),ServerUtils.Gs_ImagePath+gs_var_user_image, 1));
mMapBoxMap.addMarker(mMarker);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
try {
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient,
REQUEST,
this); // LocationListener
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onLocationChanged(Location location) {
try {
Log.i("SS","loca--"+location);
gs_var_mLatLng = new LatLng(location.getLatitude(), location.getLongitude());
GS_Funtcion_updateCamera();
if (mMarker != null) {
Log.i("SS","if---loca--"+location);
GS_Funtcion_updateMarker();
}else{
GS_Funtcion_addMarker();
Log.i("SS","else---loca--"+location);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(getContext(),
android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
if (mGoogleApiClient == null) {
buildGoogleApiClient();
}
}
} else {
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
}
我不知道为什么它没有显示,即使该位置已开启,我已连接googleApiClient
中的onStart()
,onResume()
,但为什么它显示未连接.Marker不是在地图上显示。请帮我一个人。
在Android 4.4中我有一个警告来检查gps是否启用弹出窗口没有显示。请帮助我,我做了什么错误,但有时候标记它显示如何?
答案 0 :(得分:0)
在应用mGoogleApiClient.connect()之前;你应该添加一个空检查,即你的代码应该是这样的:
if(mGoogleApiClient!=null)
mGoogleApiClient.connect();