4,397   1 ขอทราบวิธีเก็บค่าละติจูดและลองจิจูด ของ google map ไปยังฐานข้อมูลXampp หน่อยครับ (อยากทราบว่า จะดึงค่าออกจาก Map ยังไงด้วยครับ)



tawatchai5530


พลังงานการช่วยเหลือ
( Level 1 )


คะแนนขอบคุณจากสมาชิก
( 0 )


ตอบกระทู้ ( 0 )
เขียนบทความ ( 0 )

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>ทดสอบ geolocation + google map</title>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />

        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
        <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key="></script>
       
  
   

    </head>
    <body >
        
            ตำแหน่งของฉัน:
        <div id="geo_data"></div>
        <div id="map_canvas" style="background: #f5f5f5; height:300px; width: 300px;"></div>

        <script type="text/javascript">
            var initialLocation;
                var bangkok = new google.maps.LatLng(13.755716, 100.501589);
                function initialize() {
                    var myOptions = {
                        zoom: 15,
                        //center: latlng,
                        mapTypeControl: false,
                        navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
                        mapTypeId: google.maps.MapTypeId.ROADMAP
                    };
                    var map = new google.maps.Map(document.getElementById("map_canvas"),
                    myOptions);
                    

                // detect geolocation lat/lng หาตำแหน่งทางภูมิศาสตร์
                if ( navigator.geolocation ) {
                    navigator.geolocation.getCurrentPosition(function(location) {
                        var location = location.coords;
                        $("#geo_data").html('lat: '+location.latitude+'<br />long: '+location.longitude);
                        initialLocation = new google.maps.LatLng(location.latitude, location.longitude);
                        map.setCenter(initialLocation);
                        setMarker(initialLocation);           
                                                  
                var latitude = location.latitude;
                var longitude = location.longitude;
               
                $.ajax({
		        url: "save_location.php",
		        data: "value="+latitude+longitude,
		        type: 'POST',
		        dataType: 'html'

	});
                });
            }

                // set marker
                function setMarker(initialName) {
                    var marker = new google.maps.Marker({
                        draggable: true,
                        position: initialName,
                        map: map,
                        title: "คุณอยู่ที่นี่."
                    });
                    google.maps.event.addListener(marker, 'dragend', function(event) {
                        $("#geo_data").html('lat: '+marker.getPosition().lat()+'<br />long: '+marker.getPosition().lng());
                    });
                }
            }

            $(document).ready(function() {
                initialize();
            });

         
        
        </script>
  

    </body>
</html>
ความคิดเห็นที่ 1
อ้างอิงความเห็น


bamossza


พลังงานการช่วยเหลือ
( Level 3 )


คะแนนขอบคุณจากสมาชิก
( 16 )


ตอบกระทู้ ( 112 )
เขียนบทความ ( 28 )

ส่งค่าจากหน้า frontend ผ่าน ajax ผมก็เห็นคุณส่งค่าไปแล้วไม่ใช่หรอครับ ติด error อะไรหรือเปล่าครับ

$.ajax({
  url: "save_location.php",
  data: "value="+latitude+longitude,
  type: 'POST',
  dataType: 'html'
});

ถ้า Method Type Get ส่งค่าผ่าน query string ก็ได้นะครับ

$.ajax({
  url: "save_location.php?lat=" + latitude + "&lng=" + longitude,
  type: 'GET',
  dataType: 'json'
});​

ส่วนของ PHP ก็รับค่าได้เลยครับ

รับค่าแบบนี้เลยครับ

$_GET["lat"];
$_GET["lng"];​

ลองดูครับ

แสดงความเห็น

อัพโหลดรูปภาพ..คลิก *** เพื่อความปลอดภัยในด้านสิทธิ ไม่อนุญาติให้นำลิ้งรูปภาพจากที่อื่นมาแปะ ถ้าพบเจอ ลบกระทู้ ทันที ***

Back to Top