Created
April 12, 2019 00:41
-
-
Save MorningZ/553bf763069d84cae587bf9d557bb97a to your computer and use it in GitHub Desktop.
GMap plugin example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript"> | |
var generate_maps = function() { | |
// By Address | |
$("<selector>").each(function() { by_address(this, "<address to show>"); }); | |
// By Lat/Log | |
$("div[data-lat]").each(function() { by_latlong(this); }); | |
}; | |
var by_latlong = function(obj) { | |
///<summary>Assumes "obj" has data-lat and data.lng defined</summary> | |
var $map = $(this); var latlng = { "lat": $map.data("lat"), "lng": $map.data("lng") }; | |
$map.gmap3({ | |
center: latlng, | |
zoom: 6, | |
mapTypeId : google.maps.MapTypeId.ROADMAP | |
}) | |
.marker({ | |
position: latlng | |
}); | |
}; | |
var by_address = function(obj, addr) { | |
var $map = $(obj); | |
var geocoder = new google.maps.Geocoder(); | |
geocoder.geocode( | |
{ address: addr }, | |
function(results, status) { | |
if (status === 'OK') { | |
latlng = results[0].geometry.location; | |
console.log('Address "' + addr + '" --> ' + JSON.stringify(latlng)); | |
$map.gmap3({ | |
center: latlng, | |
zoom: 6, | |
mapTypeId : google.maps.MapTypeId.ROADMAP | |
}) | |
.marker({ | |
position: latlng, | |
icon: 'https://maps.google.com/mapfiles/marker_white.png' | |
}); | |
} | |
else { | |
console.log("Geocoding couldn't convert string address to lat/long points"); | |
} | |
} | |
); | |
}; | |
</script> | |
<script type="text/javascript" src="https://cdn.jsdelivr.net/gmap3/7.2.0/gmap3.min.js"></script> | |
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=<your Google Maps API key>&callback=generate_maps"></script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment