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

时间:2016-10-19 19:00:28

标签: php codeigniter google-maps google-maps-api-3 google-maps-markers

我做了我的搜索,但遗憾的是没有找到相关的解决方案。我想通过 codeigniter谷歌地图库来实现。我跟着这个 link 但它只显示起点和终点,它不会创建多个引脚,如

enter image description here

这是使用折线制作多个引脚,但我希望路由如下:

enter image description here

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

我试了但是我的伎俩无法奏效。我通过使用while循环尝试它,并在结束点之前增加变量以使我的方向像

  1. 1st lat,long:起点
  2. 第二个lat,long:结束点
  3. 第二个lat,long:起点
  4. 第3个lat,long:结束点
  5. 3rd lat,long:起点
  6. 第4个lat,long:结束点
  7. 但它只是制作开始和结束方向的第一个和最后一个结束点

    这是我的控制器功能

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

1 个答案:

答案 0 :(得分:3)

您似乎需要使用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);
相关问题