Android: access saved google locations programmati

2019-06-17 04:52发布

问题:

Google maps allows the possibility of saving favourite locations for ease of access later... see here.

In Android, where a location is required to be entered, it would be neat if there were an API to enable the user to choose from their already-saved favourite locations, e.g. via a picker dialog or something along those lines.

I can't find such an API... is there? The closest I've found is this and this, but these don't seem to allow picking from your stored locations.

回答1:

There really isn't a way to do this, but you could create your own method to do it. You could have an ImageButton with a star (or anything that symbolizes 'save'). Use SharedPreferences to save the latitudes and longitudes that the users have entered, and then every time the application is loaded up, you could check to see if there are some values in the SharedPreferences. If there are, you could use a ListView to show all the saved locations, and then when one is clicked, return to the parent activity with the Location. Finally, use the Location to orient the map correctly.



回答2:

Google/Android doesn't provide any public API to access the starred/frequent locations of a user and that data is only shared amongst the Google apps.

As this answer shows it might be possible to fetch location data via the Google Plus and Google Identity platform APIs

Alternatively (or in addition to), using Google's Plus Platform / Google Identity Platform, if you have an authenticated user, you can make an API call as described here (with examples) to get any of the specified fields for the current user (including currentLocation, organizations (including work) with addresses, if the user provided them).

Take a look at this link.