コード例 #1
0
def api_places_near(request, lat, lon, accuracy=100):
    """
        Returns the places near to the location
        
        :param lat: Latitude
        :type username: float
        :param lon: Longitude 
        :type password: float
        :param accuracy: max distance requested (in meters)
        :type accuracy: integer
        
        
        :return:
            >>> places_near(37,-3,500)
                Requesting ->
                    {"id":"jsonrpc", "params":[37, -3, 500], "method":"places_near", "jsonrpc":"1.0"}
                Got ->
                    {"error": null, "jsonrpc": "1.0", "id": "jsonrpc", "result": {"c644dcd3f743006f34c687255fae5eb02f5de004": {"google_places_reference": "CnRmAAAAzEJrzob6u9wO3cZhZH7frAEDQxNtUWFgSrINsuBsM6Pc8ECe4zm0yjfywFNuG2yH5cbYFIQikOlmFPGA0YKN9RrPMghlmLTDNl_wpHqs6-AR_aRLPmyj-DanAuOUJ8F9_y61Rvhj8CFHMOCX6RnuCRIQxKtyARYpHWK784L_vV_Z6hoUudfADvedbL-zgjEFbVmXSnDTDz0", "name": "Nevada", "location": {"lat": 37.0, "lon": -3.0166667}, "address": "Nevada", "types": ["locality", "political"], "icon": "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png"}}}        
    """
    gp = GPRequest()
    places = gp.do_search(lat, lon, radius=accuracy)
    if places['status'] == 'OK':
    #places = Place.objects.nearest_to(lat=lat, lon=lon, accuracy=accuracy)
        i = iter(places['results'])
        places_to_return = {}
        for place in i:
            places_to_return.setdefault(place['id'], {
                                                      'name' : place['name'],
                                                      'address': place.get('vicinity'),
                                                      'location': {
                                                                   'lat': place['geometry']['location']['lat'],
                                                                   'lon': place['geometry']['location']['lng'],
                                                                   },
                                                      'types': place['types'],
                                                      'google_places_reference': place['reference'],
                                                      'icon': place['icon'],
                                                      })
        #p = Place.objects.filter(google_places_id__in=places_to_return.keys()).values_list('google_places_id', 'pk')
        return places_to_return if places_to_return else {}
    return {}
コード例 #2
0
    def create_from_google(self, **kwargs):
        if 'google_places_id' in kwargs:
            place = Place.objects.filter(google_places_id = kwargs['google_places_id'])
            if place is not None:
                return place
        from libs.mapsServices.places import GPRequest
        client = GPRequest()
        search = client.retrieve_reference(kwargs['google_places_reference'])
        
        location= Point(search['result']['geometry']['location']['lng'],
                        search['result']['geometry']['location']['lat'])
        try:
            city = client._get_city(search['result'].get('address_components'))
            region = client._get_region(search['result'].get('address_components'))
            if region is None:
                region = city
            country = client._get_country(search['result'].get('address_components'))
            city_obj = City.objects.get(name__iexact = city, region__country__code=country['code'])
        except City.DoesNotExist:
            try:
                if region is not None:
                    region_obj = Region.objects.get(name__iexact = region['name'], country__code=country['code'])
                    city_obj = City.objects.create(name=city, 
                                                region=region_obj,
                                                population = 1)
                else:
                    region_obj = Region.objects.get(name = '', country__code=country['code'])
                    city_obj = City.objects.create(name=city, 
                                                region=region_obj,
                                                population = 1)
            except Region.DoesNotExist:
                try: 
                    country_obj = Country.objects.get(code = country['code'])
                    if region is not None:
                        region_obj = Region.objects.create(name = region['name'], 
                                                        country = country_obj)
                    else:
                        region_obj = Region.objects.create(name = '', 
                                                        country = country_obj)
                    city_obj = City.objects.create(name=city, 
                                                region=region_obj,
                                                population = 1)
                except Country.DoesNotExist:
                    country_obj = Country.objects.create(name=country['name'],
                                                        code=country['code'],
                                                        tld=country['code'],
                                                        population=1)
                    region_obj = Region.objects.create(name = region.get('name', ''),
                                                    code=region.get('code', ''), 
                                                    country = country_obj)
                    city_obj = City.objects.create(name=city, 
                                                region=region_obj,
                                                population = 1)
        try:
            place = Place.objects.get(
                                    google_places_id = search['result']['id']
                                    )
            place.name=search['result']['name']
            place.location=location
            place.adress=search['result'].get('formatted_address')
            place.city= city_obj
            place.google_places_reference=search['result']['reference']
            place.google_places_id=search['result']['id']
            place.save()
        except Place.DoesNotExist:
            place = Place.objects.create(name=search['result']['name'],
                            location = location,
                            address = search['result'].get('formatted_address'),
                            city = city_obj,
                            google_places_reference = search['result']['reference'],
                            google_places_id = search['result']['id'],
                            user = kwargs['user']
                             )
            place.tags.add(*search['result']['types'])

        return place