Java 如何获取当前位置?安卓工作室
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38242917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i get the current location? Android Studio
提问by Rehan Yousaf
I know that this has been asked before but all the solutions available require me to use onLocationChanged()
method so i can use the getLatitude()
and getLongitude()
methods to get coordinates.
我知道以前有人问过这个问题,但是所有可用的解决方案都要求我使用onLocationChanged()
方法,因此我可以使用getLatitude()
和getLongitude()
方法来获取坐标。
But onLocationChanged()
will be called when the location of my device is changed. So in order to get my current location i would have to move my device.
但是onLocationChanged()
当我的设备位置改变时会被调用。因此,为了获得我当前的位置,我必须移动我的设备。
But i want to get the current coordinates without having to move my device. Also i read somewhere that i can call the onLocationChanged()
method manually and i can get the last known location but again,
但我想获得当前坐标而不必移动我的设备。另外我在某处读到我可以onLocationChanged()
手动调用该方法,但我可以再次获得最后一个已知位置,
isn't it possible that the last known location won't be my current location? or is it ok to use the last known location solution?
最后一个已知位置不是我当前的位置吗?还是可以使用最后一个已知的位置解决方案?
this is part of a class that I am using to implement LocationListener.
这是我用来实现 LocationListener 的类的一部分。
public class MyLocListener extends MapsActivity implements LocationListener {
public void onLocationChanged(Location location)
{
if(location!=null)
{
MapsActivity.setLatitude(location.getLatitude());
MapsActivity.setLongitude(location.getLongitude());
}
}}
this is the override of the function i am using to provide coordinates to my map activity. latitude and longitude are two double type variables. In this class the latitude and longitude variables are initiated with 0 and they remain that way since the onLocationChanged()
function is only called when my location is changed. Hence i can't see my current location on the map.
这是我用来为我的地图活动提供坐标的函数的覆盖。纬度和经度是两个双精度型变量。在这个类中,纬度和经度变量从 0 开始,它们保持这种状态,因为onLocationChanged()
只有在我的位置改变时才会调用该函数。因此我在地图上看不到我当前的位置。
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LocationManager myLocManager;
myLocManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
MyLocListener loc = new MyLocListener();
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.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;
}
myLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, loc);
LatLng sydney = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
采纳答案by thealeksandr
You Should use GooglePlayServices
你应该使用 GooglePlayServices
compile 'com.google.android.gms:play-services-location:7.0.0'
To get location
获取位置
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
After connection
连接后
public class MainActivity extends ActionBarActivity implements
ConnectionCallbacks, OnConnectionFailedListener {
...
@Override
public void onConnected(Bundle connectionHint) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
}
}
}
For more detail information check documentation.
有关更多详细信息,请查看文档。
回答by Kushan
move your code inside location changed. Since getting location is asynchronous,your latitude and longitude never appear reflect. optimize the initializations as needed.
移动你的代码里面的位置改变了。由于获取位置是异步的,因此您的纬度和经度永远不会出现反映。根据需要优化初始化。
public class MyLocListener extends MapsActivity implements LocationListener {
public void onLocationChanged(Location location)
{
if(location!=null)
{
MapsActivity.setLatitude(location.getLatitude());
MapsActivity.setLongitude(location.getLongitude());
LatLng sydney = new LatLng(location.getLatitude(), location.getLongitude);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}}