gpt4 book ai didi

php - Google Maps API v3 - 简单但从 php/mysql 添加地址地理编码

转载 作者:行者123 更新时间:2023-11-29 07:05:03 25 4
gpt4 key购买 nike

我有一个简单的 hello world 版本的 google map api javascript v3,它适用于静态地理编码。我的数据是普通地址(即“30 rockefeller center, New York, NY”)。

此数据是从 mysql 数据库中使用 php 调用的。我可以通过类似这样的方式获取页面上的地址...出于本文的目的,假设这将包含所有信息:地址、城市、州、邮政编码

<?php echo $row_query_details['address'];?>

因此,我需要对 map 的此信息进行地理编码。我对 mysql 和 php 很熟悉,但对 javascript 就不太熟悉了。我已经试错了几天并对此进行了研究。

我到处寻找有效的样本或例子,感觉这个相对简单的问题一定已经被问过并回答过很多次了,但我还是想不通!

提前致谢。

这是我正在使用的代码:

            <!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map_canvas { height: 100% }
</style>
<script type="text/javascript"
src="http://maps.googleapis.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
function initialize() {
var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
var marker = new google.maps.Marker({
position: latlng,
title:"Hello World!"
});

// To add the marker to the map, call setMap();
marker.setMap(map);
}

</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="width:500px; height:500px"></div>
</body>
</html>

最佳答案

你好,周年快乐(关于你的问题和教程)。我是一名网络开发人员,必须感谢您。我使用本教程是因为我目前无法通过提交进行地理编码(需要从存储在数据库中的地址中提取纬度/经度),因为我正在咨询现有站点并且对后端代码的访问权限极少。

要回答有关标记交互性的问题,只需几个简单的步骤:

1) 提供一些内容。这是在声明地理编码器 (var geocoder;) 之前完成的:

var contentString = 
'line 1'+
'line 2';

2) 在标记声明 ( var marker = new google.maps.Marker({ ) 下,您需要声明 infowindow 并添加事件监听器:

var infowindow = new google.maps.InfoWindow({
content: contentString
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});

编辑:

通过扩展您的方法,我已经成功地从 MySQL 数据库中提取数组信息。我的方法在这里:

1) 下面是基本的 php:

include (dbinfo.php)
$result = mysql_query( 'SELECT * FROM table')
$count = 0

2) 设置谷歌地图 API:

<script type="text/javascript"
src="http://maps.googleapis.com/maps/api/js?key=yourkey&sensor=false">
</script>

3) 下面的地理编码设置:

<script type="text/javascript">
var geocoder;
var map;
function initialize() {
var latlng = new google.maps.LatLng(yourlat, yourlong); //This is to center the map
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
<?php //Starts while loop so all addresses for the given information will be populated.
while($row = mysql_fetch_array($result)) //instantiates array
{ ?>
geocoder = new google.maps.Geocoder();
var address = '<?php echo $row['address'].', '.$row['city'].', '.$row['state'].', '.$row['zip'].', '.$row['country']; ?>';
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
//marker generation is done though adding the primary ID to the "marker" variable, so if address "1" has the primary ID of "1", your marker would read "marker1"
var marker<?php print $row['primaryID']; ?> = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
title: "This can be a php variable or whatever you like. It is displayed on hover."
});

//var contentString manages what is seen inside of the popup
var contentString =
'line 1'+
'line 2';

var infowindow = new google.maps.InfoWindow({
content: contentString
});
google.maps.event.addListener(marker<?php print $row['primaryID']; ?>, 'click', function() {
infowindow.open(map,marker<?php print $row['primaryID']; ?>);
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
<?php
$count++;
} //ends while
?>
}

/*end javascript*/
</script>

再次感谢您发帖。这非常有用。

关于php - Google Maps API v3 - 简单但从 php/mysql 添加地址地理编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7845395/

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