Google Geocoding - Cannot pull co-ordinates

末鹿安然 提交于 2019-12-10 12:33:26

问题


I've had some help on here and I have tried to frankenstine two pieces of code together to get the following result:

User clicks Geocode button User is presented with the following information (Typed Address, City, State, Country, Co-ordinates)

This is the code I've pieced together with a bit of help: http://jsfiddle.net/QA7Xr/25/

This is the full written code:

 var myLatlng = new google.maps.LatLng(31.272410, 0.190898);


 // INITALIZATION
 function initialize() {
     var mapOptions = {
         zoom: 4,
         center: myLatlng,
         mapTypeId: google.maps.MapTypeId.ROADMAP
     }
     var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
 }

 // GEOCODE
 function codeAddress() {
     var address = document.getElementById("address").value;
     geocoder.geocode({
         'address': address
     }, function (results, status) {
         if (status == google.maps.GeocoderStatus.OK) {
             var address = "",
                 city = "",
                 state = "",
                 zip = "",
                 country = "";
             for (var i = 0; i < results[0].address_components.length; i++) {
                 var addr = results[0].address_components[i];
                 if (addr.types[0] == 'country') country = addr.long_name;
                 else if (addr.types[0] == 'street_address') // address 1
                 address = address + addr.long_name;
                 else if (addr.types[0] == 'establishment') address = address + addr.long_name;
                 else if (addr.types[0] == 'route') address = address + addr.long_name;
                 else if (addr.types[0] == 'postal_code') zip = addr.short_name;
                 else if (addr.types[0] == ['administrative_area_level_1']) state = addr.long_name;
                 else if (addr.types[0] == ['locality']) city = addr.long_name;
             }
             alert('City: ' + city + '\n' + 'State: ' + state + '\n' + 'Zip: ' + zip + '\n' + 'Country: ' + country);
         }
     }
     });
 } else {
     alert("Geocode was not successful for the following reason: " + status);
 };
 });
 } 

 initailize();
 document.getElementById("codeAddress").onclick = function () {
     codeAddress();
     return false;
 };

I am new to javascript programming but the way it should work is if the geocoding is successful it grabs the variables and dumps them in a dialogue box, but it's just not doing it. Is it falling short on a simple bracket error or have a written something that is even more erroneous?


回答1:


initailize();. Just saying.

Have a corrected fiddle (again): http://jsfiddle.net/d5DBh/

This time, I have forked it so no-one will overwrite it. The fixes:

  • You had one extra bracket near else. Cleared.
  • You also never defined or instantiated a geocoder. This has been added
  • Typo on initialize.

Please simplify your code and actually read the corrections on other questions. One of the three bugs, I had already corrected.



来源:https://stackoverflow.com/questions/16500222/google-geocoding-cannot-pull-co-ordinates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!