繁体   English   中英

从Javascript / Geocoder.geocode()获取纬度和经度,并将其与php一起使用

[英]Get Latitude and Longitude From Javascript/Geocoder.geocode() and using it with php

我需要使用Google地图从某个地址获取经度和纬度,并使用php对其进行操作。

我正在使用这样的代码https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple ,其中包含以下部分:

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 latitude = results[0].geometry.location.lat();
      var longitude = results[0].geometry.location.lng();
      var marker = new google.maps.Marker({
          map: map,
          position: results[0].geometry.location
      });
    } else {
      alert('Geocode was not successful for the following reason: ' + status);
    }
  });
}

我已经在其中添加了纬度和经度变量(按照此问题中的说明进行操作。现在,我需要在php脚本中使用在Java脚本中创建的变量。

我是javascript的新手,所以很抱歉,如果这是一个简单的问题。

一种简单的方法是使用基于jquery的ajax。 不要忘记下载/包含该库。

$.ajax({
   type:"POST",
   url:"yourscript.php",
   data: {type:getlatlong,latitude:latitude,longitude:longitude},
   success: function(){
      alert('sent');
   }
});

在您的PHP上,您可以按以下方式访问信息:

if ($_SERVER["REQUEST_METHOD"]=="POST") {
   if ($_POST["type"]=="getlatlong") {
      $latitude = $_POST["latitude"];
      $longitude = $_POST["longitude"];
      ...
   }

我认为您需要php脚本中的javascript变量.Php是用于访问数据的服务器端语言,您可以使用get或post方法。您可以简单地通过这种方式,首先将javascript变量分配给隐藏的文本字段并使用get或post在php中进行访问方法。您可以像这样分配javascript变量

<form action="yourphp.php" method="post"> <input type="hidden" value="latitude" name="latitude"> <input type="hidden" value="longitude " name="longitude"> </form>

在您的php文件中,访问数据为:

$latitude=$_POST['latitude'] $longitude=$_POST['longitude']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM