I want to find the distance between two location using google API. I want output to be look like - "The distance between location 1 and location 2 is 500 miles ( distance here is example purposes )", but how can i get the desired output as the current program is showing various output ( which i cant use to get he desired output ) . can you guys please show me the way or show me what is the exact procedure to do it?
import urllib
import json
serviceurl = 'http://maps.googleapis.com/maps/api/geocode/json?'
while True:
address = raw_input('Enter location: ')
if len(address) < 1 : break
url = serviceurl + urllib.urlencode({'sensor':'false', 'address': address})
print 'Retrieving', url
uh = urllib.urlopen(url)
data = uh.read()
print 'Retrieved',len(data),'characters'
try: js = json.loads(str(data))
except: js = None
if 'status' not in js or js['status'] != 'OK':
print '==== Failure To Retrieve ===='
print data
continue
print json.dumps(js, indent=4)
lat = js["results"][0]["geometry"]["location"]["lat"]
lng = js["results"][0]["geometry"]["location"]["lng"]
print 'lat',lat,'lng',lng
location = js['results'][0]['formatted_address']
print location
Google has a specific api for that, it's called Google Maps Distance Matrix API.
Distance & duration for multiple destinations and transport modes.
Retrieve duration and distance values based on the recommended route
between start and end points.
If you just need the distance between two points on the globe you may want to use the Haversine formula
If you know lat and lon, use the geopy package:
In [1]: from geopy.distance import great_circle
In [2]: newport_ri = (41.49008, -71.312796)
In [3]: cleveland_oh = (41.499498, -81.695391)
In [4]: great_circle(newport_ri, cleveland_oh).kilometers
Out[4]: 864.4567616296598
Related
I have written this code addressing to Geolocator API of Google Maps and it is not functional. Note that as an entry level devoloper I do this through Google collabs.
`import requests
import smtplib
import pandas as pd
file_path = "C:/Users/30697/Downloads/addresses.xlsx"
api_key = "MED_316KQ_4XqvYLSYa2k="
lat1 = 38.1579862
lng1 = 23.9626608
lat2 = 38.1579862
lng2 = 23.9626608
lat3 = 38.1540804
lng3 = 23.9595323
lat4 = 38.1540804
lng4 = 23.9595323
def save_addresses_to_excel(addresses, file_path):
# Create a DataFrame with the addresses
df = pd.DataFrame({"Address": addresses})
# Write the DataFrame to an Excel file
df.to_excel(file_path, index=False)
def get_addresses_in_region(api_key, lat1, lng1, lat2, lng2, lat3, lng3, lat4, lng4):
# Define the bounds of the region
bounds = f"{lat1},{lng1}|{lat2},{lng2}|{lat3},{lng3}|{lat4},{lng4}"
# Make the API request
response = requests.get(f"https://maps.googleapis.com/maps/api/geocode/json?bounds={bounds}&key={api_key}")
# Check if the request was successful
if response.status_code == 200:
# Parse the response JSON
data = response.json()
# Extract the addresses from the response
addresses = [result["formatted_address"] for result in data["results"]]
return addresses
else:
# Return an error message
return "Error: Could not retrieve addresses."
I expect a series of addresses to be displayed as a result.
According to the documentation, the bounds parameter is an optional addition to the request, which helps to refine the results. You must also specify address, components, latlng or place_id. Assuming you're trying to do reverse geocoding, you probably need to specify a single coordinate using latlng.
I'm using Google API to obtain the json data of nearby coffee outlets. To do this, I need to encode the latitude and longitude into the URL.
The required URL: https://maps.googleapis.com/maps/api/place/textsearch/json?query=coffee&location=22.303940,114.170372&radius=1000&maxprice=3&key=myAPIKey
The URL i'm obtaining using urlencode: https://maps.googleapis.com/maps/api/place/textsearch/json?query=coffee&location=22.303940%2C114.170372&radius=1000&maxprice=3&key=myAPIKEY
How can I remove the "%2C" in the URL? (I have shown my code below)
serviceurl_placesearch = 'https://maps.googleapis.com/maps/api/place/textsearch/json?'
parameters = dict()
query = input('What are you searching for?')
parameters['query'] = query
parameters['location'] = "22.303940,114.170372"
while True:
radius = input('Enter radius of search in meters: ')
try:
radius = int(radius)
parameters['radius'] = radius
break
except:
print('Please enter number for radius')
while True:
maxprice = input('Enter the maximum price level you are looking for(0 to 4): ')
try:
maxprice = int(maxprice)
parameters['maxprice'] = maxprice
break
except:
print('Valid inputs are 0,1,2,3,4')
parameters['key'] = API_key
url = serviceurl_placesearch + urllib.parse.urlencode(parameters)
I added this piece of code in to make the URL work however I don't think this is a long term solution. I'm looking for a more long term solution.
urlparts = url.split('%2C')
url = ','.join(urlparts)
You can add safe=","
import urllib.parse
parameters = {'location': "22.303940,114.170372"}
urllib.parse.urlencode(parameters, safe=',')
Result
location=22.303940,114.170372
The following code doesn't produce any output when I run python findArestaurant.py in the terminal
It's supposed to produce an output that lists restaurants in places around the world.
I signed up for a Foursqure dev account, created an app, and was then able to copy both the client id, and client secret. However, no output?
filename: findArestaurant.py
from geocode import getGeocodeLocation
import json
import httplib2
import sys
import codecs
sys.stdout = codecs.getwriter('utf8')(sys.stdout)
sys.stderr = codecs.getwriter('utf8')(sys.stderr)
foursquare_client_id = "entered_here"
foursquare_client_secret = "entered_here"
def findARestaurant(mealType,location):
#1. Use getGeocodeLocation to get the latitude and longitude coordinates of the location string.
latitude, longitude = getGeocodeLocation(location)
#2. Use foursquare API to find a nearby restaurant with the latitude, longitude, and mealType strings.
#HINT: format for url will be something like https://api.foursquare.com/v2/venues/search?client_id=CLIENT_ID&client_secret=CLIENT_SECRET&v=20130815&ll=40.7,-74&query=sushi
url = ('https://api.foursquare.com/v2/venues/search?client_id=%s&client_secret=%s&v=20130815&ll=%s,%s&query=%s' % (foursquare_client_id, foursquare_client_secret,latitude,longitude,mealType))
h = httplib2.Http()
result = json.loads(h.request(url,'GET')[1])
if result['response']['venues']:
#3. Grab the first restaurant
restaurant = result['response']['venues'][0]
venue_id = restaurant['id']
restaurant_name = restaurant['name']
restaurant_address = restaurant['location']['formattedAddress']
address = ""
for i in restaurant_address:
address += i + " "
restaurant_address = address
#4. Get a 300x300 picture of the restaurant using the venue_id (you can change this by altering the 300x300 value in the URL or replacing it with 'orginal' to get the original picture
url = ('https://api.foursquare.com/v2/venues/%s/photos?client_id=%s&v=20150603&client_secret=%s' % ((venue_id,foursquare_client_id,foursquare_client_secret)))
result = json.loads(h.request(url, 'GET')[1])
#5. Grab the first image
if result['response']['photos']['items']:
firstpic = result['response']['photos']['items'][0]
prefix = firstpic['prefix']
suffix = firstpic['suffix']
imageURL = prefix + "300x300" + suffix
else:
#6. if no image available, insert default image url
imageURL = "http://pixabay.com/get/8926af5eb597ca51ca4c/1433440765/cheeseburger-34314_1280.png?direct"
#7. return a dictionary containing the restaurant name, address, and image url
restaurantInfo = {'name':restaurant_name, 'address':restaurant_address, 'image':imageURL}
print ("Restaurant Name: %s" % restaurantInfo['name'] )
print ("Restaurant Address: %s" % restaurantInfo['address'] )
print ("Image: %s \n" % restaurantInfo['image'] )
return restaurantInfo
else:
print ( "No Restaurants Found for %s" % location )
return "No Restaurants Found"
if __name__ == '__main__':
findARestaurant("Pizza", "Tokyo, Japan")
findARestaurant("Tacos", "Jakarta, Indonesia")
findARestaurant("Tapas", "Maputo, Mozambique")
findARestaurant("Falafel", "Cairo, Egypt")
findARestaurant("Spaghetti", "New Delhi, India")
findARestaurant("Cappuccino", "Geneva, Switzerland")
findARestaurant("Sushi", "Los Angeles, California")
findARestaurant("Steak", "La Paz, Bolivia")
findARestaurant("Gyros", "Sydney, Australia")
I'm trying to scrape all of the French map.
I've got one issue :
1 - I'm limited by the zoom of the map
import requests
url ='https://www.iadfrance.fr/agent-search-location?southwestlat=47.0270782&southwestlng=-2.1560669&northeastlat=47.4930807&northeastlng=-1.0093689'
jsonObj = requests.get(url).json()
emails = jsonObj['agents']
#print (emails)
for agent in emails:
email = agent['email']
print(email)
Thank you
You'll have to utilize the longitude, latitude parameters in the request to "zoom out"
You can either change them manually, or I'm a fan of osmnx. You can use that to get the boundaries of different areas, then set a radius in meters to create your boundary box:
import requests
import osmnx as ox
import os
os.environ["PROJ_LIB"] = "C:/Users/xxxxxxx/AppData/Local/Continuum/anaconda3/Library/share"; #fixr
# Get a boundary box of a city/place/address
city = ox.gdf_from_place('Paris, France')
# Distance to make boundary from center in meters
# Essentially allows you to zoom out
distance = 300000
# Get centroid of that city/place boundary box
point = ( city['geometry'].centroid.x.iloc[0], city['geometry'].centroid.y.iloc[0] )
# Get a new boundary box a certain distance in North, South, East, West directions for x meters
boundary = ox.bbox_from_point(point, distance=distance , project_utm=False, return_crs=False)
sw_lat = boundary[3]
sw_lng = boundary[0]*-1
ne_lat = boundary[2]
ne_lng = boundary[1]*-1
# website to scrape https://www.iadfrance.fr/trouver-un-conseiller
url ='https://www.iadfrance.fr/agent-search-location'
# Here is the coordinates from orginial post
#payload = {
#'southwestlat': '47.0270782',
#'southwestlng': '-2.1560669',
#'northeastlat': '47.4930807',
#'northeastlng': '-1.0093689'}
payload = {
'southwestlat': sw_lat,
'southwestlng': sw_lng,
'northeastlat': ne_lat,
'northeastlng': ne_lng}
jsonObj = requests.get(url, params=payload).json()
emails = jsonObj['agents']
#print (emails)
for agent in emails:
email = agent['email']
print(email)
I find the right way, I must think out the box.
I've set 2 geographic data manually in a very very large area. ( one in Atlantic and an other in Russia ).
It works !
import requests
url ='https://www.iadfrance.fr/agent-search-location?southwestlat=9.884462&southwestlng=-35.58398&northeastlat=68.714264&northeastlng=44.796407'
jsonObj = requests.get(url).json()
emails = jsonObj['agents']
#print (emails)
for agent in emails:
email = agent['email']
print(email)
I cannot see anything in their API to do this:
https://docs.djangoproject.com/en/dev/ref/contrib/gis/geoip/#geoip-api
Or should I just use Google API for Reverse Geocoding?
Solution - call this URL and parse it's JSON.
http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false
Use geopy, it can handle multiple geocoders including googlev3.
from geopy.geocoders import GoogleV3
geolocator = GoogleV3()
location = geolocator.reverse("52.509669, 13.376294")
print(location.address)
>>> Potsdamer Platz, Mitte, Berlin, 10117, Deutschland, European Union
install with pip:
pip install geopy
infos found on: https://github.com/geopy/geopy
You can use maps API. I've included a snippet which I use to calculate marathon start points converted into a PointField using Postgis with Django. This should set you on your way.
import requests
def geocode(data):
url_list = []
for item in data:
address = ('%s+%s' % (item.city, item.country)).replace(' ', '+')
url = 'http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false' % address
url_list.append([item.pk, url])
json_results = []
for url in url_list:
r = requests.get(url[1])
json_results.append([url[0], r.json])
result_list = []
for result in json_results:
if result[1]['status'] == 'OK':
lat = float(result[1]['results'][0]['geometry']['location']['lat'])
lng = float(result[1]['results'][0]['geometry']['location']['lng'])
marathon = Marathon.objects.get(pk=result[0])
marathon.point = GEOSGeometry('POINT(%s %s)' % (lng, lat))
marathon.save()
return result_list
#rawsix answer seems smart for a django user.
Note however that the location returned by geolocator.reverse(query) is a list and not a Location object; so attempting to retrieve attribute address from it would result in an error.
Usually, the first item in that list has the closest address information. so u can simply do:
location = location[0]
address = location.address
Additionally, instead of passing a string latitude and longitude to the reverse method, you can use a tuple and it must be the latitude before the longitude. You can do:
from geopy.geocoders import GoogleV3()
geocoder = GoogleV3()
location_list = geocoder.reverse((latitude, longitude))
location = location_list[0]
address = location.address