Guest User

Geolocation

a guest
Jul 14th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
  5.   <title>Google Maps Multiple Markers</title>
  6.   <script src="http://maps.google.com/maps/api/js?sensor=false"></script>
  7.   <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.1.min.js"></script>
  8. </head>
  9. <body>
  10.   <div id="map" style="width: 500px; height: 400px;"></div>
  11.   <script type="text/javascript">
  12.     var locations = [
  13.       ['1. adresse', 'Søndergade 58, 7850 Stoholm'],
  14.       ['2. adresse', 'Kokholm 1C, 6000 Kolding']
  15.     ];
  16.    
  17.  
  18.     var map = new google.maps.Map(document.getElementById('map'), {
  19.       zoom: 6,
  20.       center: new google.maps.LatLng(56, 10),
  21.       mapTypeId: 'terrain'
  22.     });
  23.    
  24.    
  25.     var infowindow = new google.maps.InfoWindow();
  26.     var marker, i;
  27.     var markers = new Array();
  28.     var counter = locations.length;
  29.     //alert(counter);
  30.     for (i = 0; i < counter; i++) {  
  31.         $.getJSON('http://maps.googleapis.com/maps/api/geocode/json?address='+locations[i]+'&sensor=false', null, function (data) {
  32.         var p = data.results[0].geometry.location
  33.         var latlng = new google.maps.LatLng(p.lat, p.lng);
  34.         alert(latlng);
  35.         marker = new google.maps.Marker({
  36.             position: latlng,
  37.             map: map
  38.         });
  39.         });
  40.         markers.push(marker);
  41.         google.maps.event.addListener(marker, 'click', (function(marker, i) {
  42.  
  43.         return function() {
  44.           infowindow.setContent(locations[i][0]);
  45.           infowindow.open(map, marker);
  46.         }
  47.       })(marker, i));
  48.     }
  49.     function AutoCenter() {
  50.       //  Create a new viewpoint bound
  51.       var bounds = new google.maps.LatLngBounds();
  52.       //  Go through each...
  53.       $.each(markers, function (index, marker) {
  54.       bounds.extend(marker.position);
  55.       });
  56.       //  Fit these bounds to the map
  57.       map.fitBounds(bounds);
  58.     }
  59.     AutoCenter();
  60.   </script>
  61. </body>
  62. </html>
Advertisement
Add Comment
Please, Sign In to add comment