google 地图上的地址到数据库
Address into database on google map
我的数据库中有一些人的地址(在 table 命名任务中,该字段是地址)
我还有一个功能地理编码器,可以将此地址更改为经度和纬度,以便在我的地图上绘制标记。
唯一我不明白该怎么做的是从我的数据库中获取我的地址并将其用于我的地图
这是我的代码:(编辑)
我想从我的数据库中获取地址并将其放入我的地理编码器函数的 'adress' 字段中。但这可能吗?如果是怎么办?
感谢大家的帮助。 (希望我清楚^^')
编辑:
我的.js
<script type="text/javascript">
// When the window has finished loading create our google map below
var map;
var geocoder;
function initialize() {
geocoder = new google.maps.Geocoder();
var mapOptions = {
zoom: 14
};
map = new google.maps.Map(document.getElementById('map-geo'), mapOptions);
if(navigator.geolocation) {
var circle = new google.maps.Circle({
center: new google.maps.LatLng(52.376018, 4.901962),
radius: 500,
map: map
});
navigator.geolocation.getCurrentPosition(function(position) {
var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
accuracy = parseFloat(position.coords.accuracy);
map.setZoom(14);
circle.setCenter(pos);
circle.setRadius(accuracy);
map.fitBounds(circle.getBounds());
var infowindow = new google.maps.InfoWindow({
map: map,
position: pos,
content: 'Voici votre géolocalisation'
});
//map.setCenter(pos);
}, function() {
circle.setMap(null);
handleNoGeolocation(true);
});
} else {
// Browser doesn't support Geolocation
handleNoGeolocation(false);
}
$.ajax({ //create an ajax request to a page that prints the address.
url: "addMarkers.php", //url for the address page
success: function(result){
var adresse = result; //create a variable and give it the value of the response data
codeAddress(adresse); //decode the address using codeAddress into Lat and Lon
}
});
codeAddress(<?php echo $adresse; ?>);
//codeAddress();
}
function codeAddress(adresse) {
geocoder.geocode( { 'address': "61 rue de la libération, 44230, saint sebastien sur loire"}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
function handleNoGeolocation(errorFlag) {
if (errorFlag) {
var content = 'Error: The Geolocation service failed.';
} else {
var content = 'Error: Your browser doesn\'t support geolocation.';
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
还有我的.php
<?php
require_once("../admin/php/checklogin.php");
$host_name = "my_host";
$database = "my_db";
$bdduser_name = "my_name";
$bddpassword = "my_bddpass";
mysql_connect($host_name, $bdduser_name, $bddpassword) or die ("connection impossible.");
mysql_select_db($database) or die ("connection bdd impossible.");
//tu fais la connexion à la base puis:
$sql="SELECT `id`, `nom`, `adresse`, `esc`, `cp`, `ville`
FROM `missions` WHERE 1"; //remplaces par tes noms
ini_set('mysql.trace_mode', true);
mysql_set_charset('utf8');
$result = mysql_query($sql);
?>
首先,您可以使用 AJAX request (would recommend using jQuery for this) 到服务器上的 php 文件来检索地址信息。
$.ajax({ //create an ajax request to a page that prints the address.
url: "getAddress.php", //url for the address page
success: function(response){
var address = response; //create a variable and give it the value of the response data
codeAddress(address); //decode the address using codeAddress into Lat and Lon
}
});
在我看来,第二种但不太干净的方法是直接将地址变量从 php 回显到 javascript。
codeAddress(<?php echo $address; ?>);
希望我为您指明了正确的方向。如果您对 PHP 和 Javascript.
有一个一般的理解,您应该能够实现您的目标
我的数据库中有一些人的地址(在 table 命名任务中,该字段是地址)
我还有一个功能地理编码器,可以将此地址更改为经度和纬度,以便在我的地图上绘制标记。
唯一我不明白该怎么做的是从我的数据库中获取我的地址并将其用于我的地图
这是我的代码:(编辑)
我想从我的数据库中获取地址并将其放入我的地理编码器函数的 'adress' 字段中。但这可能吗?如果是怎么办?
感谢大家的帮助。 (希望我清楚^^')
编辑:
我的.js
<script type="text/javascript">
// When the window has finished loading create our google map below
var map;
var geocoder;
function initialize() {
geocoder = new google.maps.Geocoder();
var mapOptions = {
zoom: 14
};
map = new google.maps.Map(document.getElementById('map-geo'), mapOptions);
if(navigator.geolocation) {
var circle = new google.maps.Circle({
center: new google.maps.LatLng(52.376018, 4.901962),
radius: 500,
map: map
});
navigator.geolocation.getCurrentPosition(function(position) {
var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
accuracy = parseFloat(position.coords.accuracy);
map.setZoom(14);
circle.setCenter(pos);
circle.setRadius(accuracy);
map.fitBounds(circle.getBounds());
var infowindow = new google.maps.InfoWindow({
map: map,
position: pos,
content: 'Voici votre géolocalisation'
});
//map.setCenter(pos);
}, function() {
circle.setMap(null);
handleNoGeolocation(true);
});
} else {
// Browser doesn't support Geolocation
handleNoGeolocation(false);
}
$.ajax({ //create an ajax request to a page that prints the address.
url: "addMarkers.php", //url for the address page
success: function(result){
var adresse = result; //create a variable and give it the value of the response data
codeAddress(adresse); //decode the address using codeAddress into Lat and Lon
}
});
codeAddress(<?php echo $adresse; ?>);
//codeAddress();
}
function codeAddress(adresse) {
geocoder.geocode( { 'address': "61 rue de la libération, 44230, saint sebastien sur loire"}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
function handleNoGeolocation(errorFlag) {
if (errorFlag) {
var content = 'Error: The Geolocation service failed.';
} else {
var content = 'Error: Your browser doesn\'t support geolocation.';
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
还有我的.php
<?php
require_once("../admin/php/checklogin.php");
$host_name = "my_host";
$database = "my_db";
$bdduser_name = "my_name";
$bddpassword = "my_bddpass";
mysql_connect($host_name, $bdduser_name, $bddpassword) or die ("connection impossible.");
mysql_select_db($database) or die ("connection bdd impossible.");
//tu fais la connexion à la base puis:
$sql="SELECT `id`, `nom`, `adresse`, `esc`, `cp`, `ville`
FROM `missions` WHERE 1"; //remplaces par tes noms
ini_set('mysql.trace_mode', true);
mysql_set_charset('utf8');
$result = mysql_query($sql);
?>
首先,您可以使用 AJAX request (would recommend using jQuery for this) 到服务器上的 php 文件来检索地址信息。
$.ajax({ //create an ajax request to a page that prints the address.
url: "getAddress.php", //url for the address page
success: function(response){
var address = response; //create a variable and give it the value of the response data
codeAddress(address); //decode the address using codeAddress into Lat and Lon
}
});
在我看来,第二种但不太干净的方法是直接将地址变量从 php 回显到 javascript。
codeAddress(<?php echo $address; ?>);
希望我为您指明了正确的方向。如果您对 PHP 和 Javascript.
有一个一般的理解,您应该能够实现您的目标