create google maps to plot driving route to multiple location -
i create google maps showing driving route multiple locations using road directions only. using php insert lat , longitude of destination points. eg using
`var markers = [ <?php // loop through rows of info if( have_rows('tour_site', $id) ): $info_places = array(); $info_all_places = array(); while( have_rows('tour_site', $id) ) : the_row(); //display 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 $info) { ?> { "title": <?php echo "'" . $info['name'] . "'"; ?>, "lat": <?php echo "'" . $info['lat'] . "'"; ?>, "lng": <?php echo "'" . $info['long'] . "'"; ?>, }, <?php } endwhile; else : endif; ?> { "title": 'always starting point', "lat": 'xxx', "lng": 'xxx } ];` how plot these point on google map showing driving route, no crow flies?
the issue here isn't how bring in php values how plot route multiple locations along way. how do this?
from rafael sanches blog
<?php class googlegeo { public static function buildstaticmap($center, $markers=array(), $width=400, $height=400, $zoom=12, $directions=null) { $strmarkers = ""; foreach($markers $marker) { if (!empty($strmarkers)) $strmarkers .= '|'; $strmarkers .= urlencode($marker); } if ($width > 640) $width = 640; if (!empty($center)) { $center = "¢er=".$center; } if (!empty($strmarkers)) { $strmarkers = "&markers=".$strmarkers; } if ($zoom > 0) { $zoom = "&zoom=$zoom"; } $steps = ""; if (!empty($directions)) { foreach($directions['directions']['routes'][0]['steps'] $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"; homecoming $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); homecoming $result; } } ?> sample:
<?php ... /* , coordinates */ $markers = array("37.262568,-121.962232,redr", "37.229898,-121.971853,blueg"); /* driving directions google api */ $directions = googlegeo::retrievedirections("485 alberto way, suite 210. los gatos, ca 95032", "14109 winchester bl, los gatos, ca"); /* create map image url directions coordinates */ $staticmap = googlegeo::buildstaticmap(null, $markers, 640, 240, null, $directions); .... ?> google-maps
No comments:
Post a Comment