The geocoding service returns a set of results where the first one in the set should be the most accurate. Of course, the resulting set can be influenced by the map options.
Follow the comments in the script that describes the functionality.
var map;
var geocoder;
function initialize() {
map = new GMap2(document.getElementById("map_canvas"));
map.setCenter(new GLatLng(34, 0), 1);
geocoder = new GClientGeocoder();
}
// addAddressToMap() is called when the geocoder returns an
// answer. It adds a marker to the map with an open info window
// showing the nicely formatted version of the address and the country code.
function addAddressToMap(response) {
map.clearOverlays();
if (!response || response.Status.code != 200) {
alert("Sorry, we were unable to geocode that address");
} else {
// clear the container where will appear the similarities
document.getElementById('s').innerHTML='';
//this is the marker that will be placed on the map
place = response.Placemark[0];
point = new GLatLng(place.Point.coordinates[1],
place.Point.coordinates[0]);
marker = new GMarker(point);
map.addOverlay(marker);
marker.openInfoWindowHtml(place.address + '<br>' +
'<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
//now we add the similarities
//'lgt' is the total number of similaties (including the one placed on the map)
var lgt=response.Placemark.length;
//now we loop through each similarity
for(i=0;i<lgt;i++){
var mn=response.Placemark[i];
var mnpoint=new GLatLng(mn.Point.coordinates[1],
mn.Point.coordinates[0]);
markere = new GMarker(mnpoint);
//and add it on our list
document.getElementById('s').innerHTML+=mn.address+'<br/>';
}
}
}
// showLocation() is called when you click on the Search button
// in the form. It geocodes the address entered into the form
// and adds a marker to the map at that location.
function showLocation() {
var address = document.forms[0].q.value;
geocoder.getLocations(address, addAddressToMap);
}
// findLocation() is used to enter the sample addresses into the form.
function findLocation(address) {
document.forms[0].q.value = address;
showLocation();
}
No comments:
Post a Comment