calvin sugianto
calvin sugianto

Reputation: 640

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

Upvotes: 0

Views: 421

Answers (2)

calvin sugianto
calvin sugianto

Reputation: 640

i got the solution! Just check the place type, if place type is 0, just use the address because it is place from random selection with 'select this location' and the output is the coordinate

@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);

            if (place.getPlaceTypes().get(0).intValue() == 0) {
                mLocationNameTextView.setText(place.getAddress().toString());
            } else {
                mLocationNameTextView.setText(place.getName(););
            }              
        }
    }

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

Upvotes: 0

Raj
Raj

Reputation: 3001

To summarize, the question is when I select a random point using PlacePicker, I am not able to get the name of that place?

Answer -

The reason for this is that google provides latitude and longitude to every place on this earth. So almost all places have address. But when it comes to name, all places do not have a name. So when you select a random point from PlacePicker it may or may not have name assigned to it. If it has name then place.getName() will provide you that. But if it doesn't have name place.getName() will provide you the latitude and longitude of that place.

Also i have verified this by using GeoDataClient API by passing the place.getId() and retrieving the name of the place. But it also returns the name which place has name else it returns latitude and longitude of that place.

And when it comes to PlaceAutoComplete, it shows only that places which can be searched by the name.

Upvotes: 1

Related Questions