Aplikasi web yang akan dibuat berguna untuk menampilkan lokasi latitude longitude pengunjung web sehingga tampak di peta Google Map. Kode program halaman HTML dan Javascriptnya sebagai berikut, dalam hal ini penulis beri nama google-map-get-location.html :
<script src="http://maps.google.com/maps/api/js?sensor=false&language=id" type="text/javascript"></script>
<script type="text/javascript">
var geocoder = new google.maps.Geocoder();
// http://cariprogram.blogspot.com
// nuramijaya@gmail.com
function initialize() {
var latLng = new google.maps.LatLng(-7.8, 110.3666667);
var map = new google.maps.Map(document.getElementById('mapCanvas'), {
zoom: 12,
center: latLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker = new google.maps.Marker({
position: latLng,
title: 'Ambarrukmo Plaza Yogyakarta',
map: map,
draggable: true
});
// Try HTML5 geolocation
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
var marker1 = new google.maps.Marker({
position : pos,
animation:google.maps.Animation.BOUNCE,
title : 'lokasi',
//icon : 'assets/icon.png',
map : map,
draggable : true
});
map.setCenter(pos);
/*google.maps.event.addListener(marker1, 'drag', function() {
updateMarkerPosition(marker1.getPosition());
});
updateMarkerPosition(marker1.getPosition())*/
}, function() {
//=handleNoGeolocation(true);
var marker1 = new google.maps.Marker({
position : latLng,
animation:google.maps.Animation.BOUNCE,
title : 'lokasi',
//icon : 'assets/icon.png',
map : map,
draggable : true
});
/*google.maps.event.addListener(marker1, 'drag', function() {
updateMarkerPosition(marker1.getPosition());
});
updateMarkerPosition(marker1.getPosition())*/
});
} else {
// Browser doesn't support Geolocation
//=handleNoGeolocation(false);
var marker1 = new google.maps.Marker({
position : latLng,
animation:google.maps.Animation.BOUNCE,
title : 'lokasi',
//icon : 'assets/icon.png',
map : map,
draggable : true
});
/*google.maps.event.addListener(marker1, 'drag', function() {
updateMarkerPosition(marker1.getPosition());
});
updateMarkerPosition(marker1.getPosition())*/
}
}
// Onload handler to fire off the app.
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<style>
#mapCanvas {
width: 500px;
height: 300px;
float: left;
}
</style>
<br />
<div id="mapCanvas"></div>
File project selengkapnya dapat anda download disini. Jika bingung cara download, silakan lihat caranya disini.
Semoga berguna :)
Mau tanya mas, cara mendapatkan longitude dan latitude secara otomatis dari user yang menggunakan aplikasi / web gimana caranya ya ?
BalasHapusDi contoh atas kan kira harus mengisinya secara manual.
var x;
Hapusfunction getLocation(){
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(showPosition);
}else{
x="Ga Support";
alert(x);
}
}
function showPosition(position){
x=position.coords.latitude+", "+position.coords.longitude;
alert(x);
}
getLocation();
var x;
BalasHapusfunction getLocation(){
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(showPosition);
}else{
x="Ga Support";
alert(x);
}
}
function showPosition(position){
x=position.coords.latitude+", "+position.coords.longitude;
alert(x);
}
getLocation();