简体   繁体   中英

Android: Is there a way to get local city wallpapers by checking the user's location?

I was wondering if there might be an API for this. My idea is to be able to fetch a wallpaper of the city or historical landmark based on your geolocation.

I was looking at Picasa and Google but couldn't find anything in particular that might give me a solution.

TL;DR

This is possible with the Location API and Places API . However, you'll have to use both the client-side Android API and the Web Service . Make sure to perform the web request asynchronously, as the Web Service is primarily intended for server-side requests.

Note: Most of this information is directly from developer.android.com and developers.google.com. Here are the relevant links below:

https://developer.android.com/training/location/receive-location-updates.html https://developers.google.com/places/webservice/search https://developers.google.com/places/webservice/photos

DETAIL

You'll mostly need latitude,longitude anyways, so a simple location request is enough for this part. The following snippet of code demonstrates how to retrieve the latitude and longitude on Android.

@Override
public void onLocationChanged(Location location) {
  double latitude = location.getLatitude();
  double longitude = location.getLongitude();
}

In order to get a photo from the Places API, you'll need to use the Web Service API. At minimum, you'll need to perform a Place Search request to get at least one photo reference.

Note: The number of photos returned varies by request.

  • A Nearby Search or a Text Search will return at most one photo element in the array.
  • Radar Searches do not return any photo information.
  • A Details request will return up to ten photo elements.

A Place Search request is an HTTP URL of the following form: https://maps.googleapis.com/maps/api/place/nearbysearch/output?parameters

Here are the parameters to the request.

  • key
  • location - the part you care about. The latitude/longitude around which to retrieve place information. This must be specified as latitude,longitude.
  • radius
  • rankby=distance

The following example is a search request for places of type 'food' within a 500m radius of a point in Sydney, Australia, containing the word 'cruise' in their name:

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&types=food&name=cruise&key=API_KEY


Query the response for the an array of photo objects, photos[] . A photo object will have a photo_reference — a string used to identify the photo when you perform a Photo request. Use this to perform a photo request described below.

A Place Photo request is an HTTP URL of the following form: https://maps.googleapis.com/maps/api/place/photo?parameters

Here are the parameters to the request.

  • key
  • photoreference - The part you really care about.
  • maxheight or maxwidth

An example request is shown below. This request will return the referenced image, resizing it so that it is at most 400 pixels wide.

https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=CnRtAAAATLZNl354RwP_9UKbQ_5Psy40texXePv4oAlgP4qNEkdIrkyse7rPXYGd9D_Uj1rVsQdWT4oRz4QrYAJNpFX7rzqqMlZw2h2E2y5IKMUZ7ouD_SlcHxYq1yL4KbKUv3qtWgTK0A6QbGh87GB3sscrHRIQiG2RrmU_jF4tENr9wGS_YxoUSSDrYjWmrNfeEHSGSc3FyhNLlBU&key=API_KEY

Because you can request a resized image, you don't necessarily need to do custom resizing on the client-side, which is a huge plus for memory allocation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM