SORU
23 Temmuz 2011, CUMARTESİ


Şehir adı coğrafi konum kullanarak

Kullanıcının enlem ve boylam kullanarak html5 coğrafi konum başardı.

//Check if browser supports W3C Geolocation API
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
} 
//Get latitude and longitude;
function successFunction(position) {
    var lat = position.coords.latitude;
    var long = position.coords.longitude;
}

Şehir adını görüntülemek istiyorum, ters coğrafi konum bir apı kullanmak için tek yol gibi görünüyor. Ters belirleme için google belgelerine okudum ama benim sitede çıktı almak için nasıl bilmiyorum.

Kullanın Bu gitmek nasıl bilmiyorum: sayfada şehrin adını görüntülemek için "http://maps.googleapis.com/maps/api/geocode/json?latlng=' lat ',' long '&sensor=true".

Bunu yapmak için standart bir yolu nedir?

CEVAP
23 Temmuz 2011, CUMARTESİ


Böyle bir şey Google API kullanarak.

Lütfen bu iş için google maps kütüphaneye eklemeniz gerekir. Google geocoder Adres çok fazla olan bir şehir olacak gibi eğitimli bir tahmin yapmak gerekir, böylece bileşenleri verir.

< . "" . administrative_area_level_1 ^güçlü genellikle sizin için ne arıyorsanız ama bazen yerellik sonra olan şehirdir.

Neyse daha fazla google tepki türleri hakkında ayrıntılar here here bulunabilir.

Aşağıda hile yapmak gereken kod:

<!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> 

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • eisleyhead

    eisleyhead

    11 Ocak 2006
  • Justin Schenck

    Justin Schen

    24 Kasım 2006
  • Manuel Vizcaino

    Manuel Vizca

    27 Mayıs 2008