Google Maps API V3 Javascript Full Example


/ Published in: JavaScript
Save to your folder(s)

This javascript will create a complex google javascript map with multiple locations selected marked. The map will be centered in the middle and zoomed so that all point can be seen. It also stylizes the map making it mostly desaturated with bits of orange. More explanation as to how to set this up and change it at the url.


Copy this code and paste it in your HTML
  1. window.onload = function(){
  2. // Define addresses, define varible for the markers, define marker counter
  3. var addrs = ['219 4th Ave N Seattle Wa 98109','200 2nd Avenue North Seattle Wa 98109','325 5th Ave N Seattle Wa 98109'];
  4. var markers = [];
  5. var marker_num = 0;
  6. // Process each address and get it's lat long
  7. var geocoder = new google.maps.Geocoder();
  8. var center = new google.maps.LatLngBounds();
  9. for(k=0;k<addrs.length;k++){
  10. var addr = addrs[k];
  11. geocoder.geocode({'address':addr},function(res,stat){
  12. if(stat==google.maps.GeocoderStatus.OK){
  13. // add the point to the LatLngBounds to get center point, add point to markers
  14. center.extend(res[0].geometry.location);
  15. markers[marker_num]=res[0].geometry.location;
  16. marker_num++;
  17. // actually display the map and markers, this is only done the last time
  18. if(k==addrs.length){
  19. // actually display the map
  20. var map = new google.maps.Map(document.getElementById("the_map"),{
  21. 'center': center.getCenter(),
  22. 'zoom': 14,
  23. 'streetViewControl': false,
  24. 'mapTypeId': google.maps.MapTypeId.ROADMAP, // Also try TERRAIN!
  25. 'noClear':true,
  26. });
  27. // go through the markers and display them
  28. for(p=0;p<markers.length;p++){
  29. var mark = markers[p];
  30. new google.maps.Marker({
  31. 'icon':'mapmarker.png',
  32. //'animation':google.maps.Animation.DROP, // This lags my computer somethin feirce
  33. 'title':addrs[p],
  34. 'map': map,
  35. 'position': mark,
  36. })
  37. }
  38. // zoom map so all points can be seen
  39. map.fitBounds(center)
  40. // Styleize the map, doing this with the initial map options never seems to work
  41. map.setOptions({styles:[
  42. {
  43. featureType:"all",
  44. elementType:"labels",
  45. stylers:[
  46. {visibility:"off"},
  47. ]
  48. },
  49. {
  50. featureType:"all",
  51. elementType:"all",
  52. stylers:[
  53. {saturation:-100}
  54. ]
  55. },
  56. {
  57. featureType:"road",
  58. elementType:"all",
  59. stylers:[
  60. {hue:'#FF5500'},
  61. {saturation:75},
  62. {lightness:0}
  63. ]
  64. },
  65. {
  66. featureType:"transit",
  67. elementType:"all",
  68. stylers:[
  69. {hue:'#FF5500'},
  70. {saturation:75},
  71. {lightness:0}
  72. ]
  73. },
  74. {
  75. featureType:"landscape.man_made",
  76. elementType:"administrative",
  77. stylers:[
  78. {hue:'#FF5500'},
  79. {saturation:25},
  80. {lightness:0}
  81. ]
  82. },
  83. {
  84. featureType:"water",
  85. elementType:"geometry",
  86. stylers:[
  87. {visibility:"off"},
  88. {saturation:-100}
  89. ]
  90. },
  91. {
  92. featureType: "poi",
  93. stylers: [
  94. { lightness: 70 }
  95. ]
  96. },
  97. ]});
  98. }
  99. }else{
  100. console.log('can\'t find address');
  101. }
  102. });
  103. }
  104. }

URL: http://fatfolderdesign.com/607/code/google-maps-api-2-the-second-post-multiple-locations-map-styling-and-the-new-geolocation

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.