私は、これは前に頼まれましたが、利用可能なすべてのソリューションは、私が座標を取得するためにgetLatitude()
とgetLongitude()
メソッドを使用できるようにonLocationChanged()
メソッドを使用するように私を必要とすることを知っています。私のデバイスの場所が変更されたとき現在の場所はどのように入手できますか? Androidのメーカー
しかしonLocationChanged()
が呼び出されます。だから私の現在の場所を取得するには私のデバイスを移動する必要があります。
しかし、私は自分のデバイスを移動することなく、現在の座標を取得したいです。また私はどこかで私はonLocationChanged()
メソッドを手動で呼び出すことができ、私は最後の既知の場所を得ることができますが、もう一度、
最後の既知の場所は私の現在の場所ではない可能性がありますか?または最後の既知のロケーションソリューションを使用してもよろしいですか?
これは私がLocationListenerを実装するために使用していたクラスの一部です。
public class MyLocListener extends MapsActivity implements LocationListener {
public void onLocationChanged(Location location)
{
if(location!=null)
{
MapsActivity.setLatitude(location.getLatitude());
MapsActivity.setLongitude(location.getLongitude());
}
}}
これは、マップアクティビティに座標を提供するために使用している関数のオーバーライドです。緯度と経度は2つのdouble型変数です。このクラスでは、緯度と経度の変数は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));
}
あなたがウェブ上で検索した場合、「アンドロイド現在の場所は、」あなたはhttps://developer.android.com/training/location/retrieve-current.html –
はステップコードバイステップのためのルック答えを持っていますhttp://stackoverflow.com/a/38397092/5955362 –