Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with pre-populating a UK google map

Status
Not open for further replies.

jasonindus

Programmer
Dec 7, 2006
27
0
0
GB
hi, i'm not that knowledgable with javascript, but i was able to follow a tutorial to google map UK locations.

everything works fine by using a form to type in a postcode then submitting a search, but i would like to change the way it works...

i'm passing a postcode variable to the page and would like to use it to pre-populate or pre-search the map as it first loads onto the page. although i would still like to use the form to do additional searches.

can you guys help me out on this one?

here's a test page with the map...

and here the javascript code that i got from the tutorial...

Code:
var map;
var localSearch = new GlocalSearch();

var icon = new GIcon();
icon.image = "[URL unfurl="true"]http://www.google.com/mapfiles/marker.png";[/URL]
icon.shadow = "[URL unfurl="true"]http://www.google.com/mapfiles/shadow50.png";[/URL]
icon.iconSize = new GSize(20, 34);
icon.shadowSize = new GSize(37, 34);
icon.iconAnchor = new GPoint(10, 34);


function usePointFromPostcode(postcode, callbackFunction) {
	
	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);
				callbackFunction(point);
			}else{
				alert("Postcode not found!");
			}
		});	
		
	localSearch.execute(postcode + ", UK");
}

function placeMarkerAtPoint(point)
{
	var marker = new GMarker(point,icon);
	map.addOverlay(marker);
}

function setCenterToPoint(point)
{
	map.setCenter(point, 17);
}

function showPointLatLng(point)
{
	alert("Latitude: " + point.lat() + "\nLongitude: " + point.lng());
}

function mapLoad() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		
		
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(54.622978,-2.592773), 5, G_HYBRID_MAP);
	}
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function addUnLoadEvent(func) {
	var oldonunload = window.onunload;
	if (typeof window.onunload != 'function') {
	  window.onunload = func;
	} else {
	  window.onunload = function() {
	    oldonunload();
	    func();
	  }
	}
}

addLoadEvent(mapLoad);
addUnLoadEvent(GUnload);
 
I am not all that familiar with javascript, but I've been working with the google maps api all weekend (in other words, I do play one on TV ;-) )

I think your problem is that the function callbackFunction does not exist in your code. I just read over the tutorial you mentioned, and it says that callbackFunction is supposed to be the name of the function (within your code) that you want to act on the point that you've created. If I had to hazard a guess, I would bet that what you need to put there is:

Code:
placeMarkerAtPoint(point);

Like I said, I am by no means an expert on javascript but I think I'm reading this right.

HOpe this helps,

ALex

Ignorance of certain subjects is a great part of wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top