0

私はjqueryを初めて使用しますが、現時点では少し苦労しています。これは私が得ることができる限りであり、最終的なアイデアは次のことを行うことです:

  1. ユーザーが住所を入力して検索をクリックする
  2. Google Geocoder の実行
  3. 結果が得られない場合はエラーにするか、座標、州、郵便番号、国、都市を取得してメッセージ ボックスに出力します

私が現在持っているコードは、入力ボックスから座標を取得するプロセスを完了することさえできません。ブラケットが必要だと言い続けますが、ブラケットをどこに置いても実行できません。

これは私の実行しようとしています:

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

完全なコードは次のとおりです。

 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();

どこが間違っているのか教えてもらえますか?私はこれで行き止まりになりました。

4

2 に答える 2

0

ここに更新されたリンクがあります。

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

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

セミコロンの不適切な使用があります。

于 2013-05-11T17:48:38.750 に答える