Advertisement
Guest User

Untitled

a guest
Aug 30th, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1. var geocoder;
  2. var map;
  3. var markers = Array();
  4. var infos = Array();
  5.  
  6. function initialize() {
  7. // prepare Geocoder
  8. geocoder = new google.maps.Geocoder();
  9.  
  10. // set initial position (New York)
  11. var myLatlng = new google.maps.LatLng(-6.21462,106.84513);
  12.  
  13. var myOptions = { // default map options
  14. zoom: 14,
  15. center: myLatlng,
  16. mapTypeId: google.maps.MapTypeId.ROADMAP
  17. };
  18. map = new google.maps.Map(document.getElementById('gmap_canvas'), myOptions);
  19. }
  20.  
  21. // clear overlays function
  22. function clearOverlays() {
  23. if (markers) {
  24. for (i in markers) {
  25. markers[i].setMap(null);
  26. }
  27. markers = [];
  28. infos = [];
  29. }
  30. }
  31.  
  32. // clear infos function
  33. function clearInfos() {
  34. if (infos) {
  35. for (i in infos) {
  36. if (infos[i].getMap()) {
  37. infos[i].close();
  38. }
  39. }
  40. }
  41. }
  42. var addrMarker;
  43. // find address function
  44. function findAddress() {
  45. // var contentTitle;
  46. var address = '<b style="color:black;">'+document.getElementById("varListKabko").value+'</b>';
  47. var infowindow;
  48. // script uses our 'geocoder' in order to find location by address name
  49. geocoder.geocode( { 'address': address}, function(results, status) {
  50. if (status == google.maps.GeocoderStatus.OK) { // and, if everything is ok
  51.  
  52. // we will center map
  53. // console.log(results)
  54. var addrLocation = results[0].geometry.location;
  55. map.setCenter(addrLocation);
  56.  
  57. // store current coordinates into hidden variables
  58. document.getElementById('lat').value = results[0].geometry.location.lat();
  59. document.getElementById('lng').value = results[0].geometry.location.lng();
  60.  
  61. // membuat objek info window
  62. infowindow = new google.maps.InfoWindow({
  63. content: address,
  64. position: addrLocation
  65. });
  66.  
  67. // and then - add new custom marker
  68. addrMarker = new google.maps.Marker({
  69. position: addrLocation,
  70. map: map,
  71. // draggable: true,
  72. title: results[0].formatted_address,
  73. // icon: 'assets/image/marker.png',
  74.  
  75. icon: {
  76. labelOrigin: new google.maps.Point(45, 60),
  77. url: 'assets/image/agi.png',
  78. // size: new google.maps.Size(22, 40),
  79. origin: new google.maps.Point(0, 0),
  80. anchor: new google.maps.Point(30, 40),
  81. },
  82. animation: google.maps.Animation.DROP,
  83. });
  84.  
  85. // addrMarker.addListener('click', toggleBounce);
  86.  
  87. // event saat marker diklik
  88. addrMarker.addListener('click', function() {
  89. // tampilkan info window di atas marker
  90. infowindow.open(map, addrMarker);
  91. });
  92. } else {
  93. alert('Geocode was not successful for the following reason: ' + status);
  94. }
  95. });
  96. }
  97.  
  98. //Icon Marker
  99. function toggleBounce() {
  100. if (addrMarker.getAnimation() !== null) {
  101. addrMarker.setAnimation(null);
  102. } else {
  103. addrMarker.setAnimation(google.maps.Animation.BOUNCE);
  104. }
  105. }
  106.  
  107. // find custom places function
  108. function findPlaces() {
  109.  
  110. // prepare variables (filter)
  111. var type = document.getElementById('gmap_type').value;
  112. var radius = document.getElementById('gmap_radius').value;
  113. // var keyword = document.getElementById('gmap_keyword').value;
  114. var keyword = 'artha graha';
  115.  
  116.  
  117.  
  118. var lat = document.getElementById('lat').value;
  119. var lng = document.getElementById('lng').value;
  120. var cur_location = new google.maps.LatLng(lat, lng);
  121. // alert(cur_location);
  122.  
  123. // prepare request to Places
  124. var request = {
  125. location: cur_location,
  126. radius: radius,
  127. types: [type]
  128. };
  129. if (keyword) {
  130. request.keyword = [keyword];
  131. }
  132.  
  133. // send request
  134. service = new google.maps.places.PlacesService(map);
  135. service.search(request, createMarkers);
  136. }
  137.  
  138. // create markers (from 'findPlaces' function)
  139. function createMarkers(results, status) {
  140. if (status == google.maps.places.PlacesServiceStatus.OK) {
  141. // console.log(results);
  142.  
  143. // if we have found something - clear map (overlays)
  144. clearOverlays();
  145.  
  146. // and create new markers by search result
  147. for (var i = 0; i < results.length; i++) {
  148. createMarker(results[i]);
  149. }
  150. } else if (status == google.maps.places.PlacesServiceStatus.ZERO_RESULTS) {
  151. alert('Sorry, nothing is found');
  152. }
  153. }
  154.  
  155. // creare single marker function
  156. function createMarker(obj) {
  157.  
  158. // prepare new Marker object
  159. var mark = new google.maps.Marker({
  160. position: obj.geometry.location,
  161. map: map,
  162. title: obj.name
  163. });
  164. markers.push(mark);
  165.  
  166. // prepare info window
  167. var infowindow = new google.maps.InfoWindow({
  168. content: '<img src="' + obj.icon + '" /><font style="color:#000;">' + obj.name +
  169. '<br />Rating: ' + obj.rating + '<br />Vicinity: ' + obj.vicinity + '</font>'
  170. });
  171.  
  172. // add event handler to current marker
  173. google.maps.event.addListener(mark, 'click', function() {
  174. clearInfos();
  175. infowindow.open(map,mark);
  176. });
  177. infos.push(infowindow);
  178. }
  179.  
  180. // initialization
  181. google.maps.event.addDomListener(window, 'load', initialize);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement