获取离用户最近的医院名称,无需使用实际的Google地图,只需使用Places API。

huangapple go评论77阅读模式
英文:

Getting name of hospital closest to user without needing to use the actual Google map just the places api

问题

我需要这个应用程序查找用户最近的医院名称并在TextView中显示出来,我所看到的关于此的示例都是使用实际的Google地图和Places API,但我需要只使用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());
        }
    });
}

然而,代码抛出错误,以下是日志输出:

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 ComponentInfo{com.example.blooddonorsystem/com.example.blooddonorsystem.HospitalInNeedActivity}: java.lang.IllegalStateException: Places must be initialized first.
    ...
Caused by: java.lang.IllegalStateException: Places must be initialized first.
    ...

修复了权限问题,现在在调试中得到以下输出:

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...

(注意:上述翻译的代码和日志是基于您提供的原始内容进行的,不代表我正在执行代码或处理日志。)

英文:

I need the app find the name of the closest hospital the user and display it in a TextView, the only examples I've seen of this is using the actual Google maps with the places API however I need to work just using the Places api to get the name of the hospital and return it. This the code I have so far (here I have tried the log the name):

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

However the code throws an error, here's the 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 ComponentInfo{com.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)&#160;
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)&#160;
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)&#160;
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)&#160;
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)&#160;
    at android.os.Handler.dispatchMessage(Handler.java:107)&#160;
    at android.os.Looper.loop(Looper.java:214)&#160;
    at android.app.ActivityThread.main(ActivityThread.java:7356)&#160;
    at java.lang.reflect.Method.invoke(Native Method)&#160;
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)&#160;
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)&#160;

EDIT:

fixed the problem with th permissions now I get this in the debug:

W/looddonorsyste: Accessing hidden method Lsun/misc/Unsafe;-&gt;getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
W/looddonorsyste: Accessing hidden method Lsun/misc/Unsafe;-&gt;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...

答案1

得分: 1

你必须首先初始化谷歌地点 SDK。

if (!Places.isInitialized()) {
    Places.initialize(getApplicationContext(), getString(R.string.api_key), Locale.US);
}
英文:

You have to first initialize google places sdk.

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

huangapple
  • 本文由 发表于 2020年4月10日 23:31:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/61143556.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定