Skip to content

Instantly share code, notes, and snippets.

@adalbertopita
Created October 3, 2016 16:43
Show Gist options
  • Save adalbertopita/efa3b769f95d226746fa228c5fc3e8ed to your computer and use it in GitHub Desktop.
Save adalbertopita/efa3b769f95d226746fa228c5fc3e8ed to your computer and use it in GitHub Desktop.
function initMap() {
var startPlace = {lat: -12.9648489, lng: -38.4501421};
var infoWindow = new google.maps.InfoWindow;
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 11,
center: startPlace
});
var positions = [],
pos;
var geocoder = new google.maps.Geocoder();
$.ajax({
url: "address.json",
dataType: "json",
async: false,
success: function(data){
$.each(data, function( k, v ) {
getGeo(v);
});
}
});
function getGeo(v){
geocoder.geocode( { 'address': v.address }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
obj = {
lat: results[0].geometry.location.lat(),
lng: results[0].geometry.location.lng(),
marker: v
};
renderMap(obj);
}
});
}
function renderMap(obj){
var myLatlng = new google.maps.LatLng(obj.lat,obj.lng);
var marker = new google.maps.Marker({
map: map,
position: myLatlng
});
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent("<h2>"+obj.marker.title+"</h2><p>"+obj.marker.content+"</p>");
infoWindow.open(map,marker);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment