Created
November 6, 2012 10:14
-
-
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ı.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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