你好我在codeigniter中使用javascript在地图上显示选择器,我的代码是
<script type="text/javascript">
<?php
$jsItems = array();
foreach($all as $item) {
$jsItems[] = array(
'title' => $item->studio_name,
'lat' => $item->latitude,
'lng' => $item->longitude,
'description' => $item->address
);}
echo 'var markers = '.json_encode($jsItems).';';?>
我从数据库获取纬度经度值并显示到带拾取器的地图
window.onload = function () {
var mapOptions = {
center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
var infoWindow = new google.maps.InfoWindow();
var lat_lng = new Array();
var latlngbounds = new google.maps.LatLngBounds();
for (i = 0; i < markers.length; i++) {
var data = markers[i]
var myLatlng = new google.maps.LatLng(data.lat, data.lng);
lat_lng.push(myLatlng);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: data.title
});
latlngbounds.extend(marker.position);
(function (marker, data) {
google.maps.event.addListener(marker, "click", function (e) {
infoWindow.setContent(data.description);
infoWindow.open(map, marker);
});
})(marker, data);
}
map.setCenter(latlngbounds.getCenter());
map.fitBounds(latlngbounds);
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDROYAx0XxInepo42-zs3qrnU-40ER70QI"
async defer></script>
这是我的javascript代码,我使用谷歌地图,它在其他页面上工作得更好,但在codeigniter索引页面不起作用
答案 0 :(得分:0)
试试如下代码:
请避免使用脚本,只需直接在控制器中编写代码,如下所示:
$config['center'] = '13.038433, 80.253362';
$this->googlemaps->initialize($config);
$marker = array();
$marker['position'] = '13.038433, 80.253362';
$marker['ondragend'] = 'showCoords(event.latLng.lat(), event.latLng.lng());';
$this->googlemaps->add_marker($marker);
$datas['map'] = $this->googlemaps->create_map();
$this->template['middle'] = $this->load->view ($this->middle = 'pages/your_page',$datas, true);
请收集所有的lat&amp;记录数组中的值,然后将其传递给foreach或forloop。在里面尝试上面的代码。