Skip to content

Commit df5b021

Browse files
authored
Merge pull request #117 from Kentico/map-fix
Fix map component javascript
2 parents 7f069cf + c7bc91e commit df5b021

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

DancingGoat/Views/Cafes/Index.cshtml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@
2626
@section Scripts
2727
{
2828
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAVOq4C-rf7JVeHt6ws9vsf-KHIRpueASg"></script>
29-
<script src="~/Scripts/map.js"></script>
29+
<script src="~/js/map.js"></script>
3030
}

DancingGoat/Views/Contacts/Index.cshtml

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131

3232
@section Scripts {
3333
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAVOq4C-rf7JVeHt6ws9vsf-KHIRpueASg"></script>
34-
<script src="~/Scripts/map.js"></script>
34+
<script src="~/js/map.js"></script>
3535
}

DancingGoat/wwwroot/js/map.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
(function () {
2+
var geocoder;
3+
var map;
4+
var markers = {};
5+
var bounds = new google.maps.LatLngBounds();
6+
var mapElement = jQuery('div.js-map');
7+
8+
function initialize() {
9+
geocoder = new google.maps.Geocoder();
10+
var mapOptions = {
11+
mapTypeId: google.maps.MapTypeId.ROADMAP,
12+
mapTypeControl: true,
13+
scaleControl: true,
14+
panControl: true,
15+
streetViewControl: true,
16+
zoomControl: true,
17+
keyboardShortcuts: false,
18+
draggable: true,
19+
zoomControlOptions: {
20+
style: google.maps.ZoomControlStyle.DEFAULT
21+
}
22+
};
23+
map = new google.maps.Map(mapElement[0], mapOptions);
24+
jQuery('.js-scroll-to-map').each(function () {
25+
var address = jQuery(this).attr('data-address');
26+
addMarker(address);
27+
jQuery(this).click(function () {
28+
panToMarker(address);
29+
});
30+
});
31+
}
32+
33+
function panToMarker(address) {
34+
jQuery('html, body').animate({
35+
scrollTop: mapElement.offset().top
36+
}, 500);
37+
map.setZoom(17);
38+
map.panTo(markers[address].position);
39+
}
40+
41+
function addMarker(address) {
42+
geocoder.geocode({ 'address': address }, function (results, status) {
43+
if (status == google.maps.GeocoderStatus.OK) {
44+
var marker = new google.maps.Marker({
45+
map: map,
46+
position: results[0].geometry.location
47+
});
48+
markers[address] = marker;
49+
bounds.extend(marker.getPosition());
50+
map.fitBounds(bounds);
51+
} else {
52+
console.warn('Geocode was not successful for the following reason: ' + status);
53+
}
54+
});
55+
}
56+
57+
google.maps.event.addDomListener(window, 'load', initialize);
58+
}());

0 commit comments

Comments
 (0)