gpt4 book ai didi

android - 如何在 google maps api v2 上获取设备的当前位置?

转载 作者:行者123 更新时间:2023-11-29 14:31:04 25 4
gpt4 key购买 nike

我找到了一些方法来做到这一点,但已被弃用或不起作用。我想从设备获取当前的纬度和经度。

这是我获取当前位置的方法,但是 GoogleMap getMyLocation() 方法 is deprecated :

void getCurrentLocation()
{
Location myLocation = map.getMyLocation();
if(myLocation!=null)
{
double dLatitude = myLocation.getLatitude();
double dLongitude = myLocation.getLongitude();
map.addMarker(new MarkerOptions().position(new LatLng(dLatitude, dLongitude))
.title("My Location").icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(dLatitude, dLongitude), 8));

}
else
{
Toast.makeText(this, "Unable to fetch the current location", Toast.LENGTH_SHORT).show();
}
}

最佳答案

针对 api-23 及更高版本:

查看答案here .

针对 api-22 及更低版本:

这实际上非常简单,建议使用 FusedLocationProviderAPI 而不是使用旧的开源 Location API,特别是因为您已经在使用 Google map ,所以您已经在使用 Google Play 服务。

只需设置一个位置监听器,并在每个 onLocationChanged() 回调中更新您当前的位置标记。如果您只想要一个位置更新,只需在第一个回调返回后取消注册回调即可。

public class MainActivity extends FragmentActivity
implements OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
LocationListener {

private GoogleMap map;
private LocationRequest mLocationRequest;
private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
private Marker marker;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}

@Override
protected void onResume() {
super.onResume();

if (mGoogleApiClient == null || !mGoogleApiClient.isConnected()){

buildGoogleApiClient();
mGoogleApiClient.connect();

}

if (map == null) {
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);

mapFragment.getMapAsync(this);
}
}

@Override
public void onMapReady(GoogleMap retMap) {

map = retMap;

setUpMap();

}

public void setUpMap(){

map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
map.setMyLocationEnabled(true);
}

@Override
protected void onPause(){
super.onPause();
if (mGoogleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
}

protected synchronized void buildGoogleApiClient() {
Toast.makeText(this, "buildGoogleApiClient", Toast.LENGTH_SHORT).show();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}

@Override
public void onConnected(Bundle bundle) {
Toast.makeText(this,"onConnected", Toast.LENGTH_SHORT).show();

mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

//mLocationRequest.setSmallestDisplacement(0.1F);

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

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

@Override
public void onLocationChanged(Location location) {
mLastLocation = location;

//remove previous current location Marker
if (marker != null){
marker.remove();
}

double dLatitude = mLastLocation.getLatitude();
double dLongitude = mLastLocation.getLongitude();
marker = map.addMarker(new MarkerOptions().position(new LatLng(dLatitude, dLongitude))
.title("My Location").icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(dLatitude, dLongitude), 8));

}

}

关于android - 如何在 google maps api v2 上获取设备的当前位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31573094/

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