我正在尝试显示从中调用的地址 我的sql数据库,我无法显示我一直收到此错误的地址"地理编码由于以下原因而无法成功Zero_Results"
我试图在一个变量$地址中添加完整地址,而不是使用三个变量$ address,$ county,$ county但仍然没有 工作,我无法理解。
我想将地址显示为标记,如图所示。
每当我在我的数据库中添加新地址时,我都会在地图中添加新标记。
任何帮助,
提前致谢。
以下是我正在使用的代码:
$result = mysqli_query($con,"SELECT * FROM `test`") or die ("Error:
".mysqli_error($con));
while ($row = mysqli_fetch_array($result))
{
$address = $row['address'];
$county = $row['County'];
$country = $row['Country'];
}
mysqli_close($con);
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?
key=mykey&sensor=false">
</script>
<script type="text/javascript">
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(
53.41291,-8.243889999999965);
var address = '<?php echo $address.', '.$country.', '.$county; ?
>';
var myOptions = {
zoom: 6,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
geocoder.geocode( { 'address': address}, function(results,
status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert("Geocode was not successful for the following reason:
" + status);
}
});
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="width:500px; height:500px"></div>
</body>
</html>
答案 0 :(得分:2)
我的问题解决了,我的解决方案是:
<script type="text/javascript">
var geocoder;
var map;
function initialize(address) {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(
53.41291,-8.243889999999965);
var myOptions = {
zoom: 6,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
<?php
require('connection.php');
$result = mysqli_query($con,"SELECT * FROM `test`") or die
("Error: ".mysqli_error($con));
$count = 0;
?>
<?php //Starts while loop so all addresses for the given
// information will be populated.
$addresscounting=0;
while($row = mysqli_fetch_assoc($result)) //instantiates
// array
{ ?>
var address = "<?php echo $address ?>";
geocoder.geocode( { 'address': address}, function(results,
status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert("Geocode was not successful for the following reason:
" + status);
}
});
<?php
$count++;
} //ends while
?>
}
</script