Google Maps API - Batch Geocoder
Jump to navigation
Jump to search
About
NOTE: This page is a daughter page of: Google Maps API
This piece of code allows you to paste in a (new line sepearated) list of address, and will try to resolve all of them to a lat/lng, which it shows on the map, and output to a textarea for you to copy/paste into a spreadsheet program if desired. Going from a text address (eg: "1 Main Street, Sydney, NSW") to lat/lng coordinates is called "geocoding".
NOTE: If you only need 1 lat/lng... or want to "reverse geocode" (from lat/lng to text address) try www.latlong.net.
Showing a Thumbnail And Map of a Place
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Batch Geocoder</title>
<style>
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
textarea {
vertical-align: top;
font-size: 90%;
}
#btn-geocode {
vertical-align: top;
}
#panel {
position: absolute;
top: 5px;
left: 50%;
margin-left: -400px;
z-index: 5;
background-color: #fff;
padding: 5px;
border: 1px solid #999;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
<script>
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var mapOptions = {
zoom: 8,
center: latlng
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
}
/**
* Appends the given line to 'txt-coordinates' textarea, followed by a newline.
* @param {string} outputLine Line of text to output.
*/
function addLineToOutput(outputLine) {
var text = document.getElementById('txt-coordinates').value;
document.getElementById('txt-coordinates').value = text + outputLine + '\n'
}
/**
* Tries to geocode an address and, if successful, adds a marker to the
* map and writes a lat/lng to 'txt-coordinates' textarea. If lookup fails
* (address not found), no marker is added, and prints 'NOMATCH' to the
* textarea.
* @param {string} address The address to look up (eg: "Sydney, NSW").
*/
function addPoint(address) {
geocoder.geocode( { 'address': address }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
title: address
});
addLineToOutput(address + '\t' + marker.position.lat() + '\t' + marker.position.lng());
} else {
addLineToOutput(address + '\tNOMATCH');
console.log('Geocoder failed on "' + address + '" due to: ' + status);
}
});
}
/**
* Reads 'txt-addresses' textarea, and for each line, looks up that address,
* writes coordinates out, and adds a marker to the map.
*/
function geocodeAddresses() {
var allAddresses = document.getElementById('txt-addresses').value;
document.getElementById('txt-coordinates').value = ''; // Clear output.
var address = allAddresses.split('\n');
for (var i = 0; i < address.length; i++) {
// NOTE: An intracacy here is that if you call them all together then
// the output won't occur in order!
setTimeout(addPoint, i*100, address[i]);
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="panel">
<textarea id="txt-addresses" rows="10" cols="40" wrap="off">Sydney
Melbourne
Texas
(1 per line)</textarea>
<input id="btn-geocode" type="button" value="Geocode >" onclick="geocodeAddresses()">
<textarea id="txt-coordinates" rows="10" cols="40" wrap="off" readonly>Output appears here</textarea>
</div>
<div id="map-canvas"></div>
</body>
</html>
<!-- ACKNOWLEDGEMENTS:
Based on code from: https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple !-->