Best way to geocode UK postcode with Google Maps API? - google-maps

What is the most effective and accurate way to geocode a UK postcode? Using the built in geocode object results in massively blurred results (lots of postcodes returning just a general area).
Are there any free UK postcode geocoding services I can plug into via JavaScript?

Edit: To be clearer this uses the local search in google search api, not the google maps api to do the geocoding which is much more accurate.
The best way is to use Google Search Api.
If you get an api key and get it all setup: http://code.google.com/apis/ajaxsearch/
var localSearch;
var map;
var icon;
var addressMarkerOptions;
google.load("search", "1");
google.load("maps", "2");
google.setOnLoadCallback(initialize);
function initialize()
{
localSearch = new GlocalSearch();
icon = new GIcon(G_DEFAULT_ICON);
addressMarkerOptions = { icon:icon , draggable: false};
map = new GMap2(document.getElementById("map"));
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());
plotAddress("OX4 1FJ");
}
/**
* This takes either a postcode or an address string
*
*/
function plotAddress(address)
{
localSearch.setSearchCompleteCallback(null,
function() {
if (localSearch.results[0])
{
var resultLat = localSearch.results[0].lat;
var resultLng = localSearch.results[0].lng;
var point = new GLatLng(resultLat,resultLng);
var marker = new GMarker(point, addressMarkerOptions);
map.addOverlay(marker);
map.setCenter(point, 5, G_NORMAL_MAP);
}
else
{
alert('address not found');
}
});
localSearch.execute(address + ", UK");
}

I believe Live/Bing/Windows maps' API gives a much more accurate placement for a UK postcode than the Google maps API does.
Alternatively, there are databases of UK postcode data available, although they're quite costly. There are some interesting comments on this post: http://jamiethompson.co.uk/web/2008/07/24/full-uk-postcode-database-for-free/

Related

Can i get the exact results as like google by passing just keyword?

I want to search places by keyword match.
I have used Google Map Places API for this and passed the following arguments for search in this name is dynamically added from search textbox.
var search = {
bounds: map.getBounds(),
keyword:[name] //name=dentist
};
I am getting results in this but these results are not same as I got from google search.
Suppose if I enter dentist and choose location Ahmadabad in auto complete then I need the search results exactly as I search "dentist in ahmedabad" in google.
Is it possible to get same results as google?
Help will be appreciated.
Check Places Library:
The functions in the Google Places JavaScript library enable your application to search for places (defined in this API as establishments, geographic locations, or prominent points of interest) contained within a defined area, such as the bounds of a map, or around a fixed point.
In your case use Text Search Requests:
The Google Places Text Search service is a web service that returns information about a set of places based on a string — for example "pizza in New York" or "shoe stores near Ottawa". The service responds with a list of places matching the text string and any location bias that has been set. The search response will include a list of places. You can send a Place Details request for more information about any of the places in the response.
Text Searches are initiated with a call to the PlacesService's textSearch() method.
service = new google.maps.places.PlacesService(map);
service.textSearch(request, callback);
You must also pass a callback method to textSearch(), to handle the results object and a google.maps.places.PlacesServiceStatus response.
var map;
var service;
var infowindow;
function initialize() {
var pyrmont = new google.maps.LatLng(-33.8665433,151.1956316);
map = new google.maps.Map(document.getElementById('map'), {
center: pyrmont,
zoom: 15
});
var request = {
location: pyrmont,
radius: '500',
query: 'restaurant'
};
service = new google.maps.places.PlacesService(map);
service.textSearch(request, callback);
}
function callback(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
var place = results[i];
createMarker(results[i]);
}
}
}
Hope it helps!

How should I use Google Maps API in China?

Maybe you know in China there are many services/products of Google banned, so that's so bad that as a developer can't use so great services and products. Like now I meet a problem on Google map. I need to get the longitude and latitude by a known place, but even I use the ditu.google.cn service, the function as below still will request to maps.googleapis.com which should have been banned,
var geocoder = new GClientGeocoder();
var latlng = geocoder.getLatLng(location,function(point) {
if (!point)
{
alert(location + " not found");
}
else
{
window.map.setZoom(13);
var marker = new GMarker(point);
markersArray.push(marker);
map.addOverlay(marker);
marker.openInfoWindowHtml(address);
}
});
Who can give me some advice to solve this obstacle.
Thanks very much.
Google maps works in China. You just have to use a different base URL.
From their website: The Google Maps APIs are served within China from https://maps.google.cn. When serving content to China, replace https://maps.googleapis.com with https://maps.google.cn.
<script src="https://maps.google.cn/maps/api/js?key=API_KEY"
type="text/javascript">
</script>

Determining the Place according to latitude and longitude

I'm using google maps API to determine a building(Place) which is a Google Point of interest. I'm trying to get the place reference for future use. but due to API limitation or whatever limitation. I can't get the exact place. As an example below latitude longitude belongs to "The university of Sydney". Using this code I could derive University of Sydney but as second result and also I tried many lat long but sometime appropriate result comes in the end or first. These places are prominent to Google Places Search system. The radius of this request looks lame to me. Is there any way to retrieve the exact place ?
var pyrmont = new google.maps.LatLng(-33.88858402464521, 151.18735671043396);
map = new google.maps.Map(document.getElementById('map-canvas'), {
center: pyrmont,
zoom: 15
});
var request = {
location: pyrmont,
radius: 1,
};
infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
service.nearbySearch(request, callback);
}
Advanced Thanks

display User Location in Gmaps based on his Zip code

I am building an accommodation website where users can advertise their room with their address.
I want to know if i can use the zipcode to show the exact location in Gmaps which i believe would be much cooler.
I would be very thankful if someone can guide me with simple set of steps to do the same.
thanks in advance!
You may want to use the Client-side Geocoder in the Google Maps API, as in the following example:
function initialize() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map_canvas"));
var geocoder = new GClientGeocoder();
geocoder.setBaseCountryCode("us");
geocoder.getLatLng(
"90210",
function(point) {
if (point) {
map.setCenter(point, 13);
var marker = new GMarker(point);
map.addOverlay(marker);
}
}
);
}
}
// ...
<div id="map_canvas" style="width: 500px; height: 300px"></div>
Simply change the "90210" parameter with the zip code of your users.
Check out the Google Maps API. You can use their geocoding service to convert a full address into coordinates, and then display a marker in a Google Map.
There are libraries available for various languages and platforms.
For PHP, here's an article on Geocoding in PHP (it refers to V2 of the Maps API, though; V3 is the brand-newest.)

Focus a Google Map based on a town name

I have a town name, gmap modules and plg. i want to focus desired location in gmap by giving town name as input instead of longitude and latitude. Is there any way to do this in Joomla 1.5.x.
Thanks in advance
naveen
You will need a GeoCoder to get the location based on the city name. Don't know nothing about Joomla, but here is and example using JavaScript.
The HTML
<div id='myMap' style="position:relative; width:740px; height:400px;"></div>
The script
You have to complete with the Google API Key and the city you want
<script src="http://maps.google.com/maps?file=api&v=2.x&key=XXXXX">
</script>
<script type="text/javascript">
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function')
{ window.onload = func; }
else {
window.onload = function()
{ oldonload(); func(); }
}
}
var map = null;
var geocoder = null;
addLoadEvent(initialize);
function initialize() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("myMap"));
map.setCenter(new GLatLng(-12.08, -53.08), 4);
map.addControl(new GSmallMapControl());
map.addControl(new GOverviewMapControl());
geocoder = new GClientGeocoder();
showAddress('YourCity, YourCountry');
}
}
function showAddress(address) {
if (geocoder) {
geocoder.getLatLng(
address,
function(point) {
if (!point) {
$("#myMap").hide();
} else {
map.setCenter(point, 8);
var marker = new GMarker(point);
map.addOverlay(marker);
marker.openInfoWindowHtml(address);
}
}
);
}
}
</script>
I was looking for a way to do something similar myself. I tried using the Google Maps API to search for the location, but it was sometimes wrong. Eventually I ended up querying the Wikipedia page relating to the town and parsing the coordinates, then using those. Probably not the solution you were looking for, but it works for me. I would advise trying to search the Maps API first as Eduardo details, but if that doesn't work, this might.
You can make a HTTP call from Javascript to Google's geocoding service, passing it a place name/postcode, and getting lat/long back.
I do this using the CSV option as I am calling it from ASP, but I think you can get back XML or JSON too.
Fire off this URL if you have a Google maps API key:
http://maps.google.com/maps/geo?gl=uk&q=guildford,+uk&output=csv&key=XXXXXXXXXXX
First parameter back is response code. 200 means success.
Then you get latitude and longitude, comma seperated.