Last active
July 1, 2019 17:32
-
-
Save jnritchie/7e4d3161f3be5fabfcfa7927b58e5f53 to your computer and use it in GitHub Desktop.
Ajax rendered map points with ajax data in info window.
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
| function initMap(){ | |
| var mia = { lat:25.778850, lng:-80.207486 }; | |
| var mapCanvas = document.getElementById('map'); | |
| var mapOptions = { | |
| center: mia, | |
| zoom: 12, | |
| mapTypeId: google.maps.MapTypeId.ROADMAP, | |
| disableDefaultUI: true, | |
| styles: | |
| [ | |
| { | |
| "stylers": [ | |
| { | |
| "hue": "#2c3e50" | |
| }, | |
| { | |
| "saturation": 250 | |
| } | |
| ] | |
| }, | |
| { | |
| "featureType": "road", | |
| "elementType": "geometry", | |
| "stylers": [ | |
| { | |
| "lightness": 50 | |
| }, | |
| { | |
| "visibility": "simplified" | |
| } | |
| ] | |
| }, | |
| { | |
| "featureType": "road", | |
| "elementType": "labels", | |
| "stylers": [ | |
| { | |
| "visibility": "off" | |
| } | |
| ] | |
| } | |
| ] | |
| } | |
| // The map, centered at user select | |
| var map = new google.maps.Map(mapCanvas, mapOptions); | |
| //console.log("map is on?") | |
| // The marker, positioned at Nassau | |
| //var marker = new google.maps.Marker({position: miami, map: map}); | |
| const pointdata = "[data-url]" | |
| $.ajax({ | |
| url: pointdata, | |
| type: "GET", | |
| success: function(data){ | |
| //console.log(data); | |
| var datalen = data.shops.length; | |
| for ( var i = 0; i < datalen; i++) { | |
| var points = data.shops[i], | |
| latLng = new google.maps.LatLng(points.lat, points.lng); | |
| console.log(points); | |
| var sContent = "<div style='float:left'><div class='point-img' style='background-image:url("+points.image+");width:100px;height:100px;'></div></div><div class='info-right'><span class='point-name'>"+points.name+"</span><br><p class='point-blurb'>"+points.blurb+"</p><a href='/shops/"+points.url_finder+"'>View Shop</a></div>"; | |
| var infoWindow = new google.maps.InfoWindow({ content: sContent }); | |
| var marker = new google.maps.Marker({ | |
| position:latLng, | |
| map: map, | |
| icon: "/imgs/pin.png", | |
| title: points.name, | |
| info: sContent | |
| }); | |
| (function(marker, data) { | |
| // Attaching a click event to the current marker | |
| google.maps.event.addListener(marker, "click", function(e) { | |
| infoWindow.setContent(this.info); | |
| infoWindow.open(map, this); | |
| }); | |
| })(marker, data); | |
| } | |
| }, | |
| error: function(error){ | |
| console.log("Something is wrong"+error); | |
| } | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment