创建谷歌地图以绘制到多个位置的行车路线

时间:2014-06-22 18:33:00

标签: google-maps

我想创建一个谷歌地图,仅显示使用ROAD DIRECTIONS到多个地点的行车路线。 我使用php插入目标点的纬度和经度。 例如使用

  `var markers = [

    <?php 
            // loop through the rows of data
            if( have_rows('tour_site', $ID) ):

             $info_places = array();
              $info_all_places = array();                 
                while( have_rows('tour_site', $ID) ) : the_row();
                 //display a sub field value
                    $name = get_sub_field('name');
                    $long = get_sub_field('long');
                    $lat = get_sub_field('lat');
                    $info_places = array("name" => $name, "long"=>$long, "lat"=>$lat);
                    $info_all_places = array($info_places);
                    foreach ($info_all_places as $info) {
                    ?>
                    { 
                         "title": <?php echo "'" . $info['name']  . "'"; ?>,
                         "lat": <?php echo "'" . $info['lat'] . "'"; ?>,
                         "lng": <?php echo "'" . $info['long'] . "'"; ?>,
                    }, 
                    <?php 
                    }
            endwhile;
            else :   
            endif;
        ?>


        {
           "title": 'Always the starting point',
           "lat": 'xxx',
           "lng": 'xxx
         }


 ];`

如何在谷歌地图上绘制这些点,只显示一条驾驶路线,没有显示飞行的鱼?

这里的问题不是如何引入php值,而是如何在途中绘制具有多个位置的路线。我该怎么做呢?

1 个答案:

答案 0 :(得分:0)

来自Rafael Sanches blog

<?php
class GoogleGeo {
    public static function buildStaticMap($center, $markers=array(), $width=400, $height=400, $zoom=12, $directions=null) {
        $strMarkers = "";
        foreach($markers as $marker) {
            if (!empty($strMarkers)) $strMarkers .= '|';
            $strMarkers .= urlencode($marker);
        }
        if ($width > 640) $width = 640;
        if (!empty($center)) {
            $center = "&center=".$center;
        }
        if (!empty($strMarkers)) {
            $strMarkers = "&markers=".$strMarkers;
        }
        if ($zoom > 0) {
            $zoom = "&zoom=$zoom";
        }

        $steps = "";
        if (!empty($directions)) {
            foreach($directions['Directions']['Routes'][0]['Steps'] as $step) {
                $lat = $step['Point']['coordinates'][2];
                $lon = $step['Point']['coordinates'][0];
                if (!empty($steps)) $steps .= "|";
                $steps .= $lat.",".$lon;
            }
            if (!empty($steps)) {
                $steps .= "|".$directions['Directions']['Routes'][0]['End']['coordinates'][3].",".$directions['Directions']['Routes'][0]['End']['coordinates'][0];
                $steps = "&path=rgb:0x0000ff,weight:5|".$steps;
            }
        }

        $staticMap = "http://maps.google.com/staticmap?maptype=mobile&size=".$width."x$height&maptype=roadmap&key=".GOOGLE_MAPS_KEY."&sensor=false$strMarkers$center$zoom$steps";
        return $staticMap;
    }

    public static function retrieveDirections ($from, $to) {
        $params = array('key' => GOOGLE_MAPS_KEY, 'output' => 'json', 'q' => "from: $from to: $to");
        $url = "http://maps.google.com/maps/nav";
        $result = HttpHelper::doGET($url, $params);
        $result = json_decode($result, true);
        return $result;
    }
}
?>

sample:

<?php
...
    /* FROM and TO coordinates */
    $markers = array("37.262568,-121.962232,redr", "37.229898,-121.971853,blueg");
    /* Get the driving directions from google api */
    $directions = GoogleGeo::retrieveDirections("485 Alberto Way, Suite 210. Los Gatos, CA 95032", "14109 Winchester Bl, Los Gatos, CA");
    /* Create the map image url with the directions coordinates */
    $staticMap = GoogleGeo::buildStaticMap(null, $markers, 640, 240, null, $directions);
....
?>