谷歌地图通过抓取他们的IP地址显示访问者的位置

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 ??

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

According to this post http://www.techsirius.com/2014/07/visitor-realtime-geo-location-in-google-map.html it seems it is possible.

Basically it is fetching database record for country, state, and city based on given IP. It seems that database IP record is stored in IPlong format that is why it is possible to store that much IP record in one database.

After that all depended on Google Geocoding API and Google Maps JS v3 API.

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=true&key=<?=$google_api_key?>"></script>
<script type="text/javascript">
$(document).ready(function(){
 var option = {
center: new google.maps.LatLng(<?=$lat?>, <?=$lng?>),
   zoom: 5
 };
 var map = new google.maps.Map(document.getElementById("visitor_map"), option);
 var marker = new google.maps.Marker({
    position: new google.maps.LatLng(<?=$lat?>, <?=$lng?>),
    map: map
 });
});
</script>