ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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:
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?
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.
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 )
# 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:
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.