android - 实现共享位置地点选择器,与没有地点选择器的 WhatsApp 相同,因为它已被弃用

标签 android kotlin google-places-api google-places googleplacesautocomplete

我们需要共享位置,这是与用户界面和功能类似的 WhatsApp。

我发现 Google Place Picker 已被弃用,并已迁移到 Places Clients。

说喜欢,

当他们尝试搜索时,

可能是,我不擅长解释我的想法,但总的来说,我正在寻找上述类型的实现。

而且我还了解到,Google 会从以下网址对搜索或地点收费,

https://github.com/rtchagas/pingplacepicker

我所累的是,

编码部分:

Places.initialize(applicationContext, "Google API Key")
placesClient = Places.createClient(this)



val autocompleteFragment =
          getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment) as AutocompleteSupportFragment

autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));

autocompleteFragment.setOnPlaceSelectedListener(object : PlaceSelectionListener {
            override fun onPlaceSelected(p0: Place) {
                TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }

            override fun onError(p0: Status) {
                TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }
        });

 val intent = Autocomplete.IntentBuilder(
            AutocompleteActivityMode.FULLSCREEN, fields
        )
            .build(this);
        startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);

搜索两个三个地方后,我收到一条错误消息,提示“重试”。不知道原因。

错误:

enter image description here

非常感谢您的帮助。

最佳答案

自从我在我的应用程序中使用 place picker 后,我遇到了同样的问题,但由于谷歌决定删除它,我别无选择,只能找到解决方案!

我决定使用谷歌地图来重新创建地点选择器关闭体验。

首先,我将 map 创建为全屏,然后将 ImageView(标记)作为其覆盖,如下面的 xml:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/coordinator_layout"
xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.streetdev.goodnecity.SelectLocationPicker" />
    <ImageView
        android:layout_centerInParent="true"
        android:src="@drawable/ic_map"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="56dp"
    android:layout_height="56dp"
    app:backgroundTint="@color/colorPrimaryDark"
    android:src="@drawable/ic_plus"
    android:layout_margin="16dp"
    app:layout_anchor="@id/map"
    app:layout_anchorGravity="bottom|end" />
 </android.support.design.widget.CoordinatorLayout>

因此,每当用户在 map 上导航时,标记都会像 place picker

一样停留在 map 的中心

现在您要做的就是使用 startActivityForResult 启动 map 选择器,您必须使用 gps 处理 cameraPosition(如果可用),然后只要你想返回结果(对我来说,我点击 Fab 按钮),你可以通过这种方式得到结果:

            LatLng latng = mMap.getCameraPosition().target;
            Intent returnIntent = new Intent();
            returnIntent.putExtra("result",String.valueOf(latng.latitude)+";"+String.valueOf(latng.longitude));
            setResult(Activity.RESULT_OK,returnIntent);
            finish();

最后一步是在onActivityResult中处理结果,你得到你的结果,你用";"做一个split来分开经度的纬度

希望我的例子能帮助一些人!

关于android - 实现共享位置地点选择器,与没有地点选择器的 WhatsApp 相同,因为它已被弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56198233/

相关文章:

kotlin - 我可以在Kotlin中访问 “enclosing”对象以进行内联函数调用吗?

ios - Google 放置分页 ios

ios - Google 自动完成地点国家/地区过滤器在 iOS 中不起作用

google-maps - 无法从 Google Place Details API 获取营业时间

java - Action 图像捕捉导致观看时图像模糊

javascript - 如何在 Android 的 React-native 中包含 '.jar' 文件?

split - 如何拆分字符串并在 Kotlin 中拆分部分之间种植分隔符?

android - 在 Android Studio 中生成 Google App Engine 后端的问题

android - Android 上的 Neura SDK 实现

unit-testing - 在 Kotlin 中测试 CoroutineScope 基础设施