Javascript Php中google map的Php数组转换为jquery数组问题
我正在研究php,我正在进行google map集成,我有数据库数组值,我想传递javascript数组如何做到这一点,在静态地址数组中,google map需要传递php数组到javascript变量,请帮助。 我花了很长时间进行调试,但没有得到任何线索。请帮我解决这个问题 这是我的代码:Javascript Php中google map的Php数组转换为jquery数组问题,javascript,php,arrays,google-maps,Javascript,Php,Arrays,Google Maps,我正在研究php,我正在进行google map集成,我有数据库数组值,我想传递javascript数组如何做到这一点,在静态地址数组中,google map需要传递php数组到javascript变量,请帮助。 我花了很长时间进行调试,但没有得到任何线索。请帮我解决这个问题 这是我的代码: <?php include "db_connection.php"; $locations=array(); $query = $conn->
<?php
include "db_connection.php";
$locations=array();
$query = $conn->query('SELECT `address` FROM `tbl_master`');
while ($row = $query->fetch_assoc()) {
$locations[] = $row;
}
$locations = json_encode($locations);
?>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Geocoding service</title>
<style>
html, body, #map {
height: 100%;
margin: 0px;
padding: 0px
}
</style>
<body>
<div id="map"></div>
</body>
</html>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?key=AIzaSyBAM5Cs2VsrOBs8Idqy0t0o6vw4hEU0Lys">
</script>
<script type="text/javascript">
var delay = 100;
var infowindow = new google.maps.InfoWindow();
var latlng = new google.maps.LatLng(21.0000, 78.0000);
var mapOptions = {
zoom: 5,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var geocoder = new google.maps.Geocoder();
var map = new google.maps.Map(document.getElementById("map"), mapOptions);
var bounds = new google.maps.LatLngBounds();
function geocodeAddress(address, next) {
geocoder.geocode({address:address}, function (results,status)
{
if (status == google.maps.GeocoderStatus.OK) {
var p = results[0].geometry.location;
var lat=p.lat();
var lng=p.lng();
createMarker(address,lat,lng);
}
else {
if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
nextAddress--;
delay++;
} else {
}
}
next();
}
);
}
function createMarker(add,lat,lng) {
var contentString = add;
var marker = new google.maps.Marker({
position: new google.maps.LatLng(lat,lng),
map: map,
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(contentString);
infowindow.open(map,marker);
});
bounds.extend(marker.position);
}
//------here i need to pass database dynamic array value-------//
// var locations = <?php print_r($locations); ?>;
var locations = [
'New Delhi, India',
'Mumbai, India',
'Bangaluru, Karnataka, India',
'Hyderabad, Ahemdabad, India'
];
var nextAddress = 0;
function theNext() {
if (nextAddress < locations.length) {
setTimeout('geocodeAddress("'+locations[nextAddress]+'",theNext)', delay);
nextAddress++;
} else {
map.fitBounds(bounds);
}
}
theNext();
</script>
地理编码服务
html,body,#map{
身高:100%;
边际:0px;
填充:0px
}
无功延迟=100;
var infowindow=new google.maps.infowindow();
var latlng=新的google.maps.latlng(21.0000,78.0000);
变量映射选项={
缩放:5,
中心:拉特林,
mapTypeId:google.maps.mapTypeId.ROADMAP
}
var geocoder=new google.maps.geocoder();
var map=new google.maps.map(document.getElementById(“map”)、mapOptions);
var bounds=new google.maps.LatLngBounds();
功能geocodeAddress(地址,下一个){
geocoder.geocode({address:address},函数(结果,状态)
{
if(status==google.maps.GeocoderStatus.OK){
var p=results[0]。geometry.location;
var-lat=p.lat();
var lng=p.lng();
createMarker(地址、纬度、液化天然气);
}
否则{
if(status==google.maps.GeocoderStatus.OVER\u QUERY\u LIMIT){
下一件衣服--;
延迟++;
}否则{
}
}
next();
}
);
}
功能createMarker(添加、横向、横向){
var contentString=add;
var marker=new google.maps.marker({
位置:新google.maps.LatLng(lat,lng),
地图:地图,
});
google.maps.event.addListener(标记'click',函数(){
setContent(contentString);
信息窗口。打开(地图、标记);
});
扩展(标记位置);
}
//------这里我需要传递数据库动态数组值-------//
//var位置=;
变量位置=[
“印度新德里”,
“印度孟买”,
“印度卡纳塔克邦班加鲁”,
“印度阿亨达巴德海得拉巴”
];
var-nextadress=0;
函数theNext(){
如果(下一条裙子<位置.长度){
setTimeout('geocodeAddress('+位置[nextAddress]+',然后是Next'),延迟);
nextAddress++;
}否则{
映射边界(bounds);
}
}
下一步();
因为$locations
已经是一个JSON字符串,所以简单的echo就可以了
//------here i need to pass database dynamic array value-------//
var locations = <?php echo $locations; ?>;
/----这里我需要传递数据库动态数组值-------//
var位置=;
在特定的环境中,你可以用
语法来表达同样的意思。嗨,Kita,我试过你的方法,我警告它显示的位置,比如:[对象对象对象],[对象对象对象],[对象对象对象对象],[对象对象对象对象],[对象对象对象对象],[对象对象对象对象对象对象对象对象]警报
与输出到html不同。开源html,我认为这很好…我正在检查控制台日志,它显示如下:(1014)[{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},@kumar控制台正在向您显示1014个元素的数组,没有实际内容。在某些浏览器(如Chrome)中,单击这些元素可能会显示实际内容。