2017-04-10 6 views
-1

私は画像があります。このイメージ図をクリックすると、それがGoogleマップを開く必要がありますし、私は次のコードを使用しています:上記のコードでボタンをクリックすると現在地と希望する場所が表示されます。

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
    Uri.parse("http://maps.google.com/maps?saddr="+latitude_source+","+longitude_source+"&daddr="+latitude_dest+","+longitude_dest)); 

    startActivity(intent); 

を、私たちは以下与える必要があります。

ソースの緯度は、ソース経度、目的地の緯度そして、Destination経度

は今私の要件は以下の通りです:

私は先の緯度と経度を認識しています。ユーザーの現在地からソース緯度と経度を取得する必要があります。それをどうすれば実現できますか?

+0

使用LocationServicesとユーザの現在位置 –

+0

を取得するにはどうすれば緯度と経度の値を得ることができますか? – Pra

+0

のコーディングを手助けすることもできます。また、ロケーションが有効になっていない場合、有効にする必要があります。そうでない場合は、操作が失敗したというメッセージが表示されます。 – Pra

答えて

1
//Handle run time permission 
//implement LocationListener in your Activity or fragment 
//Add required permission in manifest 

private GoogleApiClient mGoogleApiClient; 
private LocationRequest mLocationRequest; 
private Location mLocation; 



mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 
mLocationRequest = LocationRequest.create() 
       .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) 
       .setInterval(60 * 10 * 1000)  // 10 seconds, in milliseconds 
       .setFastestInterval(10 * 1000); 


private void LocationRequest() { 
     if (mGoogleApiClient.isConnected()) { 
      if (ContextCompat.checkSelfPermission(this, 
        android.Manifest.permission.ACCESS_FINE_LOCATION) 
        == PackageManager.PERMISSION_GRANTED) { // to handle permission 6.0 and above 
       mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
       if (mLocation == null) { 
        BuildUtils.Log("Location", "null"); 
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
       } 
      } 
     } 
    } 


@Override 
    public void onLocationChanged(Location location) { //LocationListener implementation 
     mLocation = location; 
} 
詳細については

https://developer.android.com/training/location/retrieve-current.html

+0

これを断片的に行う必要があり、新しいGoogleApiClient.Builder(これ)のエラーが発生します .addConnectionCallbacks(this)解決方法? – Pra

+0

GoogleApiClient.ConnectionCallbacksを実装する、 GoogleApiClient.OnConnectionFailedListener –

関連する問題