在地图上绘制路线,带有多个标记,CodeIgniter

Drawing route on map, with multiple markers, CodeIgniter

我进行了搜索,但不幸的是没有找到相关的解决方案。我想通过使用 codeigniter google 地图库 来完成。我正在关注这个 link 但它只是显示起点和终点,并没有像

那样创建多个引脚

这是用多段线制作多个图钉,但我想要这样的路由:

具有多个引脚,如折线地图图片所示。.是否可以通过多个引脚获得多个方向??

我试过了,但我的把戏没用。我通过使用 while 循环进行了尝试,并在结束点之前增加了变量以使我的方向像

  1. 第一个经纬度:起点
  2. 第二纬度,经度:终点
  3. 第 2 纬度、经度:起点
  4. 第 3 纬度、经度:终点
  5. 第 3 纬度、经度:起点
  6. 第 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。 本服务Google地图API关键绘制路线 从 here 获取 google 密钥登录到 google 帐户并为您的项目生成密钥

Working Demo

HTML

<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);