1

Questions:


Hi,

I want to embed a dynamic google map in my website by IP address. I got the lat/long data using IPlocation tools. I could not yet connect the lat/log data from IPlocation to google map java codes. I m not sure how to format/define/put latitude and longitude in load map function. here are things I tried and did not work yet:

  • I tried , into javascript code,
  • I also tried manually putting values for $latitude and $longitude.
  • I tried directly putting 'ip2location_latitude()' and 'ip2location_longitude()' in java codes.

eventually I need to get a location history based on IP and record the location data to a database.

Thanks in advance

here is the summary of my code:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> <!-- Google map API v3 -->

<?php
$latitude = 'ip2location_latitude()'; //------------ IP latitude
$longitude = 'ip2location_longitude()'; //------------ IP latitude ip2location_longitude() 
?>

<!-- Start loading canvas google map -->

<script type="text/javascript">
function loadmap () { 
var latlng = new google.maps.LatLng($latitude,$longitude);
var option = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), option);
createMarker(latitude,logitude, 'lol');
downloadUrl("airport.xml", function(data) {
var xml = xmlParse(data);
var markers = xml.documentElement.getElementsByTagName("marker");
document.getElementById("test").innerHTML = markers.length;
for (var i = 0; i < markers.length; i++) {
var info = markers[i].getAttribute("info");
var lat = parseFloat(markers[i].getAttribute("lat"));
var lon = parseFloat(markers[i].getAttribute("lng"));

createMarker(lat, lon, info);
bindInfoWindow(marker, map, infoWindow, html);
}
});
}
function createMarker(lat, lon, info){
var myLatlng = new google.maps.LatLng($latitude,$longitude);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title:"My location",
icon: "http://google-maps-icons.googlecode.com/files/airport.png"
});
}
</script>
<!-- End Geting map via latitude and longitude-->

<!-- Start calling load map function to load the map -->
<body onload="loadmap ()">
<div id="map_canvas" style="width:400px; height:300px"></div>
<div id="test" style="width:50%; height:50%"></div>
<!-- End calling load map function to load the map -->
4

1 に答える 1

2

You forgot to use echo/print to print the values of the PHP-variables.

Inside createMarker you should refer to the provided arguments(lat,lon) instead of $latitude/$longitude

Fixed version:

<body onload="loadmap ()">
<script  src="http://maps.google.com/maps/api/js?sensor=false">
   </script>
<script src="http://www.iplocationtools.com/iplocationtools.js?key=YOURKEY">
   </script>
<?php
$latitude = 'ip2location_latitude()'; //------------ IP latitude
$longitude = 'ip2location_longitude()'; //------------ IP latitude ip2location_longitude() 
?>
<script type="text/javascript">
function loadmap () { 
  var latlng = new google.maps.LatLng(<?php echo $latitude;?>,<?php echo $longitude;?>);
  var option = {
                zoom: 8,
                center: latlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
               };
  map = new google.maps.Map(document.getElementById("map_canvas"), option);
  createMarker(<?php echo $latitude;?>,<?php echo $longitude;?>, 'lol');

}
function createMarker(lat, lon, info){
var myLatlng = new google.maps.LatLng(lat,lon);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title:"My location",
icon: "http://google-maps-icons.googlecode.com/files/airport.png"
});
}
</script>
<div id="map_canvas" style="width:400px; height:300px"></div>
<div id="test" style="width:50%; height:50%"></div>
</body> 

But however, this service isn't very accurate, for me it shows a location 400km away from my current place. I would suggest to use browser-implemented geolocation.

于 2012-06-14T10:03:15.293 に答える