SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,293
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    use google maps just to get distances?

    is it possible to access google maps' database to get distances without displaying a whole map? is javascript the only way to make use of google maps?

    what i want to do is have a page which allows someone to type in a uk postcode, then make use of google maps to work out the distance from the supplied postcode to another predefined/fixed postcode and display that distance back to the user -- just the distance preferably. possible? how? i've got a horrable feeling it's not possible :/

    any info much appreciated. thanks.

  2. #2
    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)
    All you need is the latitude and longitude of the address they typed in, then you can compute the distance from the lat/lon of the place you already know.

    I'm pretty sure Google offers a geocoding API that supports UK addresses somewhere. You wouldn't use Google Maps.

  3. #3
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,293
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    > I'm pretty sure Google offers a geocoding API that supports UK addresses somewhere. You wouldn't use Google Maps.

    the only thing that does uk postcodes to location of any kind that i know of (that doesn't cost a lot of money) is maps.google.com/.co.uk. uk postcodes are is expensive copyright protected info generally. that's why i thought that was the thing to use.

    the maps.google.com web page allows you to type in for example L71 2LB to L72 3LB and it replies:

    Driving directions to Liverpool, Merseyside L72, UK
    7.2 mi about 25 mins

    Liverpool L71 2LB
    UK
    1. Head southwest on Royal Mail St toward Hawke St - 246 ft
    2. Turn right at Hawke St - 249 ft
    3. Turn right at Copperas Hill ....

    if it's not possible to get that info somehow i'm skuppered i think. the above miles by road distance from postcodes would be fantastic, but straight line info would be good -- anything would be better than nothing. as you say if i could get the long+lat data all would be fine but the only thing i know of that provides info like that is the maps.google page

    oh dear it really didn't occur to me that the whole google maps thing wasn't a server side thing at all, but all javascript from the user's browser. silly me.


    there are things which look vaguely helpful in the javascript google maps api documentation, eg http://code.google.com/apis/maps/doc.../services.html

    Code:
    // Create a directions object and register a map and DIV to hold the 
    // resulting computed directions
    
    var map;
    var directionsPanel;
    var directions;
    
    function initialize() {
      map = new GMap2(document.getElementById("map_canvas"));
      directionsPanel = document.getElementById("my_textual_div");
      map.setCenter(new GLatLng(49.496675,-102.65625), 3);
      directions = new GDirections(map, directionsPanel);
      directions.load("New York, NY to Chicago, IL");
    }
    and on this page http://code.google.com/apis/maps/doc...reference.html

    distanceFrom(other, radius?)

    Number

    Returns the distance, in meters, from this point to the given point. By default, this distance is calculated given the default equatorial earth radius of 6378137 meters. The earth is approximated as a sphere, hence the distance could be off as much as 0.3%, especially in the polar extremes. You may also pass an optional radius argument to calculate distances between GLatLng coordinates on spheres of a different radius than earth. (Since 2.89)
    and
    getDistance()

    Object

    Returns an object literal representing the total distance of the directions request (across all routes). The object contains two fields: a number called "meters" indicating the numeric value of the distance (in meters), and a string called "html" containing a localized string representation of the distance in the units that are predominant in the starting country of this set of directions. (Since 2.81)
    even if it is technically possible to extract the info (like doing all the submissions etc but keeping it all hidden apart from extracting the info i'm after) google aren't going to like it are they? they said something about not obscuring the logo or soemthing in the sign up bit.

    not sure what to do. doesn't look good or am i being pessemistic? thanks.

  4. #4
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,293
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    it's not possible to do google maps javascript submission/requests from the server is it?

  5. #5
    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 can access the Google Maps API geocoding service via the GClientGeocoder object. Use GClientGeocoder.getLatLng() to convert a string address into a GLatLng. This method takes as parameters a string address to convert, and a callback function to execute upon retrieval of the address. The callback function is necessary since geocoding involves sending a request to Google's servers and can take some time.
    You don't need to use a map to use Google's geocoding service. You give it an address, it gives you a latitude and longitude, and that's all the info you need to compute the distance yourself.

    http://code.google.com/apis/maps/doc...html#Geocoding

  6. #6
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,293
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    > You don't need to use a map to use Google's geocoding service. You give it an address, it gives you a latitude and longitude, and that's all the info you need to compute the distance yourself.

    ok, thanks Dan, i'll look into that tomorrow. cheers.

  7. #7
    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)
    Of course this is still a JavaScript interface so you're going to be doing that math client side... there are other geocoding services out there, but I don't know a free one that does UK addresses off the top of my head.

  8. #8
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,293
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    > there are other geocoding services out there, but I don't know a free one that does UK addresses off the top of my head

    the official postcode -> long & lat data is expensive. there are public versions but i'm not sure how complete/incomplete they are. one i looked at was seriously incomplete.

    > Of course this is still a JavaScript interface so you're going to be doing that math client side

    yes plus that skuppers my ajax plan (was going to do this so it updated page in place) and of course this renders the distance function javascript only where as before it was going to be possible via a normal form as well as ajax...

    two words: screen and scrape. the maps.google.com/.co.uk web page works without javascript when you type in two postcodes with the word 'to' between them -- it outputs the exact info i want and more. will i get thrown in prison and they key dissolved in acid? will my server get blocked? thus making all the hard work pointless? if i could get it working like this it'd be perfect. server side. by road. ajax. cachable. even road directions between two destinations available. this would be absolutely perfect, exactly what i want. except i can see it getting stopped/blocked ?

    thanks.
    Last edited by johnyboy; Sep 4, 2008 at 08:35.

  9. #9
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,293
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    would proxying be possible? vary requests from different ip addresses? i'm not expecting that many requests, but who knows. how would proxying be done? don't know anything about that.

  10. #10
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've been looking to do something just like this.

    Apologies if I'm asking a silly question and hijacking this post but I don't understand the following:

    Use GClientGeocoder.getLatLng() to convert a string address into a GLatLng.
    Will I need to download/install anything from google to access this function? Also, is there any functionality for PHP users.


    EDIT - Sorry, I've just found what I was after on the google website.

  11. #11
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,293
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    i'm writing screen scraping code...
    Last edited by johnyboy; Sep 4, 2008 at 08:35.

  12. #12
    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)
    It's against the terms, so you risk being blocked, but if you're not doing any volume they probably won't notice.

  13. #13
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,293
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    yup, fingers crossed. thanks.

  14. #14
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might find it easier to get the kml file and use regex to pull the distance from the response. There is a post about this in the Connecticut Google Groups. I'm too new to post a url but do a search for "parsing xml from a returned file" and the post is the first result.

  15. #15
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,293
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    thanks but i've already parsed the html. seems to work ok. always a bit dodgy parsing html because it only has to change a bit and it'll stop working. judging by the html of google's main search page, they don't mess with their html much. could be wrong, we'll see. i'm going to make my code email me if and when things start going wrong so i can fix it if pos.


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
  •