dsm0688 2015-03-12 00:22
浏览 74
已采纳

在数据库上发布long和lat

I am having some problems with my current code.

I am not able to receive the results of the longitude and latitude from the address text field. Once I receive that I want to put the longitude and latitude into my database. I am not sure if that is possible because the value that it is checking(address) is in the form tag.

Any input would help... thanks

function initialize() {
  var address = (document.getElementById('address'));
  var autocomplete = new google.maps.places.Autocomplete(address);
  autocomplete.setTypes(['geocode']);
  google.maps.event.addListener(autocomplete, 'place_changed', function() {
    var place = autocomplete.getPlace();
    if (!place.geometry) {
      return;
    }

    var address = '';
    if (place.address_components) {
      address = [
        (place.address_components[0] && place.address_components[0].short_name || ''), (place.address_components[1] && place.address_components[1].short_name || ''), (place.address_components[2] && place.address_components[2].short_name || '')
      ].join(' ');
    }
  });
}

function codeAddress() {
  geocoder = new google.maps.Geocoder();
  var address = document.getElementById("address").value;
  geocoder.geocode({
    'address': address
  }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {

      alert("Latitude: " + results[0].geometry.location.lat() + "Longitude: " + results[0].geometry.location.lng());
    } else {
      alert("Geocode was not successful for the following reason: " + status);
    }
  });
}
google.maps.event.addDomListener(window, 'load', initialize);
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script>
<form method="post" action="formconnect.php" ;>

  <h2>Add new location</h2>
  <p>Please add your company information within this form</p>


  Name:
  <input type="text" name="name">
  <br>Hours:
  <input type="text" name="hours">
  <br>Baseball:
  <select name="baseball">
    <option value="" selected="selected"></option>
    <option value="Yes">Yes</option>
    <option value="No">No</option>
    <option value="Coming Soon">Coming Soon</option>
  </select>
  <br>Basketball:
  <select name="basketball">
    <option value="" selected="selected"></option>
    <option value="Yes">Yes</option>
    <option value="No">No</option>
    <option value="Coming Soon">Coming Soon</option>
  </select>
  <br>Hockey:
  <select name="hockey">
    <option value="" selected="selected"></option>
    <option value="Yes">Yes</option>
    <option value="No">No</option>
    <option value="Coming Soon">Coming Soon</option>
  </select>
  <br>Golf:
  <select name="golf">
    <option value="" selected="selected"></option>
    <option value="Yes">Yes</option>
    <option value="No">No</option>
    <option value="Coming Soon">Coming Soon</option>
  </select>
  <br>Soccer:
  <select name="soccer">
    <option value="" selected="selected"></option>
    <option value="Yes">Yes</option>
    <option value="No">No</option>
    <option value="Coming Soon">Coming Soon</option>
  </select>
  <br>Address:
  <input type="text" name="address">
  <br>
  <input type='hidden' value='' name='lat' />
  <input type='hidden' value='' name='lng' />
  </br>
  </br>

  <input onClick="codeAddress()" type="submit"></input>



</form>

Here is the formconnect.php too

$sql="INSERT INTO markers (name, hours, baseball, basketball, hockey, golf, soccer, address, lat, lng)
VALUES
('$_POST[name]','$_POST[hours]','$_POST[baseball]','$_POST[basketball]','$_POST[hockey]','$_POST[golf]','$_POST[soccer]','$_POST[address]','$_POST[lat]','$_POST[lng]')";

if (!mysqli_query($con,$sql))
{
    die('Error: ' . mysqli_error($con));
}
echo "one record added";
mysqli_close($con);
</div>
  • 写回答

2条回答 默认 最新

  • dongyi7669 2015-03-12 11:16
    关注

    You have some subtle errors in your code

    1. You did not specify code that will give the longitude and latitude fields their values. I know you are getting the values from google, but there is nowhere you are passing those values to your fields. We will see what we can do about that.

    One in your html, give id to each of the fields you want to reference through getElementById(), including the form itself. Like so ( I am leaving out other fields since they are unaffected),

        <form method="post" action="formconnect.php" id="form1">
        .
        .
        .
        <input type="text" name="address" id="address" />
    
       <input type='hidden' value='' name='lat' id="lat" />
    
       <input type='hidden' value='' name='lng' id="lng" />
    

    Let us also change the submit button to tag this has nothing to do with the problem here. Just forthe sake of html5 like so,

        <button onClick="codeAddress()" type="button" name="upload" >SUBMIT</button>
    

    Changing the type of the button to 'button' instead of 'submit' makes sure the form does not get submitted until you do so in the code.

    Now the JS,

        <script>
    
        var codeAddress = function () {
    
        geocoder = new google.maps.Geocoder();
    
        var address = document.getElementById("address").value;
    
        geocoder.geocode(
            {'address': address}, 
    
            function (results, status) {
    
               if (status == google.maps.GeocoderStatus.OK) {
    
                   document.getElementById("lat").value = results[0].geometry.location.lat();
    
                   document.getElementById("lng").value = results[0].geometry.location.lng();
    
                   document.getElementById("form1").submit();
    
              }else{
    
                alert("Geocode was not successful for the following reason: " + status );
              }
           });
        };
    
        </script>
    

    Notice that the code that gives values to the latitude and longitude fields have been move to within the callback, specifically when the result is 'OK'. The form submission also is move there to ensure the form only gets submitted when the response from google is 'OK'

    1. Again I guess the other function 'initialize' implements autocomplete. I changed the place where you call it. I just call it immediately it is created like so,

         var initialize = function() {
      
             var address = (document.getElementById('address'));
      
             var autocomplete = new google.maps.places.Autocomplete(address);
      
             autocomplete.setTypes(['geocode']);
      
             google.maps.event.addListener(autocomplete, 'place_changed', function() {
      
                var place = autocomplete.getPlace();
      
                if (!place.geometry) {
                  return;
                }
      
                var address = '';
      
                if (place.address_components) { address = [
                   (place.address_components[0] && place.address_components[0].short_name || ''), (place.address_components[1] && place.address_components[1].short_name || ''), (place.address_components[2] && place.address_components[2].short_name || '')
                  ].join(' ');
                }
             });
        }();
      

    Notice the () operator at the end of the function ? That is what calls it immediately after declaration. Try this and let's see how things go.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?