从地址变量到 google 映射
From address variables to google maps
当人们在我的网站上注册时,他们会输入他们的街道名称。我想插入一张可以生成他们位置的地图。我有 PHP 变量,但如何将它们插入到 Google Javascript 代码中,以便在地图上生成特定位置。我已经尝试阅读一些 Google 地图开发人员的文章,并制作了我自己的 API 密钥,但我不知道如何从这一点开始。
$sql = "SELECT * FROM submitted WHERE id ='$id'";
$sql_run = $db->query($sql);
$row = mysqli_fetch_assoc($sql_run);
$city= $row["city"];
$street= $row["streetname"];
要在地图上绘制地址,请按照以下步骤操作:
第 1 步:包括 google 地图 API 脚本文件
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_KEY"></script>
第 2 步:创建地图元素
<div id="map" style="height: 550px; width: 100%;"></div>
第 3 步:声明地图元素和地址地理编码以绘制标记的代码
<script type="text/javascript">
// ====== Create map objects ======
var latlng = new google.maps.LatLng(-34.397, 150.644);
var mapOptions = {
zoom: 10,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var geo = new google.maps.Geocoder();
var map = new google.maps.Map(document.getElementById("map"), mapOptions);
var bounds = new google.maps.LatLngBounds();
// ====== Geocoding ======
function getAddress(search) {
geo.geocode({address:search}, function (results,status)
{
// If that was successful
if (status == google.maps.GeocoderStatus.OK) {
// Lets assume that the first marker is the one we want
var p = results[0].geometry.location;
var lat=p.lat();
var lng=p.lng();
// Output the data
createMarker(search,lat,lng);
}
// ====== Decode the error status ======
else {
alert('Something went wrong. Please Try again.');
}
}
);
}
// ======= Function to create a marker
function createMarker(location,lat,lng) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(lat,lng),
map: map
});
google.maps.event.addListener(marker, 'click', function() {
alert("Your Location : "+location);
});
bounds.extend(marker.position);
}
</script>
第 4 步:从数据库中获取地址并使用给定的地址变量在脚本中调用地址解析函数 getAddress('address from db')
。
<?php
$city = $row["city"];
$street = $row["streetname"];
?>
<script>
getAddress('<?php echo $street.','.$city; ?>');
</script>
希望您能轻松理解这段代码。如果此代码有问题,请告诉我。
当人们在我的网站上注册时,他们会输入他们的街道名称。我想插入一张可以生成他们位置的地图。我有 PHP 变量,但如何将它们插入到 Google Javascript 代码中,以便在地图上生成特定位置。我已经尝试阅读一些 Google 地图开发人员的文章,并制作了我自己的 API 密钥,但我不知道如何从这一点开始。
$sql = "SELECT * FROM submitted WHERE id ='$id'";
$sql_run = $db->query($sql);
$row = mysqli_fetch_assoc($sql_run);
$city= $row["city"];
$street= $row["streetname"];
要在地图上绘制地址,请按照以下步骤操作:
第 1 步:包括 google 地图 API 脚本文件
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_KEY"></script>
第 2 步:创建地图元素
<div id="map" style="height: 550px; width: 100%;"></div>
第 3 步:声明地图元素和地址地理编码以绘制标记的代码
<script type="text/javascript">
// ====== Create map objects ======
var latlng = new google.maps.LatLng(-34.397, 150.644);
var mapOptions = {
zoom: 10,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var geo = new google.maps.Geocoder();
var map = new google.maps.Map(document.getElementById("map"), mapOptions);
var bounds = new google.maps.LatLngBounds();
// ====== Geocoding ======
function getAddress(search) {
geo.geocode({address:search}, function (results,status)
{
// If that was successful
if (status == google.maps.GeocoderStatus.OK) {
// Lets assume that the first marker is the one we want
var p = results[0].geometry.location;
var lat=p.lat();
var lng=p.lng();
// Output the data
createMarker(search,lat,lng);
}
// ====== Decode the error status ======
else {
alert('Something went wrong. Please Try again.');
}
}
);
}
// ======= Function to create a marker
function createMarker(location,lat,lng) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(lat,lng),
map: map
});
google.maps.event.addListener(marker, 'click', function() {
alert("Your Location : "+location);
});
bounds.extend(marker.position);
}
</script>
第 4 步:从数据库中获取地址并使用给定的地址变量在脚本中调用地址解析函数 getAddress('address from db')
。
<?php
$city = $row["city"];
$street = $row["streetname"];
?>
<script>
getAddress('<?php echo $street.','.$city; ?>');
</script>
希望您能轻松理解这段代码。如果此代码有问题,请告诉我。