简体   繁体   English

在另一个内部使用变量

[英]Using a variable inside another

I'm trying to pass a value via PHP to a Javascript function but it isn't working, the value doesn't appear to be going through. 我正在尝试通过PHP将值传递给Javascript函数,但是它不起作用,该值似乎没有通过。 When I replace the variable with static text, it works fine. 当我用静态文本替换变量时,它可以正常工作。

I'm calling the function via a PHP file like so.. 我正在通过这样的PHP文件调用该函数。

<?php
if (!empty($data['geocode'])) {
    echo '<body onload="initializeGM(\'' . $data['geocode'] . '\');">';
} else {
    echo '<body>';
}
?>

$data['geocode'] contains both the lat and lng as a string. $data['geocode']包含latlng作为字符串。

Here is the Javascript code.. 这是Javascript代码。

function initializeGM(geocode) {
  var myOptions = {
    zoom: 8,
    center: new google.maps.LatLng(geocode),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}

I have added an alert(geocode) into the JS function to test and it works fine. 我已经在JS函数中添加了一个alert(geocode)进行测试,并且工作正常。

How can I make it so I can pass the geocode variable in and it works? 如何做到这一点,以便可以将geocode变量传入并起作用?

Assuming I've understood your question correctly, geocode is a PHP variable and you want to pass that into a JavaScript function call. 假设我已经正确理解了您的问题,则geocode是一个PHP变量,您希望将其传递给JavaScript函数调用。 If that's the case, you need to echo out the value of the PHP variable: 在这种情况下,您需要echo显PHP变量的值:

new google.maps.LatLng(<?php echo($geocode); ?>);

However , note that the LatLng constructor expects 2 arguments of type Number (and an optional boolean argument), and you are only trying to pass in one argument. 但是 ,请注意, LatLng构造函数需要2个类型为Number的参数(和一个可选的布尔参数),并且您仅尝试传递一个参数。 Although I may have completely misunderstood your intentions. 尽管我可能完全误解了您的意图。

Edit (see comments) 编辑 (请参阅评论)

I didn't notice that you were passing geocode as an argument to the initializeGM function. 我没有注意到您将geocode作为参数传递给initializeGM函数。 If you were actually trying to pass your PHP variable into that function you need to modify the call to the JavaScript function to use the PHP variable: 如果您实际上试图将PHP变量传递给该函数,则需要修改对JavaScript函数的调用以使用PHP变量:

initializeGM(<?php echo($geocode); ?>);

The above "however" paragraph still applies though. 上面的“然而”段落仍然适用。 Your code is likely to throw an error as lng will be undefined in the LatLng constructor function. 您的代码很可能会引发错误,因为LatLng构造函数中undefined lng

Edit again (see edited question) 再次修改 (请参见修改后的问题)

What you have appears at first glance that it should result in something along these lines (as you've stated that geocode contains a string): 乍一看,您所看到的将导致沿这些方向的变化(正如您所说的geocode包含一个字符串):

<body onload="initializeGM('somelat,somelng');">

There is nothing wrong with that in theory (a string will be passed into initializeGM ) but the problem is that the function expects two arguments of type Number, not one argument of type String: 从理论上讲,这没有错(将一个字符串传递给initializeGM ),但是问题在于该函数需要两个Number类型的参数,而不是String类型的一个参数:

<body onload="initializeGM(100, 200);"> <!--Use real lat-lng values here!-->

That would require a change to the JavaScript function: 这将需要更改JavaScript函数:

function initializeGM(lat, lng) { //2 arguments
    var myOptions = {
        zoom: 8,
        center: new google.maps.LatLng(lat, lng), //Pass 2 arguments to constructor
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}

The google.maps.LatLng constructor takes a minimum of two arguments, lat:number and lng:number . google.maps.LatLng构造函数至少接受两个参数lat:numberlng:number You are only passing one. 您只通过了一个。

Assuming $data['geocode'] looks something like 1.234,5.678 , try this... 假设$data['geocode']看起来像1.234,5.678 ,尝试这个...

  1. Change your JS function to 将您的JS函数更改为

     function initializeGM(lat, lng) { var myOptions = { zoom: 8, center: new google.maps.LatLng(lat, lng), mapTypeId: google.maps.MapTypeId.ROADMAP } var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); } 
  2. Change the call to 更改为

     <?php if (!empty($data['geocode'])) : $coords = explode(',', $data['geocode']); ?> <body onload="initializeGM(<?php printf('%.15f, %.15f', $coords[0], $coords[1]) ?>)"> <?php else : ?> <body> <?php endif ?> 

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

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