我有一些人的地址进入我的数据库(在一个名为mission的表中,该字段是地址)
我还有一个功能地理编码器将此地址更改为long和lat以在地图上绘制标记。
我唯一不明白该怎么做的事情就是从我的数据库中取出我的地址并将其用于我的地图
这是我的代码:(编辑)
我想从我的数据库中取出地址并将其放在字段“地址”中。在我的地理编码器功能。但这可能吗?如果有,怎么样?
感谢帮助人们。 (希望我明白^^')
编辑:
我的.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);
?>
答案 0 :(得分:1)
首先,您可以使用AJAX请求(建议将jQuery用于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有一个平均的了解,那么您应该能够实现目标。