在地图上绘制路线,具有多个标记,CodeIgniter
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在地图上绘制路线,具有多个标记,CodeIgniter相关的知识,希望对你有一定的参考价值。
我做了我的搜索,但遗憾的是没有找到相关的解决方案。我想通过使用codeigniter谷歌地图库来做到这一点。我跟随这个link但它只是显示起点和终点,它不是创建多个引脚像
这是使用折线制作多个引脚,但我希望路由如下:
有多个引脚,如折线图图片所示..是否有可能获得多个引脚的多个方向?
我尝试过,但我的伎俩无法奏效。我通过使用while循环尝试它,并在结束点之前增加变量以使我的方向像
- 1st lat,long:起点
- 第二个lat,long:结束点
- 第二个lat,long:起点
- 第3个lat,long:结束点
- 第3个lat,long:起点
- 第4个纬度,长度:结束点
但它只是开始和结束方向的第一个和最后一个结束点
这是我的控制器功能
##Load library
$this->load->library('googlemaps');
## Getting data from db
$final_data['final_data'] = $this->Main_manager->getAllEmailLogsById($id);
$email = $final_data['final_data'][0]['email'];
$date = $final_data['final_data'][0]['date'];
$file = 'assets/email_logs/'.$email.'-'.str_replace(' ','-',$date).'.txt';
## Getting lat long data from txt file
$logData = file_get_contents($file);
$logData = json_decode($logData, true);
$marker = array();
$logs = count($logData['logs']);
$config['center'] = $final_data['final_data'][0]['lat'].','. $final_data['final_data'][0]['long'];
$config['zoom'] = 'auto';
$i=0;
while($i<$logs-1):
$config['position'] = $logData['logs'][$i]['lat'].','. $logData['logs'][$i]['long'];
$config['infowindow_content'] = $logs['email'];
$config['animation'] = 'DROP';
$config['draggable'] = FALSE;
$config['directions'] = TRUE;
$config['directionsStart'] = $logData['logs'][$i]['lat'].','. $logData['logs'][$i]['long'];
$i++;
$config['directionsEnd'] = $logData['logs'][$i]['lat'].','. $logData['logs'][$i]['long'];
$config['directionsDivID'] = 'directionsDiv';
endwhile;
## initialize the map
$this->googlemaps->initialize($config);
##create map
$final_data['map'] = $this->googlemaps->create_map();
$this->load->view('administrator/header');
$this->load->view('administrator/view_logs_detail', $final_data);
答案
看来你需要使用google's DirectionsService。这个服务谷歌地图API键绘制路线获取谷歌密钥从here登录到谷歌帐户并为您的项目生成密钥
<h1>Google Map direction service</h1>
<div id="map"></div>
CSS
html,
body,
#map {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
JS:
var map;
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var locations = [
['Shahrah-e-Faisal, Karachi, Pakistan', 24.8678, 67.0842, 1],
['Tariq Rd, Karachi, Pakistan', 24.8727, 67.0604, 2],
['Service Lane, Karachi, Pakistan', 24.8161, 67.0212, 3]
];
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(24.8678, 67.0842),
});
directionsDisplay.setMap(map);
var infowindow = new google.maps.InfoWindow();
var marker, i;
var request = {
travelMode: google.maps.TravelMode.DRIVING
};
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
});
google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
if (i == 0) request.origin = marker.getPosition();
else if (i == locations.length - 1) request.destination = marker.getPosition();
else {
if (!request.waypoints) request.waypoints = [];
request.waypoints.push({
location: marker.getPosition(),
stopover: true
});
}
}
directionsService.route(request, function (result, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
}
});
}
google.maps.event.addDomListener(window, "load", initialize);
以上是关于在地图上绘制路线,具有多个标记,CodeIgniter的主要内容,如果未能解决你的问题,请参考以下文章
如何使用javascript在谷歌地图中的多个标记之间绘制路线图
使用数据库中的多个纬度和经度值在 Google 地图上标记路线