如何在用户位置附近显示兽医
Posted
技术标签:
【中文标题】如何在用户位置附近显示兽医【英文标题】:How to show vets near user location 【发布时间】:2021-10-01 05:37:37 【问题描述】:如何显示用户位置附近的兽医和宠物店我使用的 api 来自 google_maps 活动,这可能是我只想指出的问题。 这是我尝试过的,但是应用程序在日志聊天中崩溃了,它显示了两行我已经用楼梯标记了它,感谢您的帮助。
public class Harita extends FragmentActivity implements OnMapReadyCallback
private GoogleMap mMap;
private ActivityHaritaBinding binding;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_harita);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
@Override
public void onMapReady(GoogleMap googleMap)
mMap = googleMap;
**PlacesSearchResult[] placesSearchResults = new NearbySearch().run().results;
Log.e("response1Tag", placesSearchResults[0].toString());
Log.e("response2Tag", placesSearchResults[1].toString());
double lat1 = placesSearchResults[0].geometry.location.lat;
double lng1 = placesSearchResults[0].geometry.location.lng;
double lat2 = placesSearchResults[1].geometry.location.lat;
double lng2 = placesSearchResults[1].geometry.location.lng;
mMap.addMarker(new MarkerOptions().position(new LatLng(lat1, lng1)));
mMap.addMarker(new MarkerOptions().position(new LatLng(lat2, lng2)));
mMap.setMinZoomPreference(14.0f);
mMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(lat1, lng1)));
public class NearbySearch
public PlacesSearchResponse run()
PlacesSearchResponse request = new PlacesSearchResponse();
GeoApiContext context = new GeoApiContext.Builder()
.apiKey("MY_APİ")
**.build();
LatLng location = new LatLng(-33.8670522, 151.1957362);
try
request = PlacesApi.nearbySearchQuery(context, location)
.radius(5000)
.rankby(RankBy.PROMINENCE)
.keyword("cruise")
.language("en")
.type(PlaceType.VETERINARY_CARE)
.await();
catch (IOException | InterruptedException e)
e.printStackTrace();
finally
return request;
这是错误日志
2021-07-24 21:05:38.747 8645-8645/com.sahin.evcilhayvanrehberi E/androidRuntime: FATAL EXCEPTION: main
Process: com.sahin.evcilhayvanrehberi, PID: 8645
java.lang.RuntimeException: Unable to start activity ComponentInfocom.sahin.evcilhayvanrehberi/com.sahin.evcilhayvanrehberi.Harita: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
Caused by: java.lang.RuntimeException: API key not found. Check that <meta-data android:name="com.google.android.geo.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml
at com.google.maps.api.android.lib6.drd.p.a(:com.google.android.gms.dynamite_mapsdynamite@212418068@21.24.18 (100700-0):6)
at com.google.maps.api.android.lib6.auth.f.i(:com.google.android.gms.dynamite_mapsdynamite@212418068@21.24.18 (100700-0):1)
at com.google.android.gms.maps.internal.CreatorImpl.b(:com.google.android.gms.dynamite_mapsdynamite@212418068@21.24.18 (100700-0):30)
at com.google.android.gms.maps.internal.CreatorImpl.newMapFragmentDelegate(:com.google.android.gms.dynamite_mapsdynamite@212418068@21.24.18 (100700-0):2)
at com.google.android.gms.maps.internal.g.aX(:com.google.android.gms.dynamite_mapsdynamite@212418068@21.24.18 (100700-0):22)
at dx.onTransact(:com.google.android.gms.dynamite_mapsdynamite@212418068@21.24.18 (100700-0):4)
at android.os.Binder.transact(Binder.java:667)
at com.google.android.gms.internal.maps.zza.zzH(com.google.android.gms:play-services-maps@@17.0.1:2)
at com.google.android.gms.maps.internal.zze.zzd(com.google.android.gms:play-services-maps@@17.0.1:3)
at com.google.android.gms.maps.zzav.zza(com.google.android.gms:play-services-maps@@17.0.1:4)
at com.google.android.gms.maps.zzav.createDelegate(com.google.android.gms:play-services-maps@@17.0.1:1)
at com.google.android.gms.dynamic.DeferredLifecycleHelper.zaa(com.google.android.gms:play-services-base@@17.5.0:18)
at com.google.android.gms.dynamic.DeferredLifecycleHelper.onInflate(com.google.android.gms:play-services-base@@17.5.0:20)
at com.google.android.gms.maps.SupportMapFragment.onInflate(com.google.android.gms:play-services-maps@@17.0.1:9)
at androidx.fragment.app.Fragment.onInflate(Fragment.java:1731)
at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:103)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:319)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:298)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
2021-07-24 21:05:38.748 8645-8645/com.sahin.evcilhayvanrehberi E/AndroidRuntime: at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:420)
at android.app.Activity.setContentView(Activity.java:2771)
at androidx.activity.ComponentActivity.setContentView(ComponentActivity.java:380)
at com.sahin.evcilhayvanrehberi.Harita.onCreate(Harita.java:39)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
【问题讨论】:
请附上错误日志并添加您实际期望的内容 @devReddit 我编辑了错误日志,我希望在用户位置附近显示兽医 错误日志显示 `API key not found.` 确保您将 API key 正确添加到应用程序中 【参考方案1】:您的错误日志显示:
Caused by: java.lang.IllegalStateException: Expected Android API level 21+ but was 30
okhttp3
可能不支持 SDK INT 30。您可以通过降级到 SDK INT 28 来尝试。
但是,对于当前情况,添加对 OkHttp 最新版本的显式依赖,无论最新版本现在都存在。这应该可以解决问题。
【讨论】:
应用程序这次再次崩溃,原因不同 2021-07-24 17:22:49.232 5706-5706/com.sahin.evcilhayvanrehberi E/AndroidRuntime: FATAL EXCEPTION: main Process: com.sahin.evcilhayvanrehberi, PID: 5706 java.lang.NullPointerException: 尝试从空数组读取 请添加更新问题的详细错误日志 你生成了api_key并检查了manifest文件吗?以上是关于如何在用户位置附近显示兽医的主要内容,如果未能解决你的问题,请参考以下文章
如何使用firebase作为数据库在android中获取附近的用户名?
如何从 Google Maps android 中的当前位置不断检测附近的标记位置?