gpt4 book ai didi

php - 在 JavaScript 中为 Google Maps V2 使用 PHP 变量

转载 作者:可可西里 更新时间:2023-10-31 23:24:08 26 4
gpt4 key购买 nike

我目前使用的是 Google map 版本 2(抱歉使用旧版本,请不要建议我使用 V3,因为这是我们的要求)。基本上,此页面应允许用户添加地点并选择该地点的类型(例如餐厅、酒吧、学校等)。这些信息将添加到数据库中并用于切换。添加地点现在可以正常工作,需要重定向到另一个 php 页面 (testmap.php),以便它将在 map 中显示标记。但是切换需要来自下拉列表中所选类型的信息。我的问题是我不知道如何将类型与每个标记“关联”。我不知道如何从“类型”下拉列表中传输数据。

附言

  1. 倒数第四行是在我的 addNewMarker 函数 onSubmit 中传输地址数据的代码。

  2. 上面的 PHP 代码是下拉列表,其中包含上述“类型”的可能值。

  3. 总而言之,我需要有关如何从下拉列表中获取标记类型并与 addNewMarker 函数一起传输的实际代码,以便 testmap.php 可以使用数据库中的值并可以轻松切换。

非常感谢那些愿意帮助我的人!

 <script type="text/javascript">

if(GBrowserIsCompatible()) {
map_compatible = true;
}

/* Initialize the map this will be called when the document is loaded from: <body onLoad="initialize_map();"> */

function initialize_map() {
if( map_compatible ) {
}

function addNewMarker(newAddress){
var set;
var lat;
var longi;
if(set==null){
set=1;
geocoder = new GClientGeocoder();
geocoder.getLatLng(newAddress, function(point){
if(!point){
alert(address + " not found");
}else{
lat = point.y;
longi = point.x;
alert("The latitude of " + newAddress + " is " + lat + " and longitude is " + longi);
default_address.push([latitude,longitude]);
location.href="testmap.php?lat="+lat+"&longi="+longi+"&set="+set+"&newAdd="+newAddress;
}
});
}
}

function getType(type){
//markertype = type;
document.write("Hello");
}


</script>



</head>

<body onLoad="initialize_map();">

<div id="main-wrapper">
<div id="main-padding">

<div id="map_canvas"></div>
<form name="markertype_form" method = "POST" action = "addMarker.php" class="form">
<?php
@$submit = $_POST['view'];
$selectedtype = '';
if(isset($_GET)){
$connect = mysql_connect("localhost","root","abc123");
mysql_select_db("mapping");
$query="SELECT id,typename FROM markertypes ORDER BY typename";

$result = mysql_query ($query);
echo "<select name='types'>";
$types = strip_tags(@$_POST['types']);
echo "<option disabled>---------------------Select---------------------</option>";
while($nt=mysql_fetch_array($result)){
$selected = false;
// check if the current value equals the value submited
if($_POST['types'] == $nt['id']){
$selected = true;
$selectedtype = $nt['typename'];
}
// show selected attribute only if $selected is true
echo "<option value='{$nt['id']}' ". ($selected ? "selected" : "") .">{$nt['typename']}</option>";
}
echo '</select>';
echo "</select>";
echo "<input type='submit' name ='view' value='View Details'>";// Closing of list box
echo '<br>';
}
?>

</form>
<form name="direction_form" onSubmit="addNewMarker(this.new_address.value); return false;" class="form">

Add markers? Enter the address and we will mark it for you: <input type="text" name="new_address" class="form-field" /><br /><br />

<input type="submit" value=" Mark this place! " class="form-submit" />

</form>

最佳答案

如果您在 php 中的类型选择框中放置一个 id,那么您可以在上面的 javascript 中添加一些代码行。

下面假设“types”选择元素的 id 为“marker-type”:

function addNewMarker(newAddress){
var set;
var lat;
var longi;
var e = document.getElementById("marker-type");
var mtype = e.options[e.selectedIndex].value;
if(set==null){
set=1;
geocoder = new GClientGeocoder();
geocoder.getLatLng(newAddress, function(point){
if(!point){
alert(address + " not found");
}else{
lat = point.y;
longi = point.x;
alert("The latitude of " + newAddress + " is " + lat + " and longitude is " + longi);
default_address.push([latitude,longitude]);
location.href="testmap.php?lat="+lat+"&longi="+longi+"&set="+set+"&newAdd="+newAddress+"&type="+mtype;
}
});
}
}

请注意,在 AddMarker 函数中,我为“mtype”添加了一个新变量,用于选择选择框并获取当前选项值。

然后在下面,在 API 调用 testmap.php 的地方,我添加了类型选择器并为其赋予了“mtype”变量的值。

稍后在您的 testmap.php 代码中,您只需继续使用:

$_GET['type'] 

关于php - 在 JavaScript 中为 Google Maps V2 使用 PHP 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9562516/

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