Google Maps API - 将 php 变量设置为 javascript 变量

发布于 2024-10-09 18:48:51 字数 2026 浏览 3 评论 0原文

我正在使用 Google Maps API 和 W3C 标准来获取用户的地理位置。我将其设置为基础 - 使用 [Google 文档][1] 中的代码。

现在我正在将其集成到我的网站中,并需要将其输入到我的 mysql 数据库中。我想将 PHP 变量设置为 JavaScript 变量(基本上将 PHP 变量设置为纬度和经度)。但我没有成功做到这一点。

编辑:

基本上我想使 $phpvar = $jsvar 。创建一个 php 变量 = 我的 javascript 变量。

这是代码:

var initialLocation;
var siberia = new google.maps.LatLng(60, 105);
var newyork = new google.maps.LatLng(40.69847032728747, -73.9514422416687);
var browserSupportFlag =  new Boolean();

function initialize() {
  var myOptions = {
    zoom: 6,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

  // Try W3C Geolocation (Preferred)
  if(navigator.geolocation) {
    browserSupportFlag = true;
    navigator.geolocation.getCurrentPosition(function(position) {
      initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
      map.setCenter(initialLocation);
    }, function() {
      handleNoGeolocation(browserSupportFlag);
    });
  // Try Google Gears Geolocation
  } else if (google.gears) {
    browserSupportFlag = true;
    var geo = google.gears.factory.create('beta.geolocation');
    geo.getCurrentPosition(function(position) {
      initialLocation = new google.maps.LatLng(position.latitude,position.longitude);
      map.setCenter(initialLocation);
    }, function() {
      handleNoGeoLocation(browserSupportFlag);
    });
  // Browser doesn't support Geolocation
  } else {
    browserSupportFlag = false;
    handleNoGeolocation(browserSupportFlag);
  }

  function handleNoGeolocation(errorFlag) {
    if (errorFlag == true) {
      alert("Geolocation service failed.");
      initialLocation = newyork;
    } else {
      alert("Your browser doesn't support geolocation. We've placed you in Siberia.");
      initialLocation = siberia;
    }
    map.setCenter(initialLocation);

} 任何

帮助都会很棒!请具体说明,因为我是这个 Google 地图 API 的新手。

谢谢! :)

I am using the Google Maps API and the W3C Standard to get a user's geolocation. I have it set up as basics - using the code from [Google's documentation][1].

Now I'm integrating this into my website and need it imputed into my mysql database. I want to set a PHP variable as a JavaScript variable (basically setting a PHP variable as the latitude and longitude). But I'm having no success of doing this.

EDIT:

Basically I want to make $phpvar = $jsvar. Making a php variable = my javascript variable.

Here's the code:

var initialLocation;
var siberia = new google.maps.LatLng(60, 105);
var newyork = new google.maps.LatLng(40.69847032728747, -73.9514422416687);
var browserSupportFlag =  new Boolean();

function initialize() {
  var myOptions = {
    zoom: 6,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

  // Try W3C Geolocation (Preferred)
  if(navigator.geolocation) {
    browserSupportFlag = true;
    navigator.geolocation.getCurrentPosition(function(position) {
      initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
      map.setCenter(initialLocation);
    }, function() {
      handleNoGeolocation(browserSupportFlag);
    });
  // Try Google Gears Geolocation
  } else if (google.gears) {
    browserSupportFlag = true;
    var geo = google.gears.factory.create('beta.geolocation');
    geo.getCurrentPosition(function(position) {
      initialLocation = new google.maps.LatLng(position.latitude,position.longitude);
      map.setCenter(initialLocation);
    }, function() {
      handleNoGeoLocation(browserSupportFlag);
    });
  // Browser doesn't support Geolocation
  } else {
    browserSupportFlag = false;
    handleNoGeolocation(browserSupportFlag);
  }

  function handleNoGeolocation(errorFlag) {
    if (errorFlag == true) {
      alert("Geolocation service failed.");
      initialLocation = newyork;
    } else {
      alert("Your browser doesn't support geolocation. We've placed you in Siberia.");
      initialLocation = siberia;
    }
    map.setCenter(initialLocation);

}
}

Any help would be great! Please be specific with me because I'm new to this Google Map API.

Thanks! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

庆幸我还是我 2024-10-16 18:48:51

您可以将 PHP 变量存储为 Javascript 变量,如下所示:

<script type="text/javascript">
    var geoloc = '<?php echo $geoloc; ?>';
</script>

然后您可以根据需要使用该变量。如果您可以提供示例代码,我会尽力让您更准确地了解。

You can store a PHP variable as a Javascript variable like this:

<script type="text/javascript">
    var geoloc = '<?php echo $geoloc; ?>';
</script>

You can then use this variable as required. If you can give an example code, I would try to let you know more accurately.

“想要将 PHP 变量设置为 Javascript 变量”。这还不太清楚吗?

如果您想要 PHP->Javascript,那么很简单:

var myVar = <?php echo json_encode($some_php_var) ?>;

对于 Javascript->PHP,则有点复杂。您必须执行一些 DOM 黑客操作才能将值插入表单值/提交表单,或者执行一些 AJAX 工作。

"want to set a PHP variable as a Javascript variable". This isn't quite clear?

If you want to go PHP->Javascript, then it's simple:

var myVar = <?php echo json_encode($some_php_var) ?>;

For Javascript->PHP, it's a bit more complicated. You'd have to do some DOM hacking to insert the value into a form value/submit form, or do some AJAX work.

感情洁癖 2024-10-16 18:48:51

这就是我目前为 Lat 和 Lng 设置 php 变量,然后使用 Javascript 调用它们的方式:

function initMap() {
  var geoLat = parseFloat("<?php echo h($location['lat']); ?>");
  var geoLng = parseFloat("<?php echo h($location['lng']); ?>");
  var theLocation = {lat: geoLat, lng: geoLng};

var map = new google.maps.Map(
    document.getElementById('map'), {
    zoom: 16,
    center: theLocation
});

我正在使用 parseFloat() 函数
parseFloat

这有助于解决 Lat/Lng有些人可能会遇到 VS Code 中的对象字面量错误消息和标记/lint 错误。我希望这对您和其他人有所帮助。

This is how I currently set the php variables for Lat and Lng and then call them with Javascript:

function initMap() {
  var geoLat = parseFloat("<?php echo h($location['lat']); ?>");
  var geoLng = parseFloat("<?php echo h($location['lng']); ?>");
  var theLocation = {lat: geoLat, lng: geoLng};

var map = new google.maps.Map(
    document.getElementById('map'), {
    zoom: 16,
    center: theLocation
});

I'm using the parseFloat() function
parseFloat

This helped solve the Lat/Lng Object Literal error messages and markup / lint errors in VS Code that some may be experiencing. I hope this helps you and others.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文