LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-21-2011, 12:00 PM   #1
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Launch Firefox, get Google Map display


Ubuntu 10.04, Firefox 3.6.24

I have a Pipe of commands which displays a single street address.
I then...
- copy the address to the clipboard
- launch Firefox
- go to Google Maps
- paste the address into the search window,
- press ENTER
- view a map of the neighborhood

Is there a way to automate this multi-step process?

Daniel B. Martin
 
Old 12-21-2011, 12:43 PM   #2
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 490
Blog Entries: 4

Rep: Reputation: 238Reputation: 238Reputation: 238
If the street address data is on a web page that you control, or can access via a server-side script like php, it should be possible to write a php script to do what you describe.

If the street address data is from your local drive, you may want to use Konqueror, not Firefox, since Konqueror has the ability to access local data.
 
Old 12-21-2011, 01:52 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Simply running "firefox" with an url as the argument will usually make it open up that location, and generally if there's an already-running instance it will open up in that instead of starting a new one. Use the -new-tab or -new-window to control how it opens them up.

The -remote option also allows you to pass various other commands to a running instance:

http://www-archive.mozilla.org/unix/remote.html
(Quite an old page though; you might search around for newer info.)

Conversely the -no-remote option forces it to ignore any currently-running instances and force a new one.

So just figure out the url you need to use to call the search, and you're set to go.

I use a variation like this in a small script I have, just to cover all bases:

Code:
url='http://www.google.com/etc/etc/etc'
firefox -remote "openURL($url,new-tab)" || firefox -new-tab "$url"

Last edited by David the H.; 12-21-2011 at 01:53 PM.
 
1 members found this post helpful.
Old 12-21-2011, 01:57 PM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Store pipe output as $parameter variable, arrange it so it follow google map parameter
http://code.google.com/apis/maps/doc...#quick_example

Then use command: firefox http://maps.googleapis.com/maps/api/staticmap?$parameter

eg:
Code:
address="North Carolina"
#replace spaces with + chars
address=$(echo $address | tr ' ' '+')
parameter="&zoom=8&size=680x680&sensor=false"
url="http://maps.googleapis.com/maps/api/staticmap?center="

firefox "$url$address$parameter"
(query limited to 1000 per day)

Last edited by Cedrik; 12-22-2011 at 04:46 AM.
 
1 members found this post helpful.
Old 12-21-2011, 08:33 PM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Original Poster
Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by Cedrik View Post
Code:
address="North Carolina"
#replace spaces with + chars
address=$(echo $address | tr ' ' '+')
parameter="&zoom=8+&size=680x680&sensor=false"
url="http://maps.googleapis.com/maps/api/staticmap?center="

firefox "$url$address$parameter"
Thank you, Cedrik, for this promising code segment. It gets me to Google but not to the desired map. This command was issued:
Code:
firefox http://maps.googleapis.com/maps/api/staticmap?center=3300+MORNINGSIDE+DR+27607&zoom=8+&size=680x680&sensor=false
and it took me to Firefox and to Google ...but... to a Google error message reading:
Code:
400. That’s an error. 

Your client has issued a malformed or illegal request. That’s all we know.
The address window (if that's the proper name) contains most but not all of the issued command. It has:
Code:
http://maps.googleapis.com/maps/api/staticmap?center=3300+MORNINGSIDE+DR+27607
Using Google to "sniff around" turns up this:
Quote:
In order to use the Google APIs, you must import them using the Google API loader in conjunction with the API key. The loader allows you to easily import one or more APIs, and specify additional settings (such as language, location, API version, etc.) applicable to your needs.
Does that mean my request was illegal because I lack an API key?

Daniel B. Martin
 
Old 12-22-2011, 04:45 AM   #6
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
You have error in url, there is a '+' after the zoom=8 parameter...

Without it, the map loads fine

Edit:
Sorry, this error was in the code I posted above
I edited it

You can play with zoom value, also you can add marker for the location
In this case replace 'center' with 'markers'

eg:
Code:
address="3300 MORNINGSIDE DR 27607"

#replace spaces with + chars
address=$(echo $address | tr ' ' '+')

# zoom value
zoom=12

# map type
# can be: roadmap, satellite, terrain, hybrid
maptype=roadmap

parameter="&zoom=$zoom&size=680x680&maptype=$maptype&sensor=false"
url="http://maps.googleapis.com/maps/api/staticmap?markers=color:red|"

firefox "$url$address$parameter"
try maptype=satellite and zoom=20

Last edited by Cedrik; 12-22-2011 at 05:07 AM.
 
Old 12-22-2011, 09:33 AM   #7
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Original Poster
Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by Cedrik View Post
Code:
address="3300 MORNINGSIDE DR 27607"

#replace spaces with + chars
address=$(echo $address | tr ' ' '+')

# zoom value
zoom=12

# map type
# can be: roadmap, satellite, terrain, hybrid
maptype=roadmap

parameter="&zoom=$zoom&size=680x680&maptype=$maptype&sensor=false"
url="http://maps.googleapis.com/maps/api/staticmap?markers=color:red|"

firefox "$url$address$parameter"
Thank you for the update and additional information. I'm still getting the Error 400 as cited in a previous post. The command issued is this:
Code:
firefox http://maps.googleapis.com/maps/api/staticmap?markers=color:red|3300+MORNINGSIDE+DR+27607&zoom=12&size=680x680&maptype=roadmap&sensor=false
Please take another look.

Daniel B. Martin
 
Old 12-22-2011, 10:03 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
That url is working just fine for me. Could you show us the exact commands you used? Make sure you're quoting the url first if you're trying to launch it directly.

Using the link and code posted above as inspiration, I played around a bit and came up with this script:

Code:
#!/bin/bash
#
# Search for a single input address in google maps, and load the result in Firefox.
# It only handles address searches, not latitude/longitude parameters.
# You can also set optional paramters in the script
# Details on the api use can be found here:
# http://code.google.com/apis/maps/documentation/staticmaps/

# The base url to use
baseurl="http://maps.googleapis.com/maps/api/staticmap"

# Required input parameters
[[ -z "$1" ]] && echo "Error: Must supply an input address, surrounded by quotes." && exit 1
center="$1"			# Example input: "3300 MORNINGSIDE DR 27607"
center=${center//[[:space:]]/+}	# Replace all whitespace with + chars in address
zoom="14"
size="680x680"
sensor="false"

# Optional parameters; uncomment and set as desired
# Only include the values to send; the labels will be added later
scale="2"			# Accepted values 1 or 2
#format=""			# Accepted values png8/png,png32,gif,jpg,jpg-baseline
maptype="roadmap"		# Map type can be: roadmap, satellite, terrain, hybrid
#language=""
#style=""
markers="color:red|$center"	# See note below
#path=""
#visible=""

# For "markers", the location(s) supplied must be within the visible portion of the map,
# or else the "center" value must be unset. "path" and "visible" may also affect the
# center value (and are not currently accounted for).  See the api.
[[ -n $markers ]] && unset center

# Use an array to assemble all parameters except baseurl
# The expansion pattern ${variable:+pattern} means to only
# use the pattern if the variable exists
# Do not quote optional parameters, or else empty values will appear in the list
parameters=(
	${center:+center=$center}
	${zoom:+zoom=$zoom}
	${size:+size=$size}
	${sensor:+sensor=$sensor}
 	${scale:+scale=$scale}
	${format:+format=$format}
	${maptype:+maptype=$maptype}
	${language:+language=$language}
	${markers:+markers=$markers}
	${path:+path=$path}
	${visible:+visible=$visible}
	${style:+style=$style}
)


# build final search string.  Expanding an array with[*] prints
# each element separated by the first character in $IFS

IFS="&"
searchstring="$baseurl?${parameters[*]}"

# Launch the final command
firefox "$searchstring"

exit
It seems to work well enough in my testing. The comments should explain everything. I tried to account for all the possible options as listed in the api, but I didn't bother looking deeply into the markers, path, style, and visible options. So it may not properly handle all values.

As noted, I also didn't bother with the lat/long notation. That would require testing the input value for the kind of input.

Finally, if you plan on modifying the parameters a lot, it might be smart to set them up in a separate configuration file and source that instead, so you don't have to keep modifying the original script.

Last edited by David the H.; 12-22-2011 at 10:04 AM.
 
1 members found this post helpful.
Old 12-22-2011, 11:09 AM   #9
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by danielbmartin View Post
Thank you for the update and additional information. I'm still getting the Error 400 as cited in a previous post. The command issued is this:
Code:
firefox http://maps.googleapis.com/maps/api/staticmap?markers=color:red|3300+MORNINGSIDE+DR+27607&zoom=12&size=680x680&maptype=roadmap&sensor=false
Please take another look.

Daniel B. Martin
Tell me you don't type 'firefox http://...' in firefox url adress bar

You used the script verbatim ? like copy and pasted it in a file, then executed it with: sh file
The only thing I can think is if the url is typed in a shell with no quotes, then shell will stop it at the first & char (or at the | char)

Nice script David the H.!
(Although it could be nicer with command line options using getopts etc )

Last edited by Cedrik; 12-22-2011 at 11:14 AM.
 
Old 12-22-2011, 11:35 AM   #10
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Original Poster
Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by David the H. View Post
Code:
# Details on the api use can be found here:
# http://code.google.com/apis/maps/documentation/staticmaps/
Thank you for this pointer. That page presents a Quick Example which presents a map of New York City, centered on the Brooklyn Bridge. I did a copy-and-paste into a test program... and it fails the same way! This is the command:
Code:
firefox http://maps.googleapis.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY&zoom=14&size=512x512&maptype=roadmap&markers=color:blue%7Clabel:S%7C40.702147,-74.015794&markers=color:green%7Clabel:G%7C40.711614,-74.012318&markers=color:red%7Ccolor:red%7Clabel:C%7C40.718217,-73.998284&sensor=false
The result is the same -- it opens a new tab in an existing multi-tab Firefox window and that tab contains the Google 400 error message.

This is a good clue!

As mentioned in other threads which you (David the H.) have read, I have a slew of self-written REXX programs which I'm improving by adding Linux code. So my shell is a REXX program run by the Regina interpreter. This is the program, qe.rex, in its entirety.

Code:
/*  Daniel B. Martin   QE = Quick Example

    To execute this program open a Terminal session and enter:
    regina /home/daniel/Desktop/RexxScripts/qe.rex

    Test a "Quick Example" as given in 
    http://code.google.com/apis/maps/documentation/staticmaps/#quick_example
*/

cmd = "firefox" ,
"http://maps.googleapis.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY" ,
|| "&zoom=14&size=512x512&maptype=roadmap" ,
|| "&markers=color:blue%7Clabel:S%7C40.702147,-74.015794" ,
|| "&markers=color:green%7Clabel:G%7C40.711614,-74.012318" ,
|| "&markers=color:red%7Ccolor:red%7Clabel:C%7C40.718217,-73.998284&sensor=false"
say cmd
cmd

exit
Daniel B. Martin
 
Old 12-23-2011, 08:00 AM   #11
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
What does the urlbar contain after you open it? The url works just fine for me, and I don't see anything obviously wrong in the script (not that I know much about rexx though). You need to compare the before and after strings to see where it's going wrong.


The google api url in my script is the same one Cedrik posted, by the way. I only removed the in-page anchor.
 
Old 12-23-2011, 01:19 PM   #12
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Original Poster
Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Thanks to danfan46 who contacted me privately with the solution. He said the parameter string sent to the Google Maps API must be enclosed in quotes.

This didn't work.
Code:
'firefox' ,
   "http://maps.googleapis.com/maps/api/staticmap?" || ,
   "center=3300+MORNINGSIDE+DR+27607&zoom=12&size=680x680" || , 
   "&maptype=roadmap&sensor=false"
This does work.
Code:
'firefox' ,
   '"' || ,
   "http://maps.googleapis.com/maps/api/staticmap?" || ,
   "center=3300+MORNINGSIDE+DR+27607&zoom=12&size=680x680" || , 
   "&maptype=roadmap&sensor=false" || ,
   '"'
In both examples I broke the long invocation line into several to improve readability on this forum.

Thanks to everyone who contributed ideas. SOLVED!

Daniel B. Martin
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Add your local knowledge to the map with Google Map Maker for the United States mjolnir General 4 05-04-2011 04:27 PM
Google creates censorship map Jeebizz Linux - News 3 09-22-2010 05:03 AM
[SOLVED] error: cannot open display: 0 when trying to launch Firefox Cultist Debian 2 09-08-2010 11:03 AM
Paid Assist Request - VPS Firefox Launch No Display Error jeewiz Linux - Software 3 08-21-2009 05:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration