0

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>

4

1 回答 1

5

您可以使用 jquery 轻松完成此操作(无需页面加载或表单提交)-

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 
        }
   });


 }

并在 php 文件中:

  <?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 


  ?>

要了解更多信息并下载 Jquery,请访问:http: //jquery.com/download/

另一种方法是没有 Jquery(页面将重定向到 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>

并在您的 map2.php 中写入-

 <?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

  ?>

在您的表单方法中,我建议使用POST而不是GET。如果您进行了此更改,那么在您的 map2.php 中只需将GET更改为POST即可。要了解使用 POST 的原因,请访问链接 1链接 2

于 2013-09-16T07:23:16.230 回答