Web sitesine konum alma özelliği eklemek

asizby

Hectopat
Katılım
20 Aralık 2020
Mesajlar
226
Çözümler
1
Daha fazla  
Cinsiyet
Erkek
Ekran Alıntısı.PNG


Resimde gördüğünüz icona kişiler tıkladığında konum gösterme veya konum belirtme gibi özellikleri.
HTML içerisine yazabiliyor muyuz? Yazabiliyorsak yardımcı olur musunuz? İyi günler.
 
JavaScript:
var userLocationLat = "0,000000000";
var userLocationLon = "0,000000000";

function getLocation() {
    if (navigator.geolocation)
        navigator.geolocation.getCurrentPosition(showPosition, showError);
    else
        console.log("Geolocation is not supported by this browser.");
}

function showPosition(position) {
    userLocationLat = position.coords.latitude;
    userLocationLon = position.coords.longitude;
}

function showError(error) {
    if (error.code == 1)
        console.log("User denied the request for Geolocation.");
    else if (err.code == 2)
        console.log("Location information is unavailable.");
    else if (err.code == 3)
        console.log("The request to get user location timed out.");
    else
        console.log("An unknown error occurred.");
}
$(document).ready(function () {
    getLocation();
});

Bu kod sana userLocationLat ve userLocationLon değişkenlerinde konumu verecektir.

Gönderiyorum derken anlayamadım özür dilerim.
Kodu yükleyemedim. Geç gitti mesajım.
 
JavaScript:
var userLocationLat = "0,000000000";
var userLocationLon = "0,000000000";

function getLocation() {
if (navigator.geolocation)
navigator.geolocation.getCurrentPosition(showPosition, showError);
else.
console.log("Geolocation is not supported by this browser.");
}

function showPosition(position) {
userLocationLat = position.coords.latitude;
userLocationLon = position.coords.longitude;
}

function showError(error) {
if (error.code == 1)
console.log("User denied the request for Geolocation.");
else if (err.code == 2)
console.log("Location information is unavailable.");
else if (err.code == 3)
console.log("The request to get user location timed out.");
else.
console.log("An unknown error occurred.");
}
$(document).ready(function () {
getLocation();
});

Bu kod sana userLocationLat ve userLocationLon değişkenlerinde konumu verecektir.

Kodu yükleyemedim. Geç gitti mesajım.

Teşekkürler fakat burada HTML dokümanına erişebildiğimiz bir kod yok direk script dosyası oluşturup import etmem yeterli mi?
 
Teşekkürler fakat burada HTML dokümanına erişebildiğimiz bir kod yok direk script dosyası oluşturup import etmem yeterli mi?
Hayır, değil. HTML'e import ettikten sonra yol tarifini de JS'ten açtırman lazım ki konuma erişebil ya da bu değerleri gizli bir inputa yazdırıp oradan okuyabilirsin.
 
Şu şekilde de yapabilirsin:

  • setInterval() fonksiyonu ile getLocation 2 saniyede bir çalıştırabilirsin.
  • Google map'daki PIN'ler var Kullanıcının Pin'ini her fonksiyon çalıştığında silip userLocationLat ve userLocationLon değerlerini ataman lazım.
  • Map'de center olarak da sürekli yeni oluşturduğun konumu verirsen. Sürekli map ortalanarak kullanıcı pini 2 saniyede bir haraket etmiş olur.
Fakat bu sistemde şöyle bir şey var. Bu javascript'i yazdıktan sonra Tarayıcı araya girer. Kullanıcıya sorar ki konumunu bu site ile paylaşmak istiyor musun?

Eğer kullanıcı izin vermezse çalışmaz onun için bu fonksiyonu try catch ile yakalayıp Google Map bloğunu lütfen izin verin tarzı bir ekranla doldurabilirsin.
 

Yeni konular

Geri
Yukarı