首页 > 解决方案 > i got wrong place name (got coordinate) when using Google Maps PlacePicker API

问题描述

i'm using Google Maps SDK right now with PlacePicker SDK for android ( iOS is coming soon) using this code:

int REQUEST_PLACE_PICKER = 1;

PlacePicker.IntentBuilder intentBuilder = new PlacePicker.IntentBuilder();                    
Intent intent = intentBuilder.build(getActivity());
startActivityForResult(intent, REQUEST_PLACE_PICKER);

this is my onActivityResult overriding function:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == REQUEST_PLACE_PICKER) {

        if (resultCode == Activity.RESULT_OK) {

            final Place place = PlacePicker.getPlace(getActivity(), data);

            final CharSequence name = place.getName();

            mLocationNameTextView.setText(name);

        }

    }

    super.onActivityResult(requestCode, resultCode, data);
}

this is my permission:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

the result is good if i'm using autocomplete search, it got the correct place name ( Central Park Mall ) place name from Autocomplete Search

But, if i use Select this location, i got wrong place name

select

the output of place name will be the coordinate of my selection location instead of the street name, so it will be bad information if i put coordinate for my user

bad result

Is there any solution to get the right place name ? It should be possible because i have found app which using Google Maps SDK and placePicker with correct place name from 'Select this location' button.

This is the example app which successfully get the appropriate place name

Correct place name

标签: javaandroidgoogle-mapsgoogle-places-api

解决方案


总而言之,问题是当我使用 PlacePicker 选择一个随机点时,我无法获得该地点的名称?

回答 -

原因是谷歌提供了地球上每个地方的纬度和经度。所以几乎所有的地方都有地址。但说到名字,所有的地方都没有名字。因此,当您从 PlacePicker 中选择一个随机点时,它可能分配有名称,也可能没有分配名称。如果它有名称,那么 place.getName() 将为您提供。但如果它没有名称 place.getName() 将为您提供该地点的纬度和经度。

此外,我通过传递 place.getId() 并检索地点名称,使用 GeoDataClient API 验证了这一点。但它也会返回该地点的名称,否则它会返回该地点的纬度和经度。

而当涉及到 PlaceAutoComplete 时,它​​只显示可以通过名称搜索的地方。


推荐阅读