1

I want to extract Latitude and Longitude for a city from Google Maps. The flow should go like this

opening google maps URL Search for a city say 'New York'

Now I want to fetch the Latitude and Longitude of this city. Any help would be appreciated. Thanx in advance.

  • Where is latitude and longitude mention in that maps ? – cruisepandey Jun 05 '21 at 15:18
  • If you do a right-click into the map, you'll see long and lat. To do a click maybe this helps: https://stackoverflow.com/questions/16807258/selenium-click-at-certain-position – patrickgerard Jun 05 '21 at 15:21
  • You can get lat/lon from name of city using [Python Geocoders module](https://stackoverflow.com/questions/13686001/python-module-for-getting-latitude-and-longitude-from-the-name-of-a-us-city) – DarrylG Jun 05 '21 at 15:21
  • @cruisepandey that's the point. They aren't visible straight away. When you right-click a location, you can see the coordinates. – crushed_bug Jun 05 '21 at 15:46
  • @DarrylG is there a way to do the same using selenium webdriver module? – crushed_bug Jun 05 '21 at 15:47
  • @crushed_bug : Yeah I figured that out, check out the below solution – cruisepandey Jun 05 '21 at 15:50

2 Answers2

1

I could do it with context_click in selenium.

wait = WebDriverWait(driver, 10)
driver.get("https://www.google.com/maps")
wait.until(EC.element_to_be_clickable((By.ID, "searchboxinput"))).send_keys("New York")
wait.until(EC.element_to_be_clickable((By.ID, "searchbox-searchbutton"))).click()
sleep(5)
ActionChains(driver).move_to_element(driver.find_element_by_xpath("//html/body")).context_click().perform()
print(wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "ul[role='menu']>li div div[class*='text']:nth-of-type(1)"))).text)

O/P:

40.69715, -74.25987

Don't forget to import these :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • 1
    Thank You, brother. That certainly solves my problem. Your URL suggestion was good too. I will try to work on that one as well to have a better understanding. – crushed_bug Jun 05 '21 at 20:55
0

After exploring google maps and how a regular person would find the longitude and latitude i noticed the URL has the longitude and latitude within it so i would search for the location then pull the URL using driver.current_url or something like that(not familiar with python) then set it to a variable and use what ever function to trim it down and separate it so you only have the longitude and latitude in 2 separate variables. sorry this only breaks down the process rather then giving you a straight answer, if you have any other questions I would prefer you ask me here. https://discord.gg/fjNX5MuTVq