Foamcow

Google Maps multiple pins example

Jul 12th, 2012
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.    
  2.         var directionsDisplay;
  3.         var directionsService = new google.maps.DirectionsService();
  4.         var map;
  5.  
  6.         function initialize() {
  7.           directionsDisplay = new google.maps.DirectionsRenderer();
  8.          
  9.         // here we set up some lat/long co-ordinates for the places we want to mark
  10.             var postlip = new google.maps.LatLng(51.939609,-2.002945);
  11.             var whitehart = new google.maps.LatLng(51.953227, -1.964589);
  12.         var risingsun = new google.maps.LatLng(51.938517, -2.029652);
  13.          
  14.     // set the map options, in this case we are centring the map on co-ords for 'postlip' (see above)
  15.           var myOptions = {
  16.             center: postlip,
  17.             zoom: 12,
  18.             mapTypeId: google.maps.MapTypeId.ROADMAP
  19.           };
  20.              
  21.     // create the map object
  22.           var map = new google.maps.Map(document.getElementById("map_canvas"),
  23.               myOptions);
  24.              
  25.  
  26.     // Let's make some markers...
  27.     // each marker has a position, the name of the map it's on ('map'), a title and an icon.
  28.     // I've also added some HTML content to display in the info windows that show when you click
  29.            // Postlip
  30.           var postlipMarker = new google.maps.Marker({
  31.             position: postlip,
  32.             map: map,
  33.             title:"Postlip Hall",
  34.             icon:"/img/wedding.png"
  35.           });
  36.  
  37.           var postlipContent = '<div id="content">'+
  38.             '<h2 id="firstHeading" class="firstHeading">Postlip Hall</h2>'+
  39.             '<div id="bodyContent">'+
  40.             '<p><img src="/img/barn.jpg" alt="Barn picture" style="float:left;margin-right:15px;">Enter via the long driveway and bear left at the fork. Then follow the signs to be directed to the car park</p>' +
  41.             '</div>'+
  42.             '</div>';
  43.  
  44.     // create the info window
  45.           var postlipInfo = new google.maps.InfoWindow({
  46.             content: postlipContent
  47.           });
  48.  
  49.     // make the info window popup when you click the marker
  50.     // we add a listener, tell it what to listen for and what to do when it triggers
  51.           google.maps.event.addListener(postlipMarker, 'click', function() {
  52.              postlipInfo.open(map,postlipMarker);
  53.            });
  54.          
  55.           // White Hart
  56.          
  57.           var whitehartMarker = new google.maps.Marker({
  58.             position: whitehart,
  59.             map: map,
  60.             title: "The White Hart Inn, Winchcombe",
  61.             icon:"/img/hotel.png"
  62.           })
  63.          
  64.          
  65.           var whitehartContent = '<div id="content">'+
  66.             '<h2 id="firstHeading" class="firstHeading">The White Hart, Winchcombe</h2>'+
  67.             '<div id="bodyContent">'+
  68.             '<p></p>' +
  69.             '</div>'+
  70.             '</div>';
  71.          
  72.            
  73.              var whitehartInfo = new google.maps.InfoWindow({
  74.                 content: whitehartContent
  75.               });
  76.    
  77.             google.maps.event.addListener(whitehartMarker, 'click', function() {
  78.               whitehartInfo.open(map,whitehartMarker);
  79.             });
  80.  
  81.  
  82.            
  83.           // Rising Sun
  84.  
  85.           var risingsunMarker = new google.maps.Marker({
  86.             position: risingsun,
  87.             map: map,
  88.             title: "The Rising Sun Hotel, Cleeve Hill",
  89.             icon:"/img/hotel.png"
  90.           })
  91.  
  92.  
  93.           var risingsunContent = '<div id="content">'+
  94.             '<h2 id="firstHeading" class="firstHeading">The Rising Sun, Cleeve Hill</h2>'+
  95.             '<div id="bodyContent">'+
  96.             '<p><a href="http://www.oldenglishinns.co.uk/cheltenham/">Hotel website</a></p>' +
  97.             '</div>'+
  98.             '</div>';
  99.  
  100.  
  101.              var risingsunInfo = new google.maps.InfoWindow({
  102.                 content: risingsunContent
  103.               });
  104.  
  105.             google.maps.event.addListener(risingsunMarker, 'click', function() {
  106.               risingsunInfo.open(map,risingsunMarker);
  107.             });
  108.            
  109.           directionsDisplay.setMap(map);
  110.         }
  111.          
  112.          
  113.     // this is all directions stuff
  114.     // it will show directions from a given point to 'postlip' (I used the co-ords rather than the variable though)
  115.  
  116.         function calcRoute(lat,lon) {
  117.          var start = new google.maps.LatLng(lat,lon);
  118.          var end = new google.maps.LatLng(51.939609,-2.002945);
  119.           var request = {
  120.             origin: start,
  121.             destination: end,
  122.             travelMode: google.maps.TravelMode.DRIVING
  123.           };
  124.           directionsService.route(request, function(result, status) {
  125.             if (status == google.maps.DirectionsStatus.OK) {
  126.               directionsDisplay.setDirections(result);
  127.             }
  128.           });
  129.         }
Add Comment
Please, Sign In to add comment