0

I'm new to jquery but I am having a bit of a hard time at the moment. This is as far as I can get, the eventual idea is to do the following:

  1. User types in an address and clicks search
  2. Google Geocoder runs
  3. Either error if not result, or grab co-ordinates, state, zip, country, city and print in a message box

The code I have at the moment is not even able to complete the process of grabbing the coordinates from the input box. It keeps telling me it is expecting a bracket but no matter where I put the bracket I cannot get it to run.

This is my trying to run:

http://jsfiddle.net/QA7Xr/16/

Here is the complete 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) {
        alert("Geocode was successful");
        };
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      };
    };
  }


// GET ADDRESS DETAILS
 function getLatLongDetail() {
     var geocoder = new google.maps.Geocoder();
     geocoder.geocode({
         'latLng': myLatlng
     },
     function (results, status) {
         if (status == google.maps.GeocoderStatus.OK) {
             if (results[0]) {
                 var address = "",
                     city = "",
                     state = "",
                     zip = "",
                     country = "";
                 for (var i = 0; i < results[0].address_components.length; i++) {
                     var addr = results[0].address_components[i];
                     // check if this entry in address_components has a type of country
                     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 2
                     address = address + addr.long_name;
                     else if (addr.types[0] == 'postal_code') // Zip
                     zip = addr.short_name;
                     else if (addr.types[0] == ['administrative_area_level_1']) // State
                     state = addr.long_name;
                     else if (addr.types[0] == ['locality']) // City
                     city = addr.long_name;
                 }
                 alert('City: ' + city + '\n' + 'State: ' + state + '\n' + 'Zip: ' + zip + '\n' + 'Country: ' + country);
             }
         }
     });
 }

 initialize();
 getLatLongDetail();

Can anyone tell me where I am going wrong please? I've hit a dead end with this.

4

2 回答 2

2

你的 JS 一团糟,我已经整理好了:http: //jsfiddle.net/QA7Xr/19/

为了:

  • 函数中有两个额外的右括号
  • getElementById()使用的 ID 不存在
  • 当 JS 分配更容易时,您绑定了 onClick
  • 您的geocodervar 在 getLatLongDetail 中是本地的。您在 codeAddress 中需要它,所以我将其设为全球性。

改进点:

  • 将代码包装在闭包中以避免命名空间污染
  • 使用 Google 事件来控制您的代码分配。现在,如果用户点击太快,你的代码就会抛出
  • 简化逻辑
于 2013-05-11T17:34:51.680 回答
0

这是更新的链接,

http://jsfiddle.net/QA7Xr/24/

geocoder.geocode({}, function(){....}); /*not closed properly*/

还有分号使用不当。

于 2013-05-11T17:48:38.750 回答