2016-04-18 10 views
0

私はhtmlの市町村名はどのようにして取得できますか?

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
<title>Geolocation</title> 
<script src="https://code.jquery.com/jquery-2.2.3.min.js" integrity="sha256-a23g1Nt4dtEYOj7bR+vTu7+T8VP13humZFBJNIYoEJo=" crossorigin="anonymous"></script> 
<script src="funciones.js"></script> 
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> 
</head> 
<body> 
<button id="startGeo">Click here to check your geolocation abilities</button> 
</body> 
</html> 

でこれをhhaveと私はfunction.jsファイルでこれを持っている:

$(document).ready(function(){ 
$("#startGeo").click(checkLocation); 

function checkLocation(){ 
    if (navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(getLocation,locationFailed); 
     //document.write("you have geolocation"); 
    } 
    else { 
     document.write("you don't have geolocation"); 
    } 
}//ends checkLocation() 

function getLocation(position){ 
    var latitud = position.coords.latitude; 
    var longitud = position.coords.longitude; 
    var exactitud = position.coords.accuracy; 

    alert("latitud: " + latitud + " longitud: " + longitud + " exactitud: " + exactitud); 
    //document.write("we received your location"); 
} 
function locationFailed(){ 
    document.write("we didn't get your location. Please check your settings"); 
} 
}); 

このことから私は、座標を取得するが、私はどのように上まったく見当がつかない都市名...または州名を取得します。私はこれに似た質問をしましたが、彼らはjsonを使っているようでした。私はマップを望んでいない、ちょうど情報、それはテキストまたはアラートにすることができます。何かお考えですか?

あなたはAPI

サンプルコール逆ジオコーディングを使用することができます

答えて

0

http://maps.googleapis.com/maps/api/geocode/json?latlng=50.123413,-22.12345&sensor=true

あなたはそのようなあなたのgetLocation機能を置き換えることができます(data.resultsにアクセスする前に適切なテストを行い、[2]):

function getLocation(position){ 
    var latitud = position.coords.latitude; 
    var longitud = position.coords.longitude; 
    var exactitud = position.coords.accuracy; 

    $.get('https://maps.googleapis.com/maps/api/geocode/json?latlng=' + latitud + ',' + longitud + '&sensor=true', function(data) { 
     alert(data.results[2].address_components[0].long_name); 
    }); 

    //alert("latitud: " + latitud + " longitud: " + longitud + " exactitud: " + exactitud); 
    //document.write("we received your location"); 
} 

次のようなフィドルを確認すると、実際の動作を確認できます。

https://jsfiddle.net/5tzxks10/

+0

ありがとうございます。確かに私は情報を得るあなたの助けに感謝しますが、間違っている、それは私に私が知らないアドレスを与える。それはおそらくapiのjsonの問題ですか?私は他のコードを実行し、それは私が使用するブラウザによって異なる場所を与えます。 これはdata.results [2]と関係があるのだろうかと思います。そのような場合は、どのようなものを使用するのかをどのように知ることができますか? –

+0

確かにそれはデータの結果に依存します。[2]ここでは例として2を書いています –

+0

レスポンスについてもっと知るためにAPIドキュメントをチェックしてください: –

0

+ Nowres Rafed私は少しあなたのコードを変更:

function getLocation(position){ 
var latitud = position.coords.latitude; 
var longitud = position.coords.longitude; 
var exactitud = position.coords.accuracy; 

$.get('https://maps.googleapis.com/maps/api/geocode/json?latlng=' + latitud + ',' + longitud, function(data) { 
    alert(data.results[1].formatted_address); 
}); 

//alert("latitud: " + latitud + " longitud: " + longitud + " exactitud: " + exactitud); 
//document.write("we received your location"); 

それはより多くの情報にその方法を提供します。どうもありがとうございます。私が知りたいのは、可能であれば、特定の回答を読み込む方法や読む方法を知ることができるところです。結果が得られたら、結果は複数返します。あなたが言及したように番号を変更し、さまざまなオプションをテストしました。しかし、私はそれらの結果を探すことができるどこかにありますか?私はちょうどこれに新しいです。

関連する問題