gpt4 book ai didi

android - Activity 开始时尝试获取当前位置

转载 作者:行者123 更新时间:2023-11-30 00:36:53 25 4
gpt4 key购买 nike

我想在 Activity 开始时缩放到用户的当前位置。我有两个java类。其中一个是 MapActivity.java,另一个是 MapFragment.java。我正在尝试在 MapFragment.java 上完成这项工作。

  private Actvity activity;
private volatile GoogleMap googleMap;
GoogleApiClient mGoogleApiClient;
LocationRequest mLocationRequest;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = getActivity();
}

MapFragment.java/onMapReady ;

@Override
public void onMapReady(GoogleMap map) {
googleMap = map;

mGoogleApiClient=new GoogleApiClient.Builder(activity)
.addApi(LocationServices.API)
.addConnectionCallbacks(activity)
.addOnConnectionFailedListener(activity)
.build();

mGoogleApiClient.connect();

}

在这个 onMapReady 中,.addConnectionCallbacks(activity) 和 .addOnConnectionFailedListener(activity) 发出警告,如图所示;

enter image description here

在我分析过的示例中,他们在实现 GoogleApiClient.ConnectionCallbacks 和 GoogleApiCleint.OnConnectionFailedListener 而不是强制转换之前。不实现,我无法获得这些功能;

 @Override
public void onConnected(Bundle bundle) {

mLocationRequest=LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

mLocationRequest.setInterval(1000);

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest,this);

}

@Override
public void onConnectionSuspended(int i) {

}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

而且我还必须获取 LocationListeners 的方法;

@Override
public void onLocationChanged(Location location) {
if(location == null){
Toast.makeText(this,"Cant get current location",Toast.LENGTH_LONG).show();

}
else{
LatLng ll= new LatLng(location.getLatitude(),location.getLongitude());
CameraUpdate update= CameraUpdateFactory.newLatLngZoom(ll,15);
mGoogleMap.animateCamera(update);
}
}

如果我在 fragment 中手动实现它们,会导致任何问题吗?在 fragment Activity 中获取当前位置的正确方法是什么?

最佳答案

If I implement them by hand in the fragment, does it cause any problem?

没有。您需要做的就是实现 ConnectionCallbacksOnConnectionFailedListener

What is the proper way to get current location in fragment activity?

public class MyFragment extends Fragment implements
ConnectionCallbacks, OnConnectionFailedListener {

// Create an instance of GoogleAPIClient.
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}

protected void onStart() {
mGoogleApiClient.connect();
super.onStart();
}

protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
public void onConnected(Bundle connectionHint) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient); // this is your location
if (mLastLocation != null) {
mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
}
}
}

Reference

关于android - Activity 开始时尝试获取当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43292344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com