SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Addict eanimator's Avatar
    Join Date
    Sep 2005
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lan/Lat information

    Hello guys,

    I am integrating google maps into my app and need to know how to get lan/lat information on the fly for a particular address?

    Is it possible?

    Thanks in advance.

  2. #2
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    Google Maps can do that for you. Read up on the documentation for it. If you have any specific technical questions, feel free to ask.
    Salathe
    Software Developer and PHP Manual Author.

  3. #3
    SitePoint Addict eanimator's Avatar
    Join Date
    Sep 2005
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have list of addresses and need to get all the lan/lat for these addresses as I need to save it in my database and then I will be able to show all the lan/lat point at once. Please let me know about it. thank you for the reply

  4. #4
    SitePoint Addict eanimator's Avatar
    Join Date
    Sep 2005
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I went through docs but couldn't find it.

    It will be a process from the back-end to save all the lan/lat in my database table.

    Please let me know thanks again

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Location
    UK
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't know about other countries but my experience of Google Maps API for UK postcodes was that, using the API, you would only get a generic central locaton for each postcode and it ignored ther second half of the postcode.

    So YO11 1AA and YO11 9ZZ would both get the same location, neither of which would be accurate.

    The Google Maps search facility is apparently based on much more accurate info so you type in postcodes to that and get a very accurate location.

    I ended up copying those locations using this:
    http://www.gorissen.info/Pierre/maps...apLocation.php

    to manually find each lat / lon and inserted them into a table and dragged these co-ordinates out for each business id.

    Very time consuming so would also be interested to know if there's a solution I missed.

  6. #6
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You do not need to look up and store the latitudes/longitudes. You can add all your addresses as points on the map. Google will figure out the latitude/longitude from the addresses.

  7. #7
    SitePoint Addict eanimator's Avatar
    Join Date
    Sep 2005
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There must be a solution to get lan/lat based on address. Currently my script is sending one request a time and as a resut pin is being displayed. there are nearly 35 pins and its taking time. I need to get lan/lat information so that I can place markers ( all at once).

  8. #8
    SitePoint Addict eanimator's Avatar
    Join Date
    Sep 2005
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    You do not need to look up and store the latitudes/longitudes. You can add all your addresses as points on the map. Google will figure out the latitude/longitude from the addresses.
    Can you please provide sample code? I would appreciate your help

  9. #9
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

  10. #10
    SitePoint Addict eanimator's Avatar
    Join Date
    Sep 2005
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    var map = new GMap2(document.getElementById("map_canvas"));
    var geocoder = new GClientGeocoder();

    function showAddress(address) {
    geocoder.getLatLng(
    address,
    function(point) {
    if (!point) {
    alert(address + " not found");
    } else {
    map.setCenter(point, 13);
    var marker = new GMarker(point);
    map.addOverlay(marker);
    marker.openInfoWindowHtml(address);
    }
    }
    );
    }

    for one address its fine. I have to show 35 markers and my script is currently calling this

    showAddress(address);
    showAddress(address);
    showAddress(address);
    showAddress(address);
    .
    .
    .
    showAddress(address); // 35 times.

    on Timeinterval which is taking long. I need to place all the markers at once.

  11. #11
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't know if this will help any, but I ran into a time issue using the GoogleMaps JS API, so I now grab the address with a PHP object and pass it to my page view AJAX.

    Code php:
    class GoogleGeoData {
     
    	private $key = ""; ## GoogleMaps Key
    
    	public $debug = false;
     
    	public function __construct($key = null) {
    		if (!is_null($key)) {
    			$this->key = $key;
    		}
    		if (empty($this->key)) {
    			$this->_error("You must specify a Google API key to continue.");
    		}
    		$this->key = rawurlencode($this->key);
    	}
     
    	public function fetch($address, $dec = 6) {
    		$address = $this->_address($address);
    		$api_path = "http://maps.google.com/maps/geo?q=".$address."&output=csv&key=".$this->key;
    		if (($result = @file_get_contents($api_path)) === false) {
    			return $this->_error("There was an error fetching your request.");
    		} else {
    			list($code, $prec, $lat, $long) = split(",", $result);
    			if ($code != 200) {
    				if ($this->debug) {
    					return $this->_error("There was an error fetching your request.");
    				}
    				return false;
    			}
    			if (empty($lat) || empty($long)) {
    				if ($this->debug) {
    					return $this->_error("There was an error fetching your request.");
    				}
    				return false;
    			}
    		}
    		return array(
    			rawurldecode($address),
    			round($lat, $dec),
    			round($long, $dec),
    		);
    	}
     
    	private function _address($address) {
    		$address = str_replace("#", " ", $address);
    		$address = preg_replace("/\s+/", " ", $address);
    		$address = preg_replace("/(\d)(th|st|nd)/i", "$1", $address);
    		$address = preg_replace("/unit\s*\d+[,\.]*/i", "", $address);
    		$address = rawurlencode($address);
    		return $address;
    	}
     
    	private function _error($err) {
    		trigger_error($err, E_USER_ERROR);
    	}
    }

    I use this object to pull the data, then save it to a database so I wont have to query google twice for the same address.

    Code php:
    $geo = new GoogleGeoData();
    $result = $geo->fetch("Address");
     
    $lat = $result[1];
    $lon = $result[2];

    Then pass that to the page via AJAX and have the GoogleMaps API add the marker

    Code javascript:
    var map;
     
    function init() {
    	map = new GMap2($("canvas"));
    	map.addControl(new GLargeMapControl3D());
    	map.addControl(new GMenuMapTypeControl());
    }
     
    function addMarker(lat, lng) {
    	var point = new GLatLng(lat, lng);
    	map.addOverlay(new GMarker(point));
    }

  12. #12
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by eanimator View Post
    // 35 times.

    on Timeinterval which is taking long. I need to place all the markers at once.
    Then why are you using a time interval?

  13. #13
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    This was announced by Google this morning;

    http://googlegeodevelopers.blogspot....oding-web.html

    This post might put that announcement into perspective for you and also links to what was, until this morning, state of the art (a comparison of 3 other services).

    Fast moving stuff this, eh?

  14. #14
    SitePoint Addict eanimator's Avatar
    Join Date
    Sep 2005
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh Wowwww Thank you all for the response

  15. #15
    SitePoint Addict eanimator's Avatar
    Join Date
    Sep 2005
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by unwrittenphp View Post
    Don't know if this will help any, but I ran into a time issue using the GoogleMaps JS API, so I now grab the address with a PHP object and pass it to my page view AJAX.

    Code php:
    class GoogleGeoData {
     
    	private $key = ""; ## GoogleMaps Key
    
    	public $debug = false;
     
    	public function __construct($key = null) {
    		if (!is_null($key)) {
    			$this->key = $key;
    		}
    		if (empty($this->key)) {
    			$this->_error("You must specify a Google API key to continue.");
    		}
    		$this->key = rawurlencode($this->key);
    	}
     
    	public function fetch($address, $dec = 6) {
    		$address = $this->_address($address);
    		$api_path = "http://maps.google.com/maps/geo?q=".$address."&output=csv&key=".$this->key;
    		if (($result = @file_get_contents($api_path)) === false) {
    			return $this->_error("There was an error fetching your request.");
    		} else {
    			list($code, $prec, $lat, $long) = split(",", $result);
    			if ($code != 200) {
    				if ($this->debug) {
    					return $this->_error("There was an error fetching your request.");
    				}
    				return false;
    			}
    			if (empty($lat) || empty($long)) {
    				if ($this->debug) {
    					return $this->_error("There was an error fetching your request.");
    				}
    				return false;
    			}
    		}
    		return array(
    			rawurldecode($address),
    			round($lat, $dec),
    			round($long, $dec),
    		);
    	}
     
    	private function _address($address) {
    		$address = str_replace("#", " ", $address);
    		$address = preg_replace("/\s+/", " ", $address);
    		$address = preg_replace("/(\d)(th|st|nd)/i", "$1", $address);
    		$address = preg_replace("/unit\s*\d+[,\.]*/i", "", $address);
    		$address = rawurlencode($address);
    		return $address;
    	}
     
    	private function _error($err) {
    		trigger_error($err, E_USER_ERROR);
    	}
    }

    I use this object to pull the data, then save it to a database so I wont have to query google twice for the same address.

    Code php:
    $geo = new GoogleGeoData();
    $result = $geo->fetch("Address");
     
    $lat = $result[1];
    $lon = $result[2];

    Then pass that to the page via AJAX and have the GoogleMaps API add the marker

    Code javascript:
    var map;
     
    function init() {
    	map = new GMap2($("canvas"));
    	map.addControl(new GLargeMapControl3D());
    	map.addControl(new GMenuMapTypeControl());
    }
     
    function addMarker(lat, lng) {
    	var point = new GLatLng(lat, lng);
    	map.addOverlay(new GMarker(point));
    }
    Wonderful!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •