How to save lat-lon geolocation point in GAE search document?

Where there is guidance in the GoogleIO talk on Search API , we can search based on geolocation.

I cannot find the appropriate field for storing location information.

How do I save geolocation information in a document so that I can issue queries based on the distance from a specific GPS location?

+6
source share
1 answer

On June 28, 2012, Google integrated the GeoPoint class into the Google App Engine Search API library with the specific intention of making spatial points searchable.

GeoPoints are stored as GeoFields in a search document. Google provides this support documentation that describes how to use GeoPoint with the search API.

The following example declares a GeoPoint and assigns it a GeoField in a search document. These new classes provide much more functionality than the ones listed below, but this code is the starting point for a basic understanding of how to use the new spatial search functions.

Building a document with the appropriate GeoPoint

## IMPORTS ## from google.appengine.api import search def CreateDocument(content, lat, long): geopoint = search.GeoPoint(lat, long) return search.Document( fields=[ search.HtmlField(name='content', value=content), search.DateField(name='date', value=datetime.now().date()) search.GeoField(name='location', value=geopoint) ]) 

GeoPoint document field search (slightly modified from search API documents)

 ## IMPORTS ## from google.appengine.api import search ndx = search.Index(DOCUMENT_INDEX) loc = (-33.857, 151.215) query = "distance(location, geopoint(-33.857, 151.215)) < 4500" loc_expr = "distance(location, geopoint(-33.857, 151.215))" sortexpr = search.SortExpression( expression=loc_expr, direction=search.SortExpression.ASCENDING, default_value=4501) search_query = search.Query( query_string=query, options=search.QueryOptions( sort_options=search.SortOptions(expressions=[sortexpr]))) results = index.search(search_query) 
+7
source

Source: https://habr.com/ru/post/915814/


All Articles