dpj96988 2013-09-16 07:00
浏览 66
已采纳

在数据库中插入纬度经度

I am currently working on a code where when the user enters location he will get latitude n longitude however i need to add both lat and long to database. Client side the code is working the html file is given below.

How do I transfer these values for server side in php and add them to database.

    <!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="UTF-8">
    <title>maps</title>
     <style>
     #map_canvas { width:400px; height:450px; }
      </style>
     <script type="text/javascript" src="http://maps.google.com/maps/api/js?                   sensor=false">
     </script>
     <script type="text/javascript">

      var geocoder;
      var map;
      var lat;
      var lng;

       function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-30.070, -51.190);
var myOptions = {
    zoom: 9,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}

    function codeAddress() {
var address = document.getElementById("address").value;
geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker;
        document.getElementById('lat').value = results[0].geometry.location.lat();
        document.getElementById('lng').value = results[0].geometry.location.lng();


    } else {
        alert("Geocode was not successful for the following reason: " + status);
    }
});
  }



   </script>
    </head>

    <body onload="initialize()">
    <form action="map2.php" method="get">

 <div id="map_canvas"></div>
 <div>
    <input id="address" type="textbox" value="">
    <input type="button" value="Localizar!" onclick="codeAddress()"><br>
    Latitude: <input type="text" id="lat"><br>
    Longitude: <input type="text" id="lng"><br>
 </div>
  • 写回答

1条回答 默认 最新

  • duanjie3267 2013-09-16 07:23
    关注

    You can do this easily using jquery( no page load or form submit needed)-

    if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker;
        document.getElementById('lat').value = results[0].geometry.location.lat();
        document.getElementById('lng').value = results[0].geometry.location.lng();
    
    
       // add this
        var latt=results[0].geometry.location.lat();
        var lngg=results[0].geometry.location.lng();
        $.ajax({
            url: "your-php-code-url-to-save-in-database",
            dataType: 'json',
            type: 'POST',
            data:{ lat: lat, lng: lngg }
            success: function(data)
            {                
               //check here whether inserted or not 
            }
       });
    
    
     }
    

    and in the php file :

      <?php
        // file name: your-php-code-url-to-save-in-database.php
       $lat=$_POST['lat'];
       $lng=$_POST['lng'];
    
       // write your insert query here to save $lat and $lng 
    
    
      ?>
    

    to know more and download Jquery visit :http://jquery.com/download/

    Another way is without Jquery( Page will redirect to map2.php):

    <div id="map_canvas"></div>
    <form action="map2.php" method="get">
       <div>
        <input id="address" type="textbox" value="">
        <input type="button" value="Localizar!" onclick="codeAddress()"><br>
        Latitude: <input name="lat" type="text" id="lat"><br>
        Longitude: <input name="lng" type="text" id="lng"><br>
       </div>
    </form>
    

    and in your map2.php write-

     <?php
    
         // file name: map2.php
    
       $lat=$_GET['lat'];
       $lng=$_GET['lng'];
    
       // write your insert query here to save $lat and $lng 
       //get your mysql db connection ready
    
        $sql="insert into table_name (latitude, longitude) values('".$lat."','".$lng.")";
        $command=mysql_query($sql);
    
        // close databse connection  and everythig is done then redict to any page.
        //check your database table to see the inserted value
    
      ?>
    

    In your form method I would suggest to use POST instead of GET .If you made this change then in your map2.php just change GET to POST only. To know the reason of using POST visit Link 1 and Link 2

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?