Kamis, 12 Desember 2019
PHP MAP MULTI MARKER DAN DIRECTION
Generate dahulu apikey:
https://console.cloud.google.com/apis/credentials/key/80ecde3a-5abc-4c54-af65-45d6446701a9?project=trusty-gradient-251202
Buat Kode sbb:
<?php
require_once"konmysqli.php";
$setjarak=100;
if(isset($_POST["Set"])){
$setjarak=$_POST["jarak"];
}
//AIzaSyBhSg6I2JvHNU_jLqIpvc5MEwc_xi3Ttvg
//AIzaSyCbkraMfyHLJxgO0XMLEWxz6xlLRmQpwYM
?>
<script type="text/javascript" src="//maps.googleapis.com/maps/api/js?key=AIzaSyBhSg6I2JvHNU_jLqIpvc5MEwc_xi3Ttvg&sensor=true"></script>
<script>
var map;
Number.prototype.toRad = function() {
return this * Math.PI / 180;
}
function initialize() {
var lat1=-6.353019;
var lon1=106.833086;
var myLatlng = new google.maps.LatLng(lat1, lon1);
var mapOptions = {
zoom: 10,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
var image = 'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png';
var location0 = new google.maps.LatLng(lat1,lon1);
var marker0 = new google.maps.Marker({
position: location0,
map: map,
icon: image
});
marker0.setTitle("Posisi Anda");
var content0="Jl. RS Fatmawati, Pangkalan Jati, Cilandak, Kota Jakarta Selatan, Daerah Khusus Ibukota Jakarta 16514";
attachSecretMessage(marker0, content0);
// or: new Array();
var arLat= [];
var arLon= [];
var arNama= [];
var arLok= [];
var arJarak= [];
var n=0;
var JM=200000;
<?php
$sql="select * from `tb_restoran`";
$arr=getData($conn,$sql);
$j=0;
if(isset($_POST["Set"])){ ?>
JM= <?php echo $_POST["jarak"];?>
<?php
}
else{
?>
var JM=200000;
<?php
}
foreach($arr as $d) {
$kd_sem=$d["id_restoran"];
$id_restoran0=$d["id_restoran"];
$nama_restoran=$d["nama_restoran"];
$alamat=$d["alamat"];
$telepon=$d["telepon"];
$lat=$d["latitude"];
$long=$d["longitude"];
$gambar=$d["gambar"];
?>
var lat2= <?php echo $lat;?>;
var lon2= <?php echo $long;?>;
var R = 6371; // km
//has a problem with the .toRad() method below.
var x1 = lat2-lat1;
var dLat = x1.toRad();
var x2 = lon2-lon1;
var dLon = x2.toRad();
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
<?php if(isset($_POST["Set"])){ ?>
if(d <= JM){
arLat[n]= <?php echo $lat;?>;
arLon[n]= <?php echo $long;?>;
arNama[n]= "<?php echo $nama_restoran;?>";
arLok[n]= "<?php echo "Hasil cari <=".$_POST["jarak"]."KM : $nama_restoran $alamat #<a href='index.php?mnu=direction&x=$lat&y=$long'>Go Direction</a>";?>";
arJarak[n]=d;
n=n+1;
}//<=
<?php
}//isset
else{
?>
arLat[n]= <?php echo $lat;?>;
arLon[n]= <?php echo $long;?>;
arNama[n]= "<?php echo $nama_restoran;?>";
arLok[n]= "<?php echo "Hasil Semua Data : $nama_restoran $alamat #<a href='index.php?mnu=direction&x=$lat&y=$long'>Go Direction</a>";?>";
arJarak[n]=d;
n=n+1;
<?php
}//else
}//foreach
?>
for (var i = 0; i < n; i++) {
var location = new google.maps.LatLng(arLat[i],arLon[i]);
var marker = new google.maps.Marker({
position: location,
map: map
});
marker.setTitle(arNama[i]);
var content=arNama[i]+"\n"+arLok[i]+" jarak :"+arJarak[i]+" KM";
attachSecretMessage(marker, content);
}
}
function attachSecretMessage(marker, lokasi) {
var infowindow = new google.maps.InfoWindow({content: lokasi,size: new google.maps.Size(50,50)});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<body>
<div id="map_canvas" style="width:100%;height:500px;"></div>
<br>
<div align="right">
<form action="" method="post">
Set jarak <input type="text" name="jarak" id="jarak" value="<?php echo $setjarak;?>">
<input type="submit" value="Set" name="Set">
</form>
</div>
</body>
<?php
function process($conn,$sql){
$s=false;
$conn->autocommit(FALSE);
try {
$rs = $conn->query($sql);
if($rs){
$conn->commit();
$last_inserted_id = $conn->insert_id;
$affected_rows = $conn->affected_rows;
$s=true;
}
}
catch (Exception $e) {
echo 'fail: ' . $e->getMessage();
$conn->rollback();
}
$conn->autocommit(TRUE);
return $s;
}
function getJum($conn,$sql){
$rs=$conn->query($sql);
$jum= $rs->num_rows;
$rs->free();
return $jum;
}
function getField($conn,$sql){
$rs=$conn->query($sql);
$rs->data_seek(0);
$d= $rs->fetch_assoc();
$rs->free();
return $d;
}
function getData($conn,$sql){
$rs=$conn->query($sql);
$rs->data_seek(0);
$arr = $rs->fetch_all(MYSQLI_ASSOC);
//foreach($arr as $row) {
// echo $row['nama_kelas'] . '*<br>';
//}
$rs->free();
return $arr;
}
?>
HASILNYA:
Untuk direction gunakan kode sbb:
OK Mudah yaaaa
Langganan:
Posting Komentar (Atom)
Selisih Waktu Berjalan
Berikut adalah fungsi PHP yang menghitung selisih hari antara tanggal saat ini dengan tanggal 2024-08-22 . Jika tanggal saat ini kurang dari...
-
<?php // <a href='download.php?file=$file' title='Download File'>Download Dokumen</a> $direktori = ...
-
Teman2 ...mungkin sering dapat kode random..... btw ini ada share sedikit ttg membuat soal random.....yang beda disini adalah sy menggunaka...
-
<?php $sql="select `id_admin` from `$tbadmin` order by `id_admin` desc"; $jum= getJum($conn,$sql); $kd="ADM"...
Tidak ada komentar:
Posting Komentar