Advertisement
Venciity

Map.cshtml

Jul 29th, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.00 KB | None | 0 0
  1.  
  2. @using PromoSquad.WebApp.Models.ViewModels
  3. @model IEnumerable<JobPostMapViewModel>
  4.  
  5. <input type="text" id="google-maps-location" placeholder="search" class="map-search" />
  6. <div id="map-canvas" style="height:500px"></div>
  7.  
  8. @section scripts{
  9.     <script>
  10.         function initMap() {
  11.             var infoWindow = new google.maps.InfoWindow();
  12.             var pos = {
  13.                 lat: 51.5287352,
  14.                 lng: -0.116043
  15.             };
  16.  
  17.             var jobPosts = [];
  18.             @foreach (var item in Model)
  19.             {
  20.                 <text>
  21.                     jobPosts.push({
  22.                         lat: @item.Latitude,
  23.                         lng: @item.Longitude,
  24.                         title: "@item.Title",
  25.                         description: "@item.Description",
  26.                         detailsUrl: "@AppSettings.Url" + "/Jobs/Details/" + "@item.Id"
  27.                     })
  28.                 </text>
  29.             }
  30.  
  31.             var markers = [];
  32.             var map = new google.maps.Map(document.getElementById('map-canvas'), {
  33.                 center: { lat: pos.lat, lng: pos.lng },
  34.                 zoom: 13
  35.             });
  36.  
  37.  
  38.             function drop() {
  39.                 clearMarkers();
  40.                 for (var i = 0; i < jobPosts.length; i++) {
  41.                     addmarkersWithTiemout(jobPosts[i], 0);
  42.                 }
  43.             }
  44.  
  45.             function addmarkersWithTiemout(jobPost, timeout) {
  46.                 window.setTimeout(function () {
  47.                     var newMarker= new google.maps.Marker({
  48.                         position: { lat: jobPost.lat, lng: jobPost.lng },
  49.                         map: map,
  50.                         animation: google.maps.Animation.DROP,
  51.                         info: new google.maps.InfoWindow({
  52.                             content: "<a href=\"" + jobPost.detailsUrl +"\">" + jobPost.title +"</a>" +
  53.                                 "<br />" + jobPost.description
  54.                         })
  55.                     });
  56.  
  57.                     markers.push(newMarker);
  58.  
  59.                     google.maps.event.addListener(newMarker, 'click', function() {
  60.                         newMarker.info.open(map, newMarker);
  61.                     });
  62.  
  63.                 }, timeout);
  64.             }
  65.  
  66.             function clearMarkers() {
  67.                 for (i = 0; i < markers.length; i++) {
  68.                     markers[i].setMap(null);
  69.                 }
  70.                 markers = [];
  71.             }
  72.  
  73.             drop();
  74.  
  75.             if (navigator.geolocation) {
  76.                 navigator.geolocation.getCurrentPosition(function (position) {
  77.                     pos.lat = position.coords.latitude;
  78.                     pos.lng = position.coords.longitude;
  79.                     $("#Location").val(pos.lat + ", " + pos.lng)
  80.                     map.setCenter(pos);
  81.                 });
  82.             }
  83.  
  84.             var input = (document.getElementById('google-maps-location'));
  85.             var autocomplete = new google.maps.places.Autocomplete(input);
  86.             autocomplete.bindTo('bounds', map);
  87.  
  88.             autocomplete.addListener('place_changed', function () {
  89.                 var place = autocomplete.getPlace();
  90.                 if (!place.geometry) {
  91.                     window.alert("Autocomplete's returned place contains no geometry");
  92.                     return;
  93.                 }
  94.  
  95.                 $("#Location").val(place.geometry.location.lat() + ", " + place.geometry.location.lng())
  96.  
  97.                 // If the place has a geometry, then present it on a map.
  98.                 if (place.geometry.viewport) {
  99.                     map.fitBounds(place.geometry.viewport);
  100.                 } else {
  101.                     map.setCenter(place.geometry.location);
  102.                     map.setZoom(17);  // Why 17? Because it looks good.
  103.                 }
  104.             });
  105.         }
  106.     </script>
  107.     <script src="https://maps.googleapis.com/maps/api/js?key=@AppKeys.GoogleMapsKey&libraries=places&callback=initMap" type="text/javascript"></script>
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement