Adding Geocoding to a script

Soldato
Joined
16 Oct 2007
Posts
7,482
Location
UK
Hi all,

Really am stuck - i managed to get Google Maps API (and the accompanying Geocoding) to work on my site (by using a database field for the postcode)
but now i want to incorporate another geocoding function - as shown
http://www.tomanthony.co.uk/blog/geocoding-uk-postcodes-with-google-map-api/

My existing, working script is

PHP:
<?php
}

//	$PostCode = "SW1V 2LN";

	if ($PostCode != "")
	{
	
		function getLngLatByPostCode($PostCode)
		{
			define("MAPS_HOST", "maps.google.co.uk");
			define("KEY", "ABQIAAAAP-VY9zvRYySwFJkfJfJjVRRy-Xi2PuALY4zOIkyqajUD6OBV-BQVSmr26juGJ8dEeLvF0CjZJD_K_Q");
		
			$PostCode = mysql_escape_string($PostCode);
		
			$delay = 0;
			$base_url = "http://" . MAPS_HOST . "/maps/geo?output=csv&key=" . KEY;
		
			$geocode_pending = true;
		
		
			while ($geocode_pending) 
			{
				$request_url = $base_url . "&q=" . urlencode($PostCode);					
		
				$csv = file_get_contents($request_url);
		
				$csvSplit = split(",", $csv);
				$status = $csvSplit[0];
				$lat = $csvSplit[2];
				$lng = $csvSplit[3];
		
				if (strcmp($status, "200") == 0) 
				{
					$geocode_pending = false;
					$lat = $csvSplit[2];
					$lng = $csvSplit[3];
				} 
				else if (strcmp($status, "620") == 0) 
				{
					$delay += 100000;
				} 
				else 
				{
					$geocode_pending = false;
				}
		
				usleep($delay);
			}
			
			if($lng != "" && $lat != "")
			{
				return $lng . "," . $lat;
			}
			else
			{
			
				return "";
			}

		}
		

		$longilati = getLngLatByPostCode($PostCode);
		$longilatiArr = @explode(',', $longilati); 
		$longi =  $longilatiArr[0];
		$lati  =  $longilatiArr[1];

		if ($longi != "0" || $lati != "0")
		{
?>
	<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAP-VY9zvRYySwFJkfJfJjVRRy-Xi2PuALY4zOIkyqajUD6OBV-BQVSmr26juGJ8dEeLvF0CjZJD_K_Q" type="text/javascript"></script>

	<script type="text/javascript">


		function onLoad()
		{
			lon = <?=$longi?>;
			lat = <?=$lati?>;
			
//			document.getElementById("map").style.display = "";
			document.getElementById("map").style.height = "500px";
			
			var map = new GMap(document.getElementById("map"));
			map.addControl(new GLargeMapControl());
			map.addControl(new GOverviewMapControl());
//					map.addControl(new GMapTypeControl());
			map.centerAndZoom(new GPoint(lon, lat), 5);


			var point = new GPoint(lon, lat);
			var icon = new GIcon();
			icon.image = "http://www.workingaway.co.uk/images/green.png";
			icon.iconSize = new GSize(36, 41);
			icon.iconAnchor = new GPoint(15, 39);
			icon.infoWindowAnchor = new GPoint(5, 1);
		
			var marker = new GMarker(point, icon); 
		
			map.addOverlay(marker);
			
		}

		window.onLoad = onLoad();
	
	</script>

If anyone could help me out, i'd REALLY appreciate it.

Thank you
 
Back
Top Bottom