views:

98

answers:

3

I want to pass several parameters to this url and generate the map from it and show it in my java application. I know the code to download the image and show it in the java application. I want to know how to pass parameters to this address:

http://maps.google.com/maps/api/staticmap?center=Nugegoda&zoom=14&size=1000x312&maptype=roadmap&markers=color:blue|label:S|size=tiny|Mirihana&markers=size:mid|color:0xFFFF00|label:C|Udahamulla&sensor=false

In this link Nugegoda and Mirihana and Udahamulla is the one that i should pass from the application. And then it will generate a image and i do need to show it. Even if u check this link. It's a image.

Can someone help me?

+3  A: 

You can use the Google Static Maps API. If you purely want to interpolate variables, you can do:

String center = ...;
String labelS = ...;
String labelC = ...;

String url = "http://maps.google.com/maps/api/staticmap?center=" 
+ center 
+ "&zoom=14&size=1000x312&maptype=roadmap&markers=color:blue|label:S|size=tiny|" 
+ labelS 
+ "&markers=size:mid|color:0xFFFF00|label:C|" + labelC + "&sensor=false";
Matthew Flaschen
The OP is, I think. The provided URL is from the Static Maps API.
Daniel Vassallo
Indeed, OP is using Static Maps API. It seems to me that the OP just wants to know how he (she?) can insert variable parts (city names) in the URL, so variable substitution in a String.
jschulenklopper
@jschulenklopper: Maybe you're right... String concatenation seemed too easy (obvious) to be the real question :)
Daniel Vassallo
A: 

Is your question "How do I construct a String that has some variable parts in it?" At the time you build the URL string in your application, it is 'just' a String. You can concatenate the different parts, or use formatted printing. (See format.)

(The URL given already has a number of parameters: every pair behind the question mark is a variable name and value.)

jschulenklopper
i do need to add a route like a highlighted line from destination to location. is it possible. can you please help
Nubkadiya
+2  A: 

The Static Maps API can do geocoding for you if you pass a valid address to the center parameter:

http://maps.google.com/maps/api/staticmap?center=Nugegoda&zoom=14&size=400x400&sensor=false

Nugegoda

The parameters that you can pass are all listed here. As is standard in URLs, all parameters are separated using the ampersand & character. Therefore if you want to add the maptype parameter and set it to hybrid for example, you can simply add &maptype=hybrid to the above URL:

http://maps.google.com/maps/api/staticmap?center=Nugegoda&zoom=14&size=400x400&sensor=false&maptype=hybrid

Nugegoda

Then if you want to show the Udahamulla map, simply replace Nugegoda with Udahamulla for the center parameter:

http://maps.google.com/maps/api/staticmap?center=Udahamulla&zoom=14&size=400x400&sensor=false

Udahamulla


UPDATE:

Further to the comments below, you can add a polyline on a static map by using the path parameter, as follows:

http://maps.google.com/maps/api/staticmap?center=Udahamulla&zoom=13&size=400x400&path=color:0xff0000ff|weight:5|Maharagama|Nugegoda&sensor=false

Maharagama to Nugegoda

Note how the API geocodes the addresses "Maharagama" and "Nugegoda", so you do not need to pass the latitude and longitude coordinates of the localities.

Currently the Static Maps API does not do driving directions. However if you have the points of a complex route, you could plot the path as an encoded polyline.

Daniel Vassallo
i do need to add a route like a highlighted line from destination to location. is it possible. can you please help
Nubkadiya
@Nubkadiya: Updated my answer.
Daniel Vassallo