Skip to main content

Google Maps Geo Coding Example

Geocoding is the process of converting addresses (like "Bangalore") into geographic coordinates (like latitude 12.97xxxxx and longitude 77.f9xxxx), which you can use to place markers or position the map. The Google Maps API includes a Geocoding service that can be accessed directly via an HTTP request or by using a GClientGeocoder object.

Note that geocoding is a time and resource intensive task. Whenever possible, pre-geocode your addresses (using the HTTP geocoder or other geocoding service), and store your results.



Getting the Geo Code using Google Api For a Particular Geo Address using

::class GClientGeocoder::


This class is used to communicate directly with Google servers to obtain geocodes for user specified addresses. In addition, a geocoder maintains its own cache of addresses, which allows repeated queries to be answered without a round trip to the server.


The Following Example shows How to echo/alert the Longitude and Latitude of a Particular location


var geocoder = null;

function initialize() {
if (GBrowserIsCompatible()) {
geocoder = new GClientGeocoder();
}
}

function showLocationLatLon(addressget) {
var address = addressget;
geocoder.getLocations(address, echoLatLong);
}

function findLocation(address) {
document.forms[0].q.value = address;
showLocation();
}

function
echoLatLong(response) {
if (!response || response.Status.code != 200) {
alert("Sorry, we were unable to geocode that address");
} else {


place = response.Placemark[0];
point = new GLatLng(place.Point.coordinates[1],
place.Point.coordinates[0]);
alert("Address:: "+place.address);
alert("Lat/Lon "+place.Point.coordinates[1]+" "+place.Point.coordinates[0]);
}
}


The Above Example is self explanatory.

Constructor

ConstructorDescription
GClientGeocoder(cache?)Creates a new instance of a geocoder that talks directly to Googleservers. The optional cache parameter allows one to specify acustom client-side cache of known addresses. If none is specified,a GFactualGeocodeCache is used.(Since 2.55)
Methods

/tr>
MethodsReturn ValueDescription
getLatLng(address, callback)noneSends a request to Google servers to geocode the specifiedaddress. If the address was successfully located, the user-specified callback function is invoked with a GLatLng point. Otherwise, the callback function is given a null point. In case of ambiguous addresses, only the point for the best match is passed to the callback function.(Since 2.55)
getLocations(address, callback)noneSends a request to Google servers to geocode the specified address. A reply that contains status code, and if successful, one or more Placemark objects, is passed to the user-specified callback function. Unlike the GClientGeocoder.getLatLng method, the callback function may determine the reasons for failure by examining the code value of the Status field.(Since 2.55)
getCache()GGeocodeCache Returns currently used geocode cache, or null, if no client-side caching is performed. (Since 2.55)
setCache(cache)noneSets a new client-side caching. If this method is invoked with cache set to null, client-side caching is disabled. Setting a new cache discards previously stored addresses. (Since 2.55)
setViewport(bounds)noneSets the geocoder to magnify geocoding results within or near the given viewport. The viewport is expressed as a GLatLngBounds rectangle. Note that setting a viewport does not restrict results to that bounding box, though it will elevate them in priority.(Since 2.82)
getViewport()GLatLngBounds Returns the viewport for magnifying geocoding results within that geocoder. The viewport is expressed as a GLatLngBounds rectangle.(Since 2.82)
setBaseCountryCode(countryCode)noneSets the geocoder to bias search results as if they were sent from the domain specified by the given ISO 3166-1 (alpha-2) country code. Geocoding is only supported for those countries in which Google Maps itself supports geocoding. Most ISO 3166-1 codes are identical to top-level Internet domain, with some notable exceptions. For example, "ES" refers to the top-level Internet domain for Spain: .es while "GB" refers to the top-level Internet domain for Great Britain: .co.uk.Note that the default domain is the domain from which you initially load the Maps API.Country codes are case insensitive.(Since 2.82)
getBaseCountryCode()StringReturns the current country code in use by the given geocoder. (If no country code is in effect, this method returns null.) (Since 2.82)
reset()none Resets the geocoder. In particular this method calls theGGeocodeCache.reset() method on the client-side cache, if one is used by this geocoder.(Since 2.55)



Example Of GeoCoding Using Google Map APi




var map = null;
var geocoder = null;

function initialize() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map_canvas"));
map.setCenter(new GLatLng(37.4419, -122.1419), 13);
geocoder = new GClientGeocoder();
}
}

function showAddress(address) {
if (geocoder) {
geocoder.getLatLng(
address,
function(point) {

if (!point) {
alert("Sorry, we were unable to geocode that address");
} else {



map.setCenter(point, 13);
var marker = new GMarker(point);
map.addOverlay(marker);
marker.openInfoWindowHtml(address);


}
}
);
}

}



More Info Here::

http://www.google.com/apis/maps/documentation/services.html#Services_Overview
http://www.google.com/apis/maps/documentation/reference.html#GClientGeocoder

Comments

Popular posts from this blog

Converting Java Map to String

Java Collections framework, String manipulation etc is something that we often encounter in Development process.
For processing collections (like checking null/empty, Intersection, Disjunction) We do have some of the very use full libraries.

Some of the Collection related libraries are Apche Commons Collections and Google  Collections(Guava).

Problem Use Case

This article explains how to convert a Java Map to String(and vice versa) using different libraries and technique.

One way is to use StringBuilder(Or String) and loop though the Map and build the String by applying some sort of separator ( for key:value and entry). Here we have to take care of the null value etc.

Without Any Library
If we want to convert the map to a String with key value separator and also individual entry seperator in the resulting String, we have to write code for that. For a simple Map, we have to iterate though the map, take care of the null values etc. Following is a sample to get String built out from Map C…

Masking Credit Card number in Java

Sometimes we need to mask crucial information like Credit Card Numbers, CVV numbers etc before storing  or logging the information. This example mask Credit Card Number (Except last 4 Digit) from a Text which contains information along with Credit Card Number.

The following example demonstrates how we can easily mask the credit card with Matcher and Pattern Classes. This Sample Code uses Matcher and Pattern.
Pattern Used in this sample is not optimized for Credit Card Numbers, this pattern will get any numerical numbers in the String Content.  Based on the Credit Card Type a more efficient and Strict RegEx can be used to mask the Credit Card.
/**Mask the Credit card number but last four digit value **/   Pattern PATTERN = Pattern.compile("[0-9]+"); String message = content; Matcher matcher = PATTERN.matcher(message); String maskingChar = "*"; StringBuilder finalMask = new StringBuilder(maskingChar); while …

Invoking EJB deployed on a remote machine

Invoking EJB deployed on a remote machineIn case we are calling remote ejb( ejb deployed on remote machines),The JNDI lookup might lookup like,Properties env = new Properties();env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");env.put(Context.PROVIDER_URL, "XX.XXX.XX.XX:1099");env.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces"); Context ctx = new InitialContext(env);If we are calling local ejb then we can simply create InitialContext without any parameters.Like,Context ctx = new InitialContext();