Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Project: Google map
#1
The point of the project is to type any address and after "enter" to automatically be redirected to google map with that location on the map. When I run this code it writes the address in search box in google maps but there is no change on the map, it still gives me my own location. Not sure why is this so, any idea is appreciated.
#! python3
# mapIt1.py - Launches a map in the browser using an address from the
# command line or clipboard.

import webbrowser, sys, pyperclip

def my_func(get_add):
    if len(client) > 1:
	    # Get address from command line
	    address = ' '.join(get_add)
    else:
	    # Get address from clipboard
	    address = pyperclip.paste()

    webbrowser.open('https://www.google.com/maps/place/' + address)
	
def test_it(args):
	print(args)
	my_func(args)

if __name__ == '__main__':
	client = input("Hi! Please add any address you want to see on map: ")
	test_it(client)
	
Reply
#2
I tested just the address itself and I used Nashville Tennessee as my test. When I put in just Nashville, it gives me my location. However when I put in Nashville TN, it showed to map at Nashville Tennessee. Hopefully this helps somewhat! :)
Reply
#3
I would suggest you take a look at the documentation for the pyperclip.paste() function. You are going to want to check and see how its formatting strings. For instance, if I go to google maps and type NewYork the URL is:

.../maps/place/New+York,+NY and for Cali:

.../maps/place/California

Now, using your code:
#! python3
# mapIt1.py - Launches a map in the browser using an address from the
# command line or clipboard.

import webbrowser, sys, pyperclip

def my_func(get_add):
    if len(client) > 1:
        # Get address from command line
        address = ' '.join(get_add)
    else:
        # Get address from clipboard
        address = pyperclip.paste()

    print(address)
    webbrowser.open('https://www.google.com/maps/place/' + address)

def test_it(args):
    print(args)
    my_func(args)

if __name__ == '__main__':
    client = input("Hi! Please add any address you want to see on map: ")
    test_it(client)
I get:

Hi! Please add any address you want to see on map: NewYork
NewYork
N e w Y o r k

Process finished with exit code 0
and for Cali:

Hi! Please add any address you want to see on map: California
California
C a l i f o r n i a

Process finished with exit code 0
As you can see google maps is not recognizing C a l i f o r n i a as a place.
Reply
#4
(Jul-31-2018, 06:39 AM)Zombie_Programming Wrote: I tested just the address itself and I used Nashville Tennessee as my test. When I put in just Nashville, it gives me my location. However when I put in Nashville TN, it showed to map at Nashville Tennessee. Hopefully this helps somewhat! :)

I sent my program to three friends and they all said that it didn't work. They added full addresses. On my computer adding address with a name of the city ( we are not all from USA and don't have states! ) also didn't bring anything. Now I tried with adding country too and nothing...

Now checked Nashville TN and it shows it on the map but when I write London UK again nothing.

Vysero, not sure that I understand you. I get the same outcome whether I add NewYork, New York or California. In each case program doesn't bring me to that location.
Reply
#5
(Jul-31-2018, 09:52 PM)Truman Wrote: Vysero, not sure that I understand you. I get the same outcome whether I add NewYork, New York or California. In each case program doesn't bring me to that location.

I was trying to point out that when you enter some location say NewYork. In your mind, the url becomes:
https://www.google.com/maps/place/NewYork

but in reality it becomes:
https://www.google.com/maps/place/N e w Y o r k

which is not what google maps wants. The reason it becomes N e w Y o r k rather than NewYork is a string formating issue.
Reply
#6
I made a little change in line 10 and it finally works. Check it yourself.

#! python3
# mapIt1.py - Launches a map in the browser using an address from the
# command line or clipboard.

import webbrowser, sys, pyperclip

def my_func(get_add):
    if len(client) > 1:
	    # Get address from command line
	    address = ' '.join(get_add.split('+'))
    else:
	    # Get address from clipboard
	    address = pyperclip.paste()

    webbrowser.open('https://www.google.com/maps/place/' + address)
	
def test_it(args):
	print(args)
	my_func(args)

if __name__ == '__main__':
	client = input("Hi! Please add any address you want to see on map: ")
	test_it(client)
	
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020