【问题标题】:Get user's current Lat Long using Places API使用 Places API 获取用户当前的经纬度
【发布时间】:2018-09-23 12:48:48
【问题描述】:

是否可以使用 Places API 而不是 Maps API 获取用户当前的经纬度?我确定有一个过程涉及到用户当前的经纬度,但有可能得到它们吗?

目前我正在创建使用 Places API 的活动,现在我需要用户当前的经纬度,我认为只为该功能添加 Maps API 是不值得的。

【问题讨论】:

  • 你也不需要。使用内置的 LocationManager,或者使用 FusedLocationProvider。获取位置不需要其他两个 API。

标签: android geolocation


【解决方案1】:

如果您的要求是只获取当前位置的经纬度,那么您不需要使用任何 Api。您可以按照以下教程在不使用 Place Api 的情况下实现它 https://www.androidhive.info/2015/02/android-location-api-using-google-play-services/

如果您仍想使用 Place Api,那么您需要使用Places.PlacesDetectionApi.getCurrentPlace 方法创建一个PendingIntent,该PendingIntent 返回一个PlaceLikelihoodBuffer。首先定义 GoogleApiClient 并使用以下代码获取位置 -

PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi.getCurrentPlace( mGoogleApiClient, null );
result.setResultCallback( new ResultCallback<PlaceLikelihoodBuffer>() {
    @Override
    public void onResult( PlaceLikelihoodBuffer likelyPlaces ) {

         PlaceLikelihood placeLikelihood = likelyPlaces.get( 0 );
         String content = "";
         if( placeLikelihood != null && placeLikelihood.getPlace() != null && !TextUtils.isEmpty( placeLikelihood.getPlace().getName() ) )
             content = "Most likely place: " + placeLikelihood.getPlace().getLatLng()+ "\n";
        if( placeLikelihood != null )
            content += "Percent change of being there: " + (int) ( placeLikelihood.getLikelihood() * 100 ) + "%";
        mTextView.setText( content );

        likelyPlaces.release();
    }
});

欲了解更多信息,请参阅这篇文章 - https://code.tutsplus.com/articles/google-play-services-using-the-places-api--cms-23715

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-28
    • 2014-01-21
    • 2017-10-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 2015-09-07
    相关资源
    最近更新 更多