บทที่ 14 HTML5 Geolocation ตอนที่ 2

3.ตัวอย่าง การแสดงตำแหน่งด้วยแผนที่

<html>
<body>
<p id="demo">Click the button to get your position:</p>
<button onclick="getLocation()">Try It</button>
<div id="mapholder"></div>
<script>
var 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)
  {  var latlon=position.coords.latitude+","+position.coords.longitude;
  var img_url="http://maps.googleapis.com/maps/api/staticmap?center="
  +latlon+"&zoom=14&size=400x300&sensor=false";
  document.getElementById("mapholder").innerHTML="<img src='"+img_url+"'>";  }

</script>
</body>
</html>

ผลลัพธ์คือ

   นอกจากนี้แล้วเรายังสามารถเพิ่มคุณสมบัติอื่นให้กับแผนที่ได้อีกด้วย เช่น จุดมารก์ในแผนที่ การซูมเข้า-ออก และการลากเพื่อดูแผนที่ใกล้เคียง เป็นต้น
ตัวอย่าง

<html>
<body>
<p id="demo">Click the button to get your position:</p>
<button onclick="getLocation()">Try It</button>
<div id="mapholder"></div>
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script>
var 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)
  {  lat=position.coords.latitude;
  lon=position.coords.longitude;
  latlon=new google.maps.LatLng(lat, lon)
  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}  };
  var map=new google.maps.Map(document.getElementById("mapholder"),myOptions);
  var marker=new google.maps.Marker({position:latlon,map:map,title:"You are here!"});  }

</script>
</body>
</html>

ผลลัพธ์คือ


อ่านเพิ่มเติม
บทที่ 14 HTML5 Geolocation คือเทคโนโลยีที่ช่วยหาพิกัด ตอนที่ 1

ข้อมูลอ้างอิง
http://www.w3schools.com

กระทู้ล่าสุดจากเว็บบอร์ด
หัวข้อกระทู้
ตอบ
เปิดดู
ล่าสุด
การใช้ ActionChains ใน Selenium สำหรับจำลองการกระทำบนหน้าเว็บ
โดย athirach.offcial จ 01 เม.ย. 2024 12:25 pm บอร์ด Software testing
0
201
จ 01 เม.ย. 2024 12:25 pm โดย athirach.offcial View Topic การใช้ ActionChains ใน Selenium สำหรับจำลองการกระทำบนหน้าเว็บ
การทำงานกับ Cookies ใน Selenium
โดย athirach.offcial จ 01 เม.ย. 2024 12:03 pm บอร์ด Software testing
0
120
จ 01 เม.ย. 2024 12:03 pm โดย athirach.offcial View Topic การทำงานกับ Cookies ใน Selenium
การเขียน Selenium ให้แจ้งเตือน Message Error
โดย athirach.offcial จ 01 เม.ย. 2024 11:59 am บอร์ด Software testing
0
113
จ 01 เม.ย. 2024 11:59 am โดย athirach.offcial View Topic การเขียน Selenium ให้แจ้งเตือน Message Error
เครื่องมือที่ช่วยในการทำ Automated test ใน python
โดย athirach.offcial จ 01 เม.ย. 2024 11:51 am บอร์ด Software testing
0
110
จ 01 เม.ย. 2024 11:51 am โดย athirach.offcial View Topic เครื่องมือที่ช่วยในการทำ Automated test ใน python
selenium เวอร์ชั่น ปัจจุบัน เป็นเวอร์ชั่นที่เท่าไหร่
โดย athirach.offcial จ 01 เม.ย. 2024 11:39 am บอร์ด Software testing
0
87
จ 01 เม.ย. 2024 11:39 am โดย athirach.offcial View Topic selenium เวอร์ชั่น ปัจจุบัน เป็นเวอร์ชั่นที่เท่าไหร่
ปัจจัยที่ทำให้เกิด Spam score ในเว็บไซต์
โดย athirach.offcial จ 01 เม.ย. 2024 11:27 am บอร์ด Share Knowledge
0
151
จ 01 เม.ย. 2024 11:27 am โดย athirach.offcial View Topic ปัจจัยที่ทำให้เกิด Spam score ในเว็บไซต์
Dell Optiplex 980 ไม่รองรับแรม Rank แบบ Single Rank
โดย mindphp จ 01 เม.ย. 2024 1:55 am บอร์ด ถาม - ตอบ คอมพิวเตอร์
0
349
จ 01 เม.ย. 2024 1:55 am โดย mindphp View Topic Dell Optiplex 980 ไม่รองรับแรม Rank แบบ Single Rank
การจัดประเภทของการ Test โปรแกรม
โดย athirach.offcial ส 30 มี.ค. 2024 12:54 pm บอร์ด Software testing
0
215
ส 30 มี.ค. 2024 12:54 pm โดย athirach.offcial View Topic การจัดประเภทของการ Test โปรแกรม