获取离用户最近的医院名称,而无需使用实际的 Google 地图,只需使用地点 api

Posted

技术标签:

【中文标题】获取离用户最近的医院名称,而无需使用实际的 Google 地图,只需使用地点 api【英文标题】:Getting name of hospital closest to user without needing to use the actual Google map just the places api 【发布时间】:2020-07-23 09:29:03 【问题描述】:

我需要该应用程序找到用户最近的医院的名称并将其显示在 TextView 中,我见过的唯一示例是使用带有位置 API 的实际 Google 地图,但是我需要使用Places api 获取医院名称并返回。这是我到目前为止的代码(这里我尝试了日志名称):

private void findHospital() 
    List<Place.Field> placeFields = Arrays.asList(Place.Field.NAME);
    FetchPlaceRequest fetchPlaceRequest = FetchPlaceRequest.builder("Hospital",placeFields).build();
    placesClient.fetchPlace(fetchPlaceRequest).addOnSuccessListener(new OnSuccessListener<FetchPlaceResponse>() 
        @Override
        public void onSuccess(FetchPlaceResponse fetchPlaceResponse) 
            Place place = fetchPlaceResponse.getPlace();
            Log.i("Hospital","Place found" + place.getName());
        
    );

但是代码会抛出错误,这里是 logcat:

2020-04-10 16:15:32.877 24773-24773/com.example.blooddonorsystem E/androidRuntime: FATAL EXCEPTION: main
Process: com.example.blooddonorsystem, PID: 24773
java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.blooddonorsystem/com.example.blooddonorsystem.HospitalInNeedActivity: java.lang.IllegalStateException: Places must be initialized first.
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7356)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
 Caused by: java.lang.IllegalStateException: Places must be initialized first.
    at com.google.android.libraries.places.internal.zzft.zzb(com.google.android.libraries.places:places@@2.2.0:11)
    at com.google.android.libraries.places.api.Places.zza(com.google.android.libraries.places:places@@2.2.0:36)
    at com.google.android.libraries.places.api.Places.createClient(com.google.android.libraries.places:places@@2.2.0:30)
    at com.example.blooddonorsystem.HospitalInNeedActivity.onCreate(HospitalInNeedActivity.java:76)
    at android.app.Activity.performCreate(Activity.java:7825)
    at android.app.Activity.performCreate(Activity.java:7814)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
    at android.os.Handler.dispatchMessage(Handler.java:107) 
    at android.os.Looper.loop(Looper.java:214) 
    at android.app.ActivityThread.main(ActivityThread.java:7356) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

编辑:

修复了权限问题,现在我在调试中得到了这个:

W/looddonorsyste: Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
W/looddonorsyste: Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
D/TransportRuntime.SQLiteEventStore: Storing event with priority=DEFAULT, name=LE for destination cct
D/TransportRuntime.JobInfoScheduler: Upload for context TransportContext(cct, DEFAULT, ) is already scheduled. Returning...
D/TransportRuntime.SQLiteEventStore: Storing event with priority=DEFAULT, name=LE for destination cct
D/TransportRuntime.JobInfoScheduler: Upload for context TransportContext(cct, DEFAULT, ) is already scheduled. Returning...

【问题讨论】:

这是重复的,见***.com/questions/56020787/… 谢谢你我修正了我会更新问题 【参考方案1】:

你必须先初始化 google places sdk。

if (!Places.isInitialized()) 
    Places.initialize(getApplicationContext(), getString(R.string.api_key), Locale.US);

【讨论】:

以上是关于获取离用户最近的医院名称,而无需使用实际的 Google 地图,只需使用地点 api的主要内容,如果未能解决你的问题,请参考以下文章

快速搭建 Serverless 人脸识别离线服务

快速搭建 Serverless 人脸识别离线服务

VBA结合GetOject和GetUsername返回登录用户的实际名称而不是代码

Google API 从位置列表中检测离用户最近的位置

如何使用 Google API 进行离线访问

有没有办法检查远程 git repo 是不是有更改,而无需实际获取更改?