簡體   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