gpt4 book ai didi

javascript - 将纬度和经度作为 url 参数传递

转载 作者:行者123 更新时间:2023-11-28 04:57:26 24 4
gpt4 key购买 nike

我开始使用 API 工具,我的 map 需要通过传递 URL 参数来动态更改,这是我的 URL,下面是不起作用的代码。检索参数的 PHP 部分可以工作,但将这些参数传输到 JS 变量则不行,“mylat”就是行不通的示例

希望有人能帮助我谢谢

http://www.beleuramyhome.org.au/myvillaloc.php?urllat=-38.22173&urllong=145.0732905&urlvilla=177

<script type="text/javascript">

var map;

var mylat = <?php echo json_encode($urllat); ?>; //-38.22173;
var mylong = 145.0732905;
var myvilla = '177';

function initMap()
{
var myLatLng = {lat: mylat, lng: mylong};
map = new google.maps.Map(document.getElementById('map'), {
center: myLatLng,
zoom: 18,mapTypeId: 'satellite'
});

==========================已解决====使用PHP+Javascript

<?php
$file_handle = fopen("villaloc.csv", "r");
$villa = $_GET['villa'];

// if visitor wants to start from their current location
$mylat = $_GET['mylat'];
$mylong = $_GET['mylong'];
$start = $_GET['start'];
$walk = $_GET['walk'];

$foundrecord = 0;
while (!feof($file_handle) )
{
$line_of_text = fgetcsv($file_handle, 128);

if ($line_of_text[0] == $villa)
{
$foundrecord = 1;
break;
}
}
fclose($file_handle);

if($foundrecord == 0)
{
/* Redirect browser */
header("Location: https://squadron-125.org.il/villanotfound.html");

/* Make sure that code below does not get executed when we redirect. */
exit;
}
?>

<script>

var loricket = 0;

var VillaNo = parseFloat(<?php echo $line_of_text[0] ?>);
var mylat = parseFloat(<?php echo $line_of_text[1] ?>);
var mylong = parseFloat(<?php echo $line_of_text[2] ?>);
loricket = parseInt(<?php echo $line_of_text[3] ?>);

最佳答案

使用$_GET从查询字符串中获取值

您的纬度和经度也是字符串,因为它来自 URL 查询字符串尝试将其转换为 float ,如下所示

 var map;

var mylat = parseFloat(<?php echo $_GET['urllat'] ?>); //-38.22173;
var mylong = parseFloat(<?php echo $_GET['urllong'] ?>);
var myvilla = '177';

function initMap()
{
var myLatLng = {lat: mylat, lng: mylong};
map = new google.maps.Map(document.getElementById('map'), {
center: myLatLng,
zoom: 18,mapTypeId: 'satellite'
});

关于javascript - 将纬度和经度作为 url 参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42431427/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com