As I happened to fix VBA in Excel of the data processing team at work, I was asked, "Mr. atsukinov, have you ever used API in VBA?" "Well, I've never used it. What's wrong?" "I'm using the Google Map API to look up an address from latitude and longitude in VBA, but it doesn't work very well ..." "How" That's why I was shown the code screen *** (I don't understand at all) *** That was an honest impression. I've only used the flickr API for downloading images according to the tutorial, but I'm not using the API in the first place! !! "By the way, how much do you process that data?" *** "80,000 lines" *** Oh... You'll definitely want to use APIs and other features like that. But honestly, I thought it would be easier to handle it with Python if I used the API. "For now, I'll try to find out if I can do anything with Python." That said, I decided to look into the Google Maps API first.
That's why I tried to google immediately, but apparently it turned out that it is "reverse-Geocoding" to detect the address from "latitude / longitude". Searching for an address, latitude / longitude from a property name or name seems to be ordinary "geocoding". Unfortunately, there weren't many articles that hit this "reverse geocoding," and I couldn't find the code I was looking for. *** I'm tired of seeing regular geocoding articles! !! *** *** Furthermore, even if a code similar to that of reverse geocoding was on board, the problem was that the address obtained was written in English, that is, in the form of address → prefecture, and was written in alphabets. For example, if you look at "Tokyo Tower" ・ 4-2-8 Shibakoen, Minato-ku, Tokyo 105-0011 Is not displayed ・ 4-2-8, Shiba Park, Minatoku, Tokiyo It will be written like this. This would be a problem for the next person to read the address, so I managed to find a way to write it in Japanese. *** And found ***
I will post the code below, so I hope you find it helpful. [Easy-to-understand explanation (Geocoder / Googlemaps)](https://qiita.com/yoshi_yast/items/bb75d8fceb712f1f49d1#%E3%82%84%E3%81%A3%E3%81% 9F% E3% 81% 93% E3% 81% A8% E3% 82% B3% E3% 83% BC% E3% 83% 89)
import googlemaps
#Store the API key obtained for GoogleMapsAPI in a variable
Key = XXXXX
#Returns API key for Google Maps API
gmaps = googlemaps.Client(key=Key)
#Store some latitude / longitude in the list
list = [
"35.65858645, 139.745440057962", #Tokyo Tower
"35.71005425, 139.810714099926" #Tokyo Sky Tree
]
#Search for addresses in order from the list
for i in list:
results = gmaps.reverse_geocode((i), language='ja')
add = [d.get('formatted_address') for d in results]
print(add[1])
#=>Japan, 〒105-0011 4-2-8 Shibakoen, Minato-ku, Tokyo
#=>Japan, 〒131-0045 1-1-83 Oshiage, Sumida-ku, Tokyo
The problem is that, as it is, there is a slight extra character "Japan," at the beginning of the sentence. I'll modify the code a bit so that I can write this in a slice from the middle.
for i in list:
results = gmaps.reverse_geocode((i), language='ja')
add = [d.get('formatted_address') for d in results]
Jusho = add[1]
print(Jusho[3:])
#=>〒105-0011 4-2-8 Shibakoen, Minato-ku, Tokyo
#=>〒131-0045 1-1-83 Oshiage, Sumida-ku, Tokyo
Yeah, it's a great work. It is the shape that I was looking for. However, the problem seems to be that the Google Maps API can access up to 40,000 free APIs a month (source. = ja))) Well, there may not be many heavy users who exceed that, but if you exceed it, you will be charged, so keep in mind.
Recommended Posts