Skip to content

Instantly share code, notes, and snippets.

@ekrembk
Created November 6, 2012 10:14
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ekrembk/4023860 to your computer and use it in GitHub Desktop.
Save ekrembk/4023860 to your computer and use it in GitHub Desktop.
Kullanıcının şehrini Javascript ile otomatik tespit etme. HTML5 Geocoding API ve Reverse Geocoding için Yandex Geocoder kullanıldı.
function otomatikBul() {
// Browser desteğini kontrol et
if( ! navigator.geolocation ) {
console.log( 'Broserınız desteklemiyor. Lütfen manual seçim yapınız.' );
return;
}
// Uyarı
console.log( 'Koordinatlar alınıyor...' );
// Koordinatları al
var timeoutVal = 10 * 1000 * 1000;
navigator.geolocation.getCurrentPosition(
ilAra,
ilHata,
{ enableHighAccuracy: true, timeout: timeoutVal, maximumAge: 0 }
);
}
// ---
// Otomatik seçimin callback fonksiyonları
// ---
// Koordinatı şehire çeviren fonksiyon
function ilAra( position ) {
// Koordinatlar
var koord = position.coords.longitude + ',' + position.coords.latitude;
// Yandex'ten sonuçları al
var konumBul = $.getJSON( 'http://geocode-maps.yandex.ru/1.x/?geocode=' + koord + '&lang=tr-TR&format=json&callback=?', function(d){
var sehir = null;
var dongu = d.response.GeoObjectCollection.featureMember;
$.each( dongu, function(i){
var secenek = dongu[i];
if( secenek.GeoObject.metaDataProperty.GeocoderMetaData.kind == 'province' ) {
// Şehir ismini al
// Burada API ile dönen verilerden istenilen alınabilir, ben ismi alıyorum sadece.
sehir = secenek.GeoObject.name;
}
});
if( ! sehir ) {
// Bulunamadı
console.log( 'Şehir bulunamadı.' );
} else {
// Bulundu
// Şehir ismini yazdır
console.log( sehir );
}
});
}
// Browser koordinatları tespit etmeye çalışırken hata oluştu
function ilHata( error ) {
console.log( error );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment