2017-04-10 67 views
-1

我有图像视图。在点击这个图片来看,它必须打开谷歌地图,我用下面的代码:打开地图显示当前位置和所需位置按钮单击

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

在上面的代码,我们需要给以下:

来源纬度,源经度,目的纬度和目标经度现在

我的要求如下:

我知道目的地的经纬度。我必须从用户当前位置获取源纬度和经度。我怎样才能做到这一点?

+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错误(this) .addConnectionCallbacks(this)如何解决? – Pra

+0

实施GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener –

相关问题