Created
March 4, 2012 09:17
-
-
Save icoloma/1971643 to your computer and use it in GitHub Desktop.
Creates a URL to be used in Google Maps. Useful for creating links where the javascript API is not available, e.g. in HTML mail.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Creates a URL to be used in Google Maps. Useful for creating links where the javascript API is not available, e.g. in HTML mail. | |
* For info about each arguments, see http://mapki.com/wiki/Google_Map_Parameters | |
* @author icoloma | |
*/ | |
public class GoogleMapsUrlFactory { | |
private HashMap<String, String> params = Maps.newLinkedHashMap(); | |
public String build() { | |
StringBuilder url = new StringBuilder().append("http://maps.google.com/?"); | |
boolean hasParams = false; | |
for (Map.Entry<String, String> entry : params.entrySet()) { | |
if (hasParams) { | |
url.append("&"); | |
} | |
url.append(entry.getKey()).append("=").append(entry.getValue()); | |
hasParams = true; | |
} | |
return url.toString(); | |
} | |
/** the human-readable address to search for */ | |
public GoogleMapsUrlFactory withAddress(String address) { | |
params.put("q", UrlBuilder.urlEncode(address)); | |
return this; | |
} | |
/** if set, the location */ | |
public GoogleMapsUrlFactory withGeo(GeoPt geo) { | |
params.put("ll", geo.getLatitude() + "," + geo.getLongitude()); | |
return this; | |
} | |
/** if set, this is the text that will be displayed in the info window */ | |
public GoogleMapsUrlFactory withTitle(String title) { | |
String currentValue = params.get("q"); | |
if (currentValue == null) { | |
currentValue = ""; | |
} | |
params.put("q", currentValue + "(" + UrlBuilder.urlEncode(title) + ")"); | |
return this; | |
} | |
/** if set, the Locale to display the user interface */ | |
public GoogleMapsUrlFactory withLocale(Locale locale) { | |
params.put("hl", locale.getLanguage()); | |
return this; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GoogleMapsUrlFactoryTest { | |
@Test | |
public void testBuild() throws Exception { | |
assertEquals( | |
"http://maps.google.com/?q=foo+bar", | |
new GoogleMapsUrlFactory().withAddress("foo bar").build() | |
); | |
assertEquals( | |
"http://maps.google.com/?q=foo(bar)", | |
new GoogleMapsUrlFactory().withAddress("foo").withTitle("bar").build() | |
); | |
assertEquals( | |
"http://maps.google.com/?q=foo(bar)&ll=1.2,-2.0", | |
new GoogleMapsUrlFactory().withAddress("foo").withTitle("bar").withGeo(new GeoPt(1.2f, -2.0f)).build() | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment