views:

1493

answers:

1

Hi there,

I'm trying to replicate the following exemple :

http://www.leigeber.com/2008/04/map-your-users-using-the-google-maps-api-and-php/

How to integrate this PHP source code with the following Java script (replacing lat/lgt with $latitude/$longitude above ??? Looks easy but I'm a bit lost in mixing PHP and Javascript together ! ;-(

<script type="text/javascript"> 
    function initialize() {
      var map;
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));

     var zoomlevel = 9;
     var lat= 50.62829913465776;
     var lgt= 4.650295972824097;
        map.setCenter(new GLatLng(lat, lgt), zoomlevel);

     // add a marker
     map.openInfoWindow(map.getCenter(), document.createTextNode ("Center of the map at (lat - lgt)= ("+lat+" - "+lgt+") ! "));

     // Large Map Default UI   
     map.setUIToDefault();

     // Map type
     map.setMapType(G_PHYSICAL_MAP);  
      }
    }
    </script>

<body onload="initialize()" onunload="GUnload()">    
     <div id="map_canvas" style="width: 1000px; height: 600px"></div> 
</body>

Thanks in advance for your help.

Hub

ps: is this the best way to do it ? is it good to use hostip.info or should I use another one ??

A: 

Should be fairly easy. Once you've fetched the $latitude and $longitude according to the linked example, you can use PHP to echo those values out to your Javascript code.

<script type="text/javascript"> 
function initialize() {
  var map;
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map_canvas"));

    var zoomlevel = 9;
    var lat= <?php echo $latitude ?>;
    var lgt= <?php echo $longitude ?>;
    map.setCenter(new GLatLng(lat, lgt), zoomlevel);
    .........
    ...............
</script>

That should get you going for the script mixing part. Remember, you've to use PHP to fetch the lat/long BEFORE you attempt to output the values to your JS.

In response to your queries, here's the general flow of code (say, in the file index.php):

<?php
// Code from the blog
$ip = $_SERVER['REMOTE_ADDR'];
.....
.......
$latitude = $res[2];
$longitude = $res[3];
?>
<html>
    <head>
        <script type="text/javascript"> 
        function initialize() {
          var map;
          if (GBrowserIsCompatible()) {
            map = new GMap2(document.getElementById("map_canvas"));

            var zoomlevel = 9;
            var lat= <?php echo $latitude ?>;
            var lgt= <?php echo $longitude ?>;
            map.setCenter(new GLatLng(lat, lgt), zoomlevel);
            .........
            ...............
        }
        </script>
    </head>
    <body onload="initialize()" onunload="GUnload()">
    ......
    </body>
</html>

See if this helps you :)

Cheers, m^e

miCRoSCoPiC_eaRthLinG
Thanks a lot. referring to your advice to "use PHP to fetch the lat/long BEFORE you attempt to output the values to your JS." ... that's exactly my problem in fact... How do you do this in fact ? (sorry for this really beginner question ...)the function initialize() is called on "body onload" (I'm following Google advice on this):<body onload="initialize()" onunload="GUnload()"> so that the PHP script runs after the JS, no ?I've tried and here's the result : http://www.disy.be/gmap4.php
Hubert
Resolved: I moved the function initialize()outside of the <head> </head> (I can do that I guess? Is that fine programing ?)so that I can call first the PHP script, then function initialize() when doing <body onload="initialize()" onunload="GUnload()">Here it is : http://www.disy.be/gmap5.phpThanks for your help.Hub
Hubert
Yes of course. You can have the initialize() function anywhere within the HTML BODY tag too. Won't affect the functionality in any way. I'm attaching some more code stubs to my reply, which shows the general flow of code and allows you to leave the init function in the HEAD.
miCRoSCoPiC_eaRthLinG
Thanks a lot again. This is perfect. Any way I can vote you up to thank you for this complete answer? (I have to wait for 15 reputation I saw)?
Hubert
No problem at all :) Glad to be of help. Have you managed to get a working example up? I visited gmap5.php, but couldn't see any maps... but then again, my IP wasn't in the hostip.info database - I listed it just now. So quite likely, my IP didn't fetch any lat/long to generate the map properly.
miCRoSCoPiC_eaRthLinG
Yup! Working now... once my IP was included in their DB. At least now I can see the country flag when I visit gmap5.php. However, the map is still missing and upon viewing your source, I see empty values for the lat / long. Have you included that second block of script (mentioned in the blog) that utilizes Google Maps API to fetch latitude-longitude from the City / Country information that hostip provides?
miCRoSCoPiC_eaRthLinG
Yes I have included that second block of script that utilizes Google Maps API to fetch latitude-longitude.I've now added the following:// default latitude $default_longitude = -122.1419;and after every $value = 'N/A';I've added :$latitude = default_latitude;$longitude = default_longitude;to return a default value.Do you see a map now ?
Hubert
Yep I do! Good job :)
miCRoSCoPiC_eaRthLinG