def city_by_latlng(request): """ Receive GET lat/lng and return localized info on nearest city. The client sends values from the HTML5 geolocation API: longitude and latitude. Find the city closest to the location and return its data in the language set in the LANGUAGE setting. """ try: lat = float(request.GET.get('latitude', None)) lng = float(request.GET.get('longitude', None)) except TypeError: return HttpResponseBadRequest() try: city = City.by_latlng(lat, lng) except City.DoesNotExist: raise Http404 try: an = AltName.objects.get(geoname_id=city.pk, type=3, is_main=True, language=settings.LANGUAGE_CODE) except AltName.DoesNotExist: raise Http404 return HttpResponse(json.dumps({ "id": city.id, "lat": city.lat, "lng": city.lng, "region": city.region.pk, "country": city.country.pk, "population": city.population, "slug": an.slug, "name": an.name, "crc": an.crc, "url": an.url, }), content_type="application/json")
def city_by_latlng(request): """The client sends values from the HTML5 geolocation API: accuracy, longitude, latitude. Find the city closest to the location and return its data. """ try: lat = float(request.GET.get('latitude', None)) lng = float(request.GET.get('longitude', None)) except TypeError: return HttpResponseBadRequest() city = City.by_latlng(lat, lng) data = {"id": city.pk, "lat": city.lat, "lng": city.lng, "population": city.population, "country": city.country.pk, "crc": city.get_crc(), } return HttpResponse(json.dumps(data), content_type="application/json")