打开地图,显示按钮点击时的当前位置和所需位置

时间:2017-04-10 11:15:07

标签: android google-maps android-intent google-maps-api-3

我有一个图像视图。点击此图片视图后,必须打开Goog​​le地图,我使用以下代码:

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

在上面的代码中,我们需要提供以下内容:

来源纬度,来源经度,目的地纬度和目的地经度

现在我的要求如下:

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

1 个答案:

答案 0 :(得分: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