/ Published in: JavaScript
Init markup <a id=\"gmap\" rel=\"New York\" ></a>
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function showMap() { var geocoder, map, gmap = document.getElementById('gmap'), adr = gmap.getAttribute('rel'); geocoder = new google.maps.Geocoder(); geocoder.geocode( { 'address': adr}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { map = new google.maps.Map(document.getElementById("gmap"), { zoom: 16, center: results[0].geometry.location, mapTypeId: google.maps.MapTypeId.ROADMAP }); var marker = new google.maps.Marker({ map: map, title: adr, position: results[0].geometry.location }); var infowindow = new google.maps.InfoWindow({ content: adr }); google.maps.event.addListener(marker, 'click', function() { infowindow.open(map,marker); }); } else { alert("Nepodarilo sa nájsť lokalitu " + adr + ": " + status); } }); } function loadMap() { var script = document.createElement("script"); script.type = "text/javascript"; script.src = "http://maps.google.com/maps/api/js?sensor=false&language=sk&callback=showMap"; document.body.appendChild(script); } function chMap(adr) { var gmap = document.getElementById('gmap'); gmap.setAttribute('rel', adr); showMap(); } if (document.getElementById('gmap')) { window.onload = loadMap; }