doushenyi9104 2016-10-19 19:00
浏览 44
已采纳

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

I did my searching but unfortunately didnt find relevant solution. I want to do it by using codeigniter google map library. i am following this link But it is just showing starting and ending point, it's not creating multiple pins like

enter image description here

This is making multiple pins with polyline but i want routing like:

enter image description here

with multiple pins as shown in polyline map picture.. Is it posible to get multiple directions with multiple pins ??

I tried it but my trick couldn't work. i tried it by using while loop and i incremented the variable before ending point to make my direction like

  1. 1st lat, long : starting point
  2. 2nd lat, long : ending point
  3. 2nd lat, long : starting point
  4. 3rd lat, long : ending point
  5. 3rd lat, long : starting point
  6. 4th lat, long : ending point

But it's only making 1st and last ending point for start and end direction

Here is my controller function

##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条回答 默认 最新

  • doq70020 2016-10-20 14:13
    关注

    It seems like you need to use google's DirectionsService. This service Google map API key to draw routes Get google key from here login to google account and generate key for your project

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含