SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot fredep57's Avatar
    Join Date
    Aug 2009
    Location
    Pacific Northwest
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Google Maps API help

    OK, I am trying to set up a page that a user can enter in an address and that information will then "generate" a call to the google maps API to make an iframe for display in another page.

    If found the following online but it does not get any information
    PHP Code:
    <?php

    // First, setup the variables you will use on your <iframe> code
    // Your Iframe will need a Width and Height set
    // as well as the address you plan to Iframe
    // Don't forget to get a Google Maps API key

    $latitude '';
    $longitude '';
    $iframe_width '400px';
    $iframe_height '400px';
    $address '12215 East Sprague Avenue, Spokane Valley, WA 99206';
    echo 
    'Addres to find: ' $address '<br>';
    $address urlencode($address);
    $key "AIzaSyDwDLLJCtmZCJaquWBWlKecSy46i_5StGc";
    $url "http://maps.google.com/maps/geo?q=".$address."&output=json&key=".$key;
    echo 
    'URL: ' $url '<br>';
    $ch curl_init();

    curl_setopt($chCURLOPT_URL$url);
    curl_setopt($chCURLOPT_HEADER,0);
    curl_setopt($chCURLOPT_USERAGENT$_SERVER["HTTP_USER_AGENT"]);
    // Comment out the line below if you receive an error on certain hosts that have security restrictions
    curl_setopt($chCURLOPT_FOLLOWLOCATION1);
    curl_setopt($chCURLOPT_RETURNTRANSFER1);

    $data curl_exec($ch);
    curl_close($ch);

    $geo_json json_decode($datatrue);

    // Uncomment the line below to see the full output from the API request
    // var_dump($geo_json);

    // If the Json request was successful (status 200) proceed
    if ($geo_json['Status']['code'] == '200') {

    $latitude $geo_json['Placemark'][0]['Point']['coordinates'][0];
    $longitude $geo_json['Placemark'][0]['Point']['coordinates'][1]; ?>

    <iframe width="<?php echo $iframe_width?>" height="<?php echo $iframe_height?>" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="

    http://maps.google.com/maps

    ?f=q
    &amp;source=s_q
    &amp;hl=en
    &amp;geocode=
    &amp;q=<?php echo $address?>
    &amp;aq=0
    &amp;ie=UTF8
    &amp;hq=
    &amp;hnear=<?php echo $address?>
    &amp;t=m
    &amp;ll=<?php echo $longitude?>,<?php echo $latitude?>
    &amp;z=12
    &amp;iwloc=
    &amp;output=embed"></iframe>

    <?php

    } else { echo "<p>No Address Available</p>";}

    ?>
    Putting this on my site, I get the following information:
    Addres to find: 12215 East Sprague Avenue, Spokane Valley, WA 99206
    URL: http://maps.google.com/maps/geo?q=12...KecSy46i_5StGc

    No Address Available
    Any idea why the "No Address Available" comes back?

    E

  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,440
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    Hi,

    I tried clicking the link that your code generated, and I got a message saying that Google couldn't process the request right now. I tried removing the API key from the URL, and then I got a response saying that v2 of the Google Maps API was discontinued last year. You'll need to alter the code to work with v3 of the API.

    There's a guide for moving from v2 to v3, but it's aimed at Javascript users, so I'm not sure how helpful it's going to be.

  3. #3
    SitePoint Zealot fredep57's Avatar
    Join Date
    Aug 2009
    Location
    Pacific Northwest
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks but want this work with PHP - not a fan of javascript since I got hijacked via a javascript once.

    Hope someone else can tell me how/what to modify to use v3 and not the v2 stuff. I found some information about v3 but was very confused as to how to change this.

    E

  4. #4
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,440
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    I've managed to modify the script to work with v3 of the API. It no longer needs an API key to work:

    PHP Code:
    <?php

    $iframe_width 
    '400px'
    $iframe_height '400px'
    $address '12215 East Sprague Avenue, Spokane Valley, WA 99206'

    echo 
    'Address to find: ' $address '<br>'

    $address urlencode($address); 
    $url "http://maps.googleapis.com/maps/api/geocode/json?address=$address&sensor=false";

    echo 
    'URL: ' $url '<br>';

    $ch curl_init(); 
    curl_setopt($chCURLOPT_URL$url); 
    curl_setopt($chCURLOPT_HEADER,0); 
    curl_setopt($chCURLOPT_USERAGENT$_SERVER["HTTP_USER_AGENT"]); 
    curl_setopt($chCURLOPT_FOLLOWLOCATION1); 
    curl_setopt($chCURLOPT_RETURNTRANSFER1); 

    $data curl_exec($ch); 
    curl_close($ch); 

    $geo_json json_decode($datatrue); 

    if (
    $geo_json['status'] == 'OK') { 

        
    $latitude $geo_json['results']['geometry']['location']['lat']; 
        
    $longitude $geo_json['results']['geometry']['location']['lng'];

        
    $map_params = array(
            
    'f' => 'q',
            
    'source' => 's_q',
            
    'hl' => 'en'
            
    'q' => $address,
            
    'aq' => 0,
            
    'ie' => 'UTF8',
            
    'hnear' => $address,
            
    't' => 'm',
            
    'll' => $longitude .','$latitude
            
    'z' => 12,
            
    'output' => 'embed'
        
    );

        
    $url 'http://maps.google.com/maps?' http_build_query($map_params);
    ?> 

    <iframe width="<?php echo $iframe_width?>" height="<?php echo $iframe_height?>" frameborder="0" 
    scrolling="no" marginheight="0" marginwidth="0" src="<?php echo $url ?>"></iframe> 

    <?php 

    } else {
        echo 
    "<p>No Address Available</p>";
    }


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
  •