我有脚本来计算 2 个坐标之间的距离,这里:
(function() {
window.onload = function() {
var map;
//Parameter Google maps
var options = {
zoom: 10, //level zoom
//posisi tengah peta
center: new google.maps.LatLng(-7.695001, 113.996544),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
// Buat peta di menu MAP
var map = new google.maps.Map(document.getElementById('peta'), options);
var locations = [
<?php while($rs = mysql_fetch_array($a_peta, MYSQL_ASSOC)) { ?>
['<?php echo $rs['nama'] ?>', <?php echo $rs['latitude'] ?>, <?php echo $rs['longitude'] ?>],
<?php } ?>
];
var infowindow = new google.maps.InfoWindow();
var marker, i;
/* kode untuk menampilkan banyak marker */
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
fmap: fmap,
icon: 'pics/ico.jpg'
});
var circle = new google.maps.Circle({
map: map,
radius: 10000,
fillColor: '#AA0000'
});
circle.bindTo('center', marker, 'position');
/* menambahkan event clik untuk menampikan
infowindows dengan isi sesuai dengan
marker yang di klik */
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
};
})();
您可以看到代码是 php 和 javascript 之间的混合。我想将它移动到一个完整的 javascipt 文件中,但不知道该怎么做。
这是关于 '$a_peta' 的脚本:
<?php
include 'connect.php';
$conn = mysql_connect($dbhost,$dbuser,$dbpass);
$db = mysql_select_db($dbname);
if($db){
$sql="select id, nama, latitude, longitude from jarak order by id";
$a_peta = mysql_query($sql);
}
else {
print "Database NOT Found ";
mysql_close($conn);
}
?>