首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >android -在自动完成中输入纬度/经度

问android -在自动完成中输入纬度/经度
EN

Stack Overflow用户
提问于 2016-12-26 21:47:39
回答 3查看 786关注 0票数 1

我在我的安卓应用程序中添加了PlaceAutocompleteFragment,当我搜索一个地方时,它运行得很好。我的问题是,如何让用户输入纬度和经度值,并在该坐标上放置标记?我只是想隐藏“没有结果找到”部分,让用户点击进入和获得的坐标。

下面是我到目前为止的情况。

代码语言:javascript
复制
<fragment
        android:id="@+id/place_autocomplete_fragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
        />

选择一个位置时的代码:

代码语言:javascript
复制
PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
          getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {
            String placeName = place.getName().toString();
            LatLng latLng = place.getLatLng();
            mMap.addMarker(new MarkerOptions().position(latLng).title(placeName));
            mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
        }
        @Override
        public void onError(Status status) {
            Log.i(TAG, "An error occurred: " + status);
        }
    });

​

​

我想实现一个搜索栏,这很像谷歌地图。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-01-06 03:25:15

与其使用PlaceAutoCompleteFragment,不如使用PlaceAutocompleteIntent:

代码语言:javascript
复制
    try {
        Intent intent = new   
PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY)
                                .build(getThis());
        startActivityForResult(intent, RequestCodes.PLACE_AUTOCOMPLETE_REQUEST_CODE);

    } catch (GooglePlayServicesRepairableException e) {     
//Do something
    } catch (GooglePlayServicesNotAvailableException e) {
//Do something                
    }

这将发送一个意图,而不是直接显示一个片段。然后,通过onResult方法,您可以捕获故障,并在那里执行反向地理编码:

代码语言:javascript
复制
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {

        switch(requestCode){
            case RequestCodes.PLACE_AUTOCOMPLETE_REQUEST_CODE:
                switch(resultCode){
                    case RESULT_OK:
                        Place place = PlaceAutocomplete.getPlace(this, data);
                       //Do something with the address you got.
                        break;
                    case RESULT_ERROR:
                        Status status = PlaceAutocomplete.getStatus(this, data);
                        //In this case, if you typed coordinates instead of an address, 
                        //you can use them to perform a reverse geocoding as you want.
            break;
                    case RESULT_CANCELED:
                        break;
                }
                break;
        }
    } catch (NullPointerException ex) {
        ex.printStackTrace();
    }
}
票数 2
EN

Stack Overflow用户

发布于 2017-01-04 12:48:06

Google将人类可读的地址翻译到地图上的某个位置。

你想要做的是反向地理编码。

安卓有一个地编码器,你可以用于这个目的。您必须实现自己的片段才能做到这一点,但是它不能使用PlaceAutocompleteFragment。

票数 1
EN

Stack Overflow用户

发布于 2017-01-06 12:22:07

@请按照下面的链接查找您的解决方案。

https://developers.google.com/places/android-api/autocomplete#add

在该链接中向下滚动到“以编程方式获取位置预测”部分。

您可以创建一个自定义搜索UI,作为自动完成小部件提供的UI的替代。要做到这一点,应用程序必须以编程的方式获得位置预测。通过调用GeoDataApi.getAutocompletePredictions(),应用程序可以从自动完成服务中获取预测的地名和/或地址列表。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41336584

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档