Guest User

Untitled

a guest
Jul 22nd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.23 KB | None | 0 0
  1. geocoder.geocode( {'address': request.term }, function(results, status) {
  2.  
  3. response($.map(results, function(item) {
  4.  
  5. alert(item.formatted_address+" "+item.address_components.locality)
  6. }
  7. });
  8.  
  9. "results" : [
  10. {
  11. "address_components" : [
  12. {
  13. "long_name" : "London",
  14. "short_name" : "London",
  15. "types" : [ "locality", "political" ]
  16. } ],
  17. "formatted_address" : "Westminster, London, UK" // rest of array...
  18.  
  19. var arrAddress = item.address_components;
  20. var itemRoute='';
  21. var itemLocality='';
  22. var itemCountry='';
  23. var itemPc='';
  24. var itemSnumber='';
  25.  
  26. // iterate through address_component array
  27. $.each(arrAddress, function (i, address_component) {
  28. console.log('address_component:'+i);
  29.  
  30. if (address_component.types[0] == "route"){
  31. console.log(i+": route:"+address_component.long_name);
  32. itemRoute = address_component.long_name;
  33. }
  34.  
  35. if (address_component.types[0] == "locality"){
  36. console.log("town:"+address_component.long_name);
  37. itemLocality = address_component.long_name;
  38. }
  39.  
  40. if (address_component.types[0] == "country"){
  41. console.log("country:"+address_component.long_name);
  42. itemCountry = address_component.long_name;
  43. }
  44.  
  45. if (address_component.types[0] == "postal_code_prefix"){
  46. console.log("pc:"+address_component.long_name);
  47. itemPc = address_component.long_name;
  48. }
  49.  
  50. if (address_component.types[0] == "street_number"){
  51. console.log("street_number:"+address_component.long_name);
  52. itemSnumber = address_component.long_name;
  53. }
  54. //return false; // break the loop
  55. });
  56.  
  57. var arrAddress = item.results[0].address_components;
  58. // iterate through address_component array
  59. $.each(arrAddress, function (i, address_component) {
  60. if (address_component.types[0] == "locality") // locality type
  61. console.log(address_component.long_name); // here's your town name
  62. return false; // break the loop
  63. });
  64.  
  65. <script type="text/javascript">
  66. function initialize()
  67. {
  68. //set initial settings for the map here
  69. var mapOptions =
  70. {
  71. //set center of map as center for the contiguous US
  72. center: new google.maps.LatLng(39.828, -98.5795),
  73. zoom: 4,
  74. mapTypeId: google.maps.MapTypeId.HYBRID
  75. };
  76.  
  77. //load the map
  78. var map = new google.maps.Map(document.getElementById("map"), mapOptions);
  79.  
  80. //This runs when the user clicks on the map
  81. google.maps.event.addListener(map, 'click', function(event)
  82. {
  83. //initialize geocoder
  84. var geocoder = new google.maps.Geocoder()
  85.  
  86. //load coordinates into the user form
  87. main_form.latitude.value = event.latLng.lat();
  88. main_form.longitude.value = event.latLng.lng();
  89.  
  90. //prepare latitude and longitude
  91. var latlng = new google.maps.LatLng(event.latLng.lat(), event.latLng.lng());
  92.  
  93. //get address info such as city and state from lat and long
  94. geocoder.geocode({'latLng': latlng}, function(results, status)
  95. {
  96. if (status == google.maps.GeocoderStatus.OK)
  97. {
  98. //break down the three dimensional array into simpler arrays
  99. for (i = 0 ; i < results.length ; ++i)
  100. {
  101. var super_var1 = results[i].address_components;
  102. for (j = 0 ; j < super_var1.length ; ++j)
  103. {
  104. var super_var2 = super_var1[j].types;
  105. for (k = 0 ; k < super_var2.length ; ++k)
  106. {
  107. //find city
  108. if (super_var2[k] == "locality")
  109. {
  110. //put the city name in the form
  111. main_form.city.value = super_var1[j].long_name;
  112. }
  113. //find county
  114. if (super_var2[k] == "administrative_area_level_2")
  115. {
  116. //put the county name in the form
  117. main_form.county.value = super_var1[j].long_name;
  118. }
  119. //find State
  120. if (super_var2[k] == "administrative_area_level_1")
  121. {
  122. //put the state abbreviation in the form
  123. main_form.state.value = super_var1[j].short_name;
  124. }
  125. }
  126. }
  127. }
  128. }
  129. });
  130. });
  131. }
  132. </script>
  133.  
  134. var map_center = map.getCenter();
  135. reverseGeocode(map_center);
  136.  
  137.  
  138. function reverseGeocode(latlng){
  139. geocoder.geocode({'latLng': latlng}, function(results, status) {
  140. if (status == google.maps.GeocoderStatus.OK) {
  141. var level_1;
  142. var level_2;
  143. for (var x = 0, length_1 = results.length; x < length_1; x++){
  144. for (var y = 0, length_2 = results[x].address_components.length; y < length_2; y++){
  145. var type = results[x].address_components[y].types[0];
  146. if ( type === "administrative_area_level_1") {
  147. level_1 = results[x].address_components[y].long_name;
  148. if (level_2) break;
  149. } else if (type === "locality"){
  150. level_2 = results[x].address_components[y].long_name;
  151. if (level_1) break;
  152. }
  153. }
  154. }
  155. updateAddress(level_2, level_1);
  156. }
  157. });
  158. }
  159.  
  160. function updateAddress(city, prov){
  161. // do what you want with the address here
  162. }
  163.  
  164. geocoder.geocode({'address': request.term }, function(results, status){
  165.  
  166. response($.map(results, function(item){
  167.  
  168. var city = $.grep(item.address_components, function(x){
  169. return $.inArray('locality', x.types) != -1;
  170. })[0].short_name;
  171.  
  172. alert(city);
  173. }
  174. });
  175.  
  176. // Use Google Geocoder to get Lat/Lon for Address
  177. function codeAddress() {
  178. // Function geocodes address1 in the Edit Panel and fills in lat and lon
  179. address = document.getElementById("tbAddress").value;
  180. geocoder.geocode({ 'address': address }, function (results, status) {
  181. if (status == google.maps.GeocoderStatus.OK) {
  182. loc[0] = results[0].geometry.location.lat();
  183. loc[1] = results[0].geometry.location.lng();
  184. document.getElementById("tbLat").value = loc[0];
  185. document.getElementById("tbLon").value = loc[1];
  186. var arrAddress = results[0].address_components;
  187. for (ac = 0; ac < arrAddress.length; ac++) {
  188. if (arrAddress[ac].types[0] == "street_number") { document.getElementById("tbUnit").value = arrAddress[ac].long_name }
  189. if (arrAddress[ac].types[0] == "route") { document.getElementById("tbStreet").value = arrAddress[ac].short_name }
  190. if (arrAddress[ac].types[0] == "locality") { document.getElementById("tbCity").value = arrAddress[ac].long_name }
  191. if (arrAddress[ac].types[0] == "administrative_area_level_1") { document.getElementById("tbState").value = arrAddress[ac].short_name }
  192. if (arrAddress[ac].types[0] == "postal_code") { document.getElementById("tbZip").value = arrAddress[ac].long_name }
  193. }
  194. document.getElementById("tbAddress").value = results[0].formatted_address;
  195. }
  196. document.getElementById("pResult").innerHTML = 'GeoCode Status:' + status;
  197. })
  198. }
  199.  
  200. //if (arrAddress[ac].types[0] == "street_number") { alert(arrAddress[ac].long_name) } // SOKAK NO
  201. //if (arrAddress[ac].types[0] == "route") { alert(arrAddress[ac].short_name); } // CADDE
  202. //if (arrAddress[ac].types[0] == "locality") { alert(arrAddress[ac].long_name) } // İL
  203. //if (arrAddress[ac].types[0] == "administrative_area_level_1") { alert(arrAddress[ac].short_name) } // İL
  204. //if (arrAddress[ac].types[0] == "postal_code") { alert(arrAddress[ac].long_name); } // POSTA KODU
  205. //if (arrAddress[ac].types[0] == "neighborhood") { alert(arrAddress[ac].long_name); } // Mahalle
  206. //if (arrAddress[ac].types[0] == "sublocality") { alert(arrAddress[ac].long_name); } // İlçe
  207. //if (arrAddress[ac].types[0] == "country") { alert(arrAddress[ac].long_name); } // Ülke
  208.  
  209. _.findKey(vObj.address_components, function(component) {
  210.  
  211. if (component.types[0] == 'street_number') {
  212. $scope.eventDetail.location.address = component.short_name
  213. }
  214.  
  215. if (component.types[0] == 'route') {
  216. $scope.eventDetail.location.address = $scope.eventDetail.location.address + " " + component.short_name;
  217. }
  218.  
  219. if (component.types[0] == 'locality') {
  220. $scope.eventDetail.location.city = component.long_name;
  221. }
  222.  
  223. if (component.types[0] == 'neighborhood') {
  224. $scope.eventDetail.location.neighborhood = component.long_name;
  225. }
  226.  
  227. });
  228.  
  229. let city = find(result, (address) => {
  230. return typeof find(address.types, (a) => { return a === 'locality'; }) === 'string';
  231. });
  232.  
  233. city = results[0].address_components.filter(function(addr){
  234. return (addr.types[0]=='locality')?1:(addr.types[0]=='administrative_area_level_1')?1:0;
  235. });
  236.  
  237. const localityObject = body.results[0].address_components.filter((obj) => {
  238. return obj.types.includes('locality');
  239. })[0];
  240. const city = localityObject.long_name;
  241.  
  242. const city = body.results[0].address_components.filter((obj) => {
  243. return obj.types.includes('locality');
  244. )[0].long_name;
Add Comment
Please, Sign In to add comment