2016-07-07 4 views
2

私は、これは前に頼まれましたが、利用可能なすべてのソリューションは、私が座標を取得するために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)); 
    } 
+1

あなたがウェブ上で検索した場合、「アンドロイド現在の場所は、」あなたはhttps://developer.android.com/training/location/retrieve-current.html –

+0

はステップコードバイステップのためのルック答えを持っていますhttp://stackoverflow.com/a/38397092/5955362 –

答えて

0

あなたは詳細情報チェックdocumentationの接続

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())); 
     } 
    } 
} 

後場所

if (mGoogleApiClient == null) { 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
     .addConnectionCallbacks(this) 
     .addOnConnectionFailedListener(this) 
     .addApi(LocationServices.API) 
     .build(); 
} 
if (mGoogleApiClient != null) { 
    mGoogleApiClient.connect(); 
} 

を取得するにはGooglePlayServices

compile 'com.google.android.gms:play-services-location:7.0.0' 

を使用する必要があります。

0

場所の内側にあなたのコードを移動が変更されました。場所の取得は非同期なので、緯度と経度は決して反映されません。必要に応じて初期化を最適化します。

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)); 

    } 
}} 
+0

onLocationUpdateはすぐに、プロバイダが利用可能になるようrequestLocationsの最初の呼び出しからの更新を登録開始します。あなたが時々移動しなくても、更新された場所を返信し続けます – Kushan

関連する問題