HTML Geolocation API ﺩﻮﺷ ﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ ﺮﺑﺭﺎﮐ ﮏﯾ ﯽﯾﺎﯿﻓﺍﺮﻐﺟ ﺖﯿﻌﻗﻮﻣ ﻥﺩﺭﻭﺁ ﺖﺳﺪﺑ ﯼﺍﺮﺑ.
.ﺪﻨﮐ ﺪﯿﯾﺄﺗ ﺍﺭ ﻥﺁ ﺮﺑﺭﺎﮐ ﻪﮑﻨﯾﺍ ﺮﮕﻣ ﺖﺴﯿﻧ ﺱﺮﺘﺳﺩ ﺭﺩ ﺖﯿﻌﻗﻮﻣ ،ﺩﺯﺍﺪﻨﯿﺑ ﺮﻄﺧ ﻪﺑ ﺍﺭ ﯽﺻﻮﺼﺧ ﻢﯾﺮﺣ ﺪ
.ﺖﺳﺍ ﻦﯾﺮﺗ ﻖﯿﻗﺩ ،ﺪﻨﻤﺷﻮﻫ ﯼﺎﻫ ﯽﺷﻮﮔ ﺪﻨﻧﺎﻣ ،GPS ﯼﺍﺭﺍﺩ ﯼﺎﻫ ﻩﺎﮕﺘﺳﺩ ﯼﺍﺮﺑ ﯽﯾﺎﯿﻓﺍﺮﻐﺟ ﻥﺎﮑﻣ
Geolocation API ﺩﻮﺷ ﯽﻣ ﯽﻧﺎﺒﯿﺘﺸﭘ ﺎﻫﺮﮔﺭﻭﺮﻣ ﻪﻤﻫ ﺭﺩ:
Yes | Yes | Yes | Yes | Yes |
Geolocation API ﻪﻨﯿﻣﺯ ﯼﻭﺭ ﻂﻘﻓﯽﻣ ﺭﺎﮐ ﯽﻨﻣﺍ ﯼﺎﻫﺪﻨﮐ
ﺩﻮﺷ ﯽﻣ ﯽﻧﺎﺑﺰﯿﻣ (HTTP ﺪﻨﻧﺎﻣ) ﻦﻣﺍ ﺮﯿﻏ ﻊﺒﻨﻣ ﮏﯾ ﯼﻭﺭ ﺮﺑ ﺎﻤﺷ ﺖﯾﺎﺳ ﺮﮔﺍ
.ﺩﻮﺷ ﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ ﺮﺑﺭﺎﮐ ﺖﯿﻌﻗﻮﻣ ﻥﺪﻧﺍﺩﺮﮔﺮﺑ ﯼﺍﺮﺑ getCurrentPosit
:ﺪﻧﺍﺩﺮﮔ ﯽﻣﺮﺑ ﺍﺭ ﺮﺑﺭﺎﮐ ﺖﯿﻌﻗﻮﻣ ﯽﯾﺎﯿﻓﺍﺮﻐﺟ ﺽﺮﻋ ﻭ ﻝﻮﻃ ﺮﯾﺯ ﻝﺎﺜﻣ
<script>
const x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Geolocation API</h2>
<p>Click the button to get your coordinates.</p>
<button onclick="getLocation()">Try It</button>
<p id="demo"></p>
<script>
const x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>
</body>
</html>
:ﻩﺪﺷ ﻩﺩﺍﺩ ﺢﯿﺿﻮﺗ ﻝﺎﺜﻣ
ﺩﻮﺷ ﯽﻣ ﯽﻧﺎﺒﯿﺘﺸﭘ ﯽﯾﺎﯿﻓﺍﺮﻐﺟ ﺖﯿﻌﻗﻮﻣ ﺎﯾﺁ ﻪﮐ ﺪﯿﻨﮐ ﯽﺳﺭﺮﺑ
ﺪﯿﻫﺩ ﺶﯾﺎﻤﻧ ﺮﺑﺭﺎﮐ ﻪﺑ ﯽﻣﺎﯿﭘ ﺕﺭﻮﺻ ﻦﯾﺍ ﺮﯿﻏ ﺭﺩ .ﺪﯿﻨﮐ ﺍﺮﺟﺍ ﺍﺭ getCurrentPosition() ﺪﺘﻣ ،ﯽﻧﺎ
.ﺪﻧﺍﺩﺮﮔ ﯽﻣﺮﺑ (showPosition) ﺮﺘﻣﺍﺭﺎﭘ ﺭﺩ ﻩﺪﺷ ﺺﺨﺸﻣ ﻊﺑﺎﺗ ﻪﺑ ﺍﺭ ﺕﺎﺼﺘﺨﻣ ﯽﺷ ﮏﯾ ،ﺪﺷﺎﺑ ﻖﻓﻮﻣ()
ﺪﻫﺩ ﯽﻣ ﯽﺟﻭﺮﺧ ﺍﺭ Longitude ﻭ showPosition() Latitude ﻊﺑﺎﺗ
.ﺎﻄﺧ ﻪﺑ ﯽﮔﺪﯿﺳﺭ ﻥﻭﺪﺑ ،ﺖﺳﺍ ﯽﯾﺍﺪﺘﺑﺍ ﺭﺎﯿﺴﺑ Geolocation ﺖﭙﯾﺮﮑﺳﺍ ﮏﯾ ﻻﺎﺑ ﻝﺎﺜﻣ
ﺩﻮﺷ ﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ ﺖﯾﺮﯾﺪﻣ ﯼﺍﺮﺑ getCurrentPosition()
ﺵﻭﺭ
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
x.innerHTML = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML = "Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML = "An unknown error occurred."
break;
}
}
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Geolocation API</h2>
<p>Click the button to get your coordinates.</p>
<button onclick="getLocation()">Try It</button>
<p id="demo"></p>
<script>
const x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
x.innerHTML = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML = "Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML = "An unknown error occurred."
break;
}
}
</script>
</body>
</html>
ﺪﯿﺷﺎﺑ ﻪﺘﺷﺍﺩ ﯽﺳﺮﺘﺳﺩ Google ﺪﻨﻧﺎﻣ ﻪﺸﻘﻧ ﺲﯾﻭﺮﺳ ﮏﯾ ﻪﺑ ﺪﯾﺎﺑ ،ﻪﺸﻘﻧ ﺭﺩ ﻪﺠﯿﺘﻧ ﺶﯾﺎﻤﻧ ﯼﺍﺮﺑ
ﺩﻮﺷ ﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ ﻞﮔﻮﮔ ﺭﺩ ﻥﺎﮑﻣ ﻥﺩﺍﺩ ﻥﺎﺸﻧ ﯼﺍﺮﺑ ﯽﺘﺸﮔﺯﺎﺑ ﯽﯾﺎﯿﻓﺍﺮﻐﺟ ﺽﺮﻋ ﻭ ﻝﻮﻃ ،ﺮﯾﺯ ﻝﺎﺜﻣ ﺭﺩ
function showPosition(position) {
let latlon = position.coords.latitude + "," + position.coords.longitude;
let img_url = "https://maps.googleapis.com/maps/api/staticmap?center=
"+latlon+"&zoom=14&size=400x300&sensor=false&key=YOUR_KEY";
document.getElementById("mapholder").innerHTML = "<img src='"+img_url+"'>";
}
ﻦﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﺕﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>HTML Geolocation</h1>
<p id="demo">Click the button to get your position.</p>
<button onclick="getLocation()">Try It</button>
<div id="mapholder"></div>
<script>
const x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
let latlon = position.coords.latitude + "," + position.coords.longitude;
let img_url = "https://maps.googleapis.com/maps/api/staticmap?center="
+latlon+"&zoom=14&size=400x300&key=AIzaSyBu-916DdpKAjTmJNIgngS6HL_kDIKU0aU";
document.getElementById("mapholder").innerHTML = "<img src='"+img_url+"'>";
}
//To use this code on your website, get a free API key from Google.
//Read more at: https://www.w3schools.com/graphics/google_maps_basic.asp
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
x.innerHTML = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML = "Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML = "An unknown error occurred."
break;
}
}
</script>
</body>
</html>
.ﻥﺪﯿﺸﮐ ﻭ ﯽﯾﺎﻤﻨﮔﺭﺰﺑ ،ﺮﮕﻧﺎﺸﻧ ﯼﺎﻫ ﻪﻨﯾﺰﮔ ﺎﺑ ﻞﮔﻮﮔ ﯽﻠﻣﺎﻌﺗ ﻪﺸﻘﻧ ﺶﯾﺎﻤﻧ ﻩﻮﺤﻧ
ﻞﮔﻮﮔ ﻪﺸﻘﻧ ﺖﭙﯾﺮﮑﺳﺍ
<!DOCTYPE html>
<html>
<body>
<h1>HTML Geolocation</h1>
<p id="demo">Click the button to get your position.</p>
<button onclick="getLocation()">Try It</button>
<div id="mapholder"></div>
<script src="https://maps.google.com/maps/api/js?key=AIzaSyBu-916DdpKAjTmJNIgngS6HL_kDIKU0aU"></script>
<!--
To use this code on your website, get a free API key from Google.
Read more at: https://www.w3schools.com/graphics/google_maps_basic.asp
-->
<script>
const x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
let lat = position.coords.latitude;
let lon = position.coords.longitude;
const latlon = new google.maps.LatLng(lat, lon)
const mapholder = document.getElementById('mapholder')
mapholder.style.height = '250px';
mapholder.style.width = '500px';
var myOptions = {
center:latlon,zoom:14,
mapTypeId:google.maps.MapTypeId.ROADMAP,
mapTypeControl:false,
navigationControlOptions:{style:google.maps.NavigationControlStyle.SMALL}
}
const map = new google.maps.Map(document.getElementById("mapholder"), myOptions);
const marker = new google.maps.Marker({position:latlon,map:map,title:"You are here!"});
}
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
x.innerHTML = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML = "Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML = "An unknown error occurred."
break;
}
}
</script>
</body>
</html>
.ﺖﺳﺍ ﻩﺪﺷ ﻩﺩﺍﺩ ﻥﺎﺸﻧ ﻪﺸﻘﻧ ﯼﻭﺭ ﺮﺑ ﺮﺑﺭﺎﮐ ﺖﯿﻌﻗﻮﻣ ﺶﯾﺎﻤﻧ ﻩﻮﺤﻧ ﻪﺤﻔﺻ ﻦﯾﺍ ﺭﺩ
:ﺪﻨﻧﺎﻣ ،ﺖﺳﺍ ﺪﯿﻔﻣ ﺭﺎﯿﺴﺑ ﻥﺎﮑﻣ ﻪﺑ ﻁﻮﺑﺮﻣ ﺕﺎﻋﻼﻃﺍ ﯼﺍﺮﺑ ﻦﯿﻨﭽﻤﻫ ﯽﯾﺎﯿﻓﺍﺮﻐﺟ ﺖﯿﻌﻗﻮﻣ
ﺯﻭﺭ ﻪﺑ ﯽﻠﺤﻣ ﺕﺎﻋﻼﻃﺍ
ﺮﺑﺭﺎﮐ ﯽﮑﯾﺩﺰﻧ ﺭﺩ ﻪﻗﻼﻋ ﺩﺭﻮﻣ ﻁﺎﻘﻧ ﺶﯾﺎﻤﻧ
(GPS) ﻡﺎﮔ ﻪﺑ ﻡﺎﮔ ﯼﺮﺑﻭﺎﻧ
،ﯽﯾﺎﯿﻓﺍﺮﻐﺟ ﺽﺮﻋ .ﺪﻧﺍﺩﺮﮔ ﯽﻣﺮﺑ ﺖﯿﻘﻓﻮﻣ ﺕﺭﻮﺻ ﺭﺩ ﺍﺭ ﯽﺷ ﮏﯾ getCurr
(ﺩﻮﺷ ﯽﻣ ﻩﺪﻧﺍﺩﺮﮔﺮﺑ ﻪﺸﯿﻤﻫ) ﯼﺭﺎﺸﻋﺍ ﺩﺪﻋ ﮏﯾ ﻥﺍﻮﻨﻋ ﻪﺑ ﯽﯾﺎﯿﻓﺍﺮﻐﺟ ﺽﺮﻋ
(ﺩﻮﺷ ﯽﻣ ﻩﺪﻧﺍﺩﺮﮔﺮﺑ ﻪﺸﯿﻤﻫ) ﯼﺭﺎﺸﻋﺍ ﺩﺪﻋ ﮏﯾ ﻥﺍﻮﻨﻋ ﻪﺑ ﯽﯾﺎﯿﻓﺍﺮﻐﺟ ﻝﻮﻃ
(ﺩﻮﺷ ﯽﻣ ﻩﺪﻧﺍﺩﺮﮔﺮﺑ ﻪﺸﯿﻤﻫ) ﺖﯿﻌﻗﻮﻣ ﺖﻗﺩ
(ﺩﻮﺷ ﯽﻣ ﻩﺪﻧﺍﺩﺮﮔﺮﺑ ﺩﻮﺟﻭ ﺕﺭﻮﺻ ﺭﺩ) ﺎﯾﺭﺩ ﻂﺳﻮﺘﻣ ﺢﻄﺳ ﺯﺍ ﺮﺗﻻﺎﺑ ﺮﺘﻣ ﺐﺴﺣ ﺮﺑ ﻉﺎﻔﺗﺭﺍ
(ﺩﻮﺷ ﯽﻣ ﻩﺪﻧﺍﺩﺮﮔﺮﺑ ﺩﻮﺟﻭ ﺕﺭﻮﺻ ﺭﺩ) ﺖﯿﻌﻗﻮﻣ ﻉﺎﻔﺗﺭﺍ ﺖﻗﺩ
(ﺩﻮﺷ ﯽﻣ ﻩﺪﻧﺍﺩﺮﮔﺮﺑ ﺩﻮﺟﻭ ﺕﺭﻮﺻ ﺭﺩ) ﻝﺎﻤﺷ ﺯﺍ ﺖﻋﺎﺳ ﯼﺎﻫ ﻪﺑﺮﻘﻋ ﺖﻬﺟ ﺭﺩ ﻪﺟﺭﺩ ﺕﺭﻮﺻ ﻪﺑ ﻥﺍﻮﻨﻋ
(ﺩﻮﺷ ﯽﻣ ﻩﺪﻧﺍﺩﺮﮔﺮﺑ ﺩﻮﺟﻭ ﺕﺭﻮﺻ ﺭﺩ) ﻪﯿﻧﺎﺛ ﺮﺑ ﺮﺘﻣ ﺐﺴﺣ ﺮﺑ ﺖﻋﺮﺳ
(ﺩﻮﺷ ﯽﻣ ﻩﺪﻧﺍﺩﺮﮔﺮﺑ ﺩﻮﺟﻭ ﺕﺭﻮﺻ ﺭﺩ) ﺦﺳﺎﭘ ﻥﺎﻣﺯ/ﺦﯾﺭﺎﺗ
:ﺩﺭﺍﺩ ﺰﯿﻧ ﯼﺮﮕﯾﺩ ﺐﻟﺎﺟ ﯼﺎﻫ ﺵﻭﺭ Geolocation ﯽﺷ
watchPosition()
- ﯽﻣﺮﺑ ﺍﺭ ﺮﺑﺭﺎﮐ ﯽﻠﻌﻓ ﺖﯿﻌﻗﻮﻣﻪﺑ ﻭ
clearWatch()
- ﺵﻭﺭ watchPo
ﺪﻨﻧﺎﻣ) ﺪﯾﺭﺍﺩ ﺯﺎﯿﻧ ﻖﯿﻗﺩ GPS ﻩﺎﮕﺘﺳﺩ ﮏﯾ ﻪﺑ ﺩﺭﻮﻣ ﻦﯾﺍ ﺶﯾﺎﻣﺯﺁ ﯼﺍﺮﺑ .ﺪﻫﺩ ﯽﻣ ﻥﺎﺸﻧ ﺍﺭ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ ﻝﺎﺜﻣ
<script>
const x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.watchPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Geolocation API</h2>
<p>Click the button to get your coordinates.</p>
<button onclick="getLocation()">Try It</button>
<p id="demo"></p>
<script>
const x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.watchPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML="Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>
</body>
</html>