0

GoogleジオコーダーAPIを使用して、ユーザーの位置情報をフォームに直接入力しようとしています(逆ジオコーディング)。この投稿で最も人気のある回答をコードベースとして使用しています。

その回答に示されているように、body onload = "initialize()"を使用すると、フォームに正しく入力されます。ただし、ボタンを使用して機能を開始しようとしても、何も起こりません(body onload属性を削除します)。私は次の2つのバリエーションを試しました。

<a href="javascript:;" style="center" onclick="initiate()" class="button">Get Geolocation data</a>

<input type="button" value="Get Geolocation Data" onclick="initiate()">

誰かが私の間違いを指摘してもらえますか?完全なコードは次のとおりです。

    <!DOCTYPE html> 
<html> 
<head> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
<title>Reverse Geocoding</title> 

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> 
<script type="text/javascript"> 
  var geocoder;

  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
} 
//Get the latitude and the longitude;
function successFunction(position) {
    var lat = position.coords.latitude;
    var lng = position.coords.longitude;
    codeLatLng(lat, lng)
}

function errorFunction(){
    alert("Geocoder failed");
}

  function initialize() {
    geocoder = new google.maps.Geocoder();



  }

  function codeLatLng(lat, lng) {

    var latlng = new google.maps.LatLng(lat, lng);
    geocoder.geocode({'latLng': latlng}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
      console.log(results)
        if (results[1]) {
         //formatted address
         alert(results[0].formatted_address)
        //find country name
             for (var i=0; i<results[0].address_components.length; i++) {
            for (var b=0;b<results[0].address_components[i].types.length;b++) {

            //there are different types that might hold a city admin_area_lvl_1 usually does in come cases looking for sublocality type will be more appropriate
                if (results[0].address_components[i].types[b] == "administrative_area_level_1") {
                    //this is the object you are looking for
                    city= results[0].address_components[i];
                    break;
                }
            }
        }
        //city data
        alert(city.short_name + " " + city.long_name)


        } else {
          alert("No results found");
        }
      } else {
        alert("Geocoder failed due to: " + status);
      }
    });
  }
</script> 
</head> 
<body onload="initialize()"> 

</body> 
</html> 

よろしくお願いします。

4

3 に答える 3

0

タイプミスのようです:initializeの代わりに を使用してinitiateください。

を使用していたと述べbody onload="initialize()"、初期化関数の呼び出しをボタンの onclick に移動すると、動作が停止しました。

ただし、あなたの例では、次を使用しましたinitiate

<a href="javascript:;" style="center" onclick="initiate()" class="button">Get Geolocation data</a>

<input type="button" value="Get Geolocation Data" onclick="initiate()">
于 2012-05-08T21:30:05.320 に答える
0

これは、デフォルトの動作を停止せずにアンカー要素で onlick を使用しているためです。これを試して。

<a href="#" style="center" onclick="return initialize()" class="button">Get Geolocation</a>

initializeそして、 js メソッドから false を返します。

function initiate() {
   geocoder = new google.maps.Geocoder();
   return false;
}

@Michaelが述べたように、タイプミスもあります。

于 2012-05-08T21:30:22.167 に答える
0

これを変える:

<a href="javascript:;" style="center" onclick="initiate()" class="button">Get Geolocation data</a>

これに:

<a href="javascript:initiate();return(false);" style="center" class="button">Get Geolocation data</a>

そして、関数がinitialize( )またはinitialize( ) と呼ばれているかどうかを確認してください。そこにタイプミスがあるようです。

于 2012-05-08T21:35:44.540 に答える