cahyadsn

multiple marker google map from json

Oct 10th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 1.83 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Simple Map</title>
  5. <meta name="viewport" content="initial-scale=1.0">
  6. <meta charset="utf-8">
  7. <style>
  8. /* Always set the map height explicitly to define the size of the div
  9. * element that contains the map. */
  10. #map{height: 100%;}
  11. /* Optional: Makes the sample page fill the window. */
  12. html, body {
  13. height: 100%;
  14. margin: 0;
  15. padding: 0;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <div id="map"></div>
  21. <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDB1S8mf8lG-rc7iYUSn4TYVZZsivQoslI"></script>
  22. <!-- JQuery -->
  23. <script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
  24. <script>
  25. var map;
  26. var lokasi = [];
  27.  
  28. //function initialize(){
  29.   map = new google.maps.Map(document.getElementById('map'), {
  30.   center:{
  31.     lat: -6.18,
  32.     lng: 106.8},
  33.     zoom: 11
  34.   });
  35. //}
  36. //google.maps.event.addDomListener(window, 'load', initialize);
  37.  
  38. function findLokasi(){
  39.   $.ajax({
  40.     type: "GET",
  41.     url: "phd.json",
  42.     dataType: "json",  
  43.     success: function(data){
  44.       var d = new google.maps.InfoWindow();
  45.       var e;    
  46.       $.each(data,function(i, b){
  47.         e = new google.maps.Marker({
  48.           position: new google.maps.LatLng(b.lat, b.lng),
  49.           map: map
  50.         });
  51.         lokasi.push(e);
  52.         google.maps.event.addListener(e, 'click', (function(e, i) {
  53.           return function() {
  54.             d.setContent('<div><h3>' + b.nama + '</h3></div>');
  55.             d.open(map, e)
  56.           }
  57.         })(e, i))
  58.       });
  59.     }
  60.   });
  61. }
  62. $(function() {
  63.   findLokasi();
  64. });
  65. </script>
  66. </body>
  67. </html>
  68.  
  69. //--- file phd.json
  70. [
  71. {
  72.     "lat":"-6.16402808234",
  73.     "lng":"106.903297227",
  74.     "nama":"Kelapa Gading"
  75. },
  76. {
  77.     "lat":"-6.16586244575",
  78.     "lng":"106.762844694",
  79.     "nama":"Green Garden"
  80. },
  81. {
  82.     "lat":"-6.23903446821",
  83.     "lng":"106.847553563",
  84.     "nama":"Tebet"
  85. }
  86. ]
Add Comment
Please, Sign In to add comment