我想要路线图图像两个地址,我已经尝试了谷歌地图API和MapQuest API,我想要静态图像中的数据,以便我可以使用该图像生成PDF文件,我该怎么做
我试过了 起始地址是6100 Richmond Ave,Houston TX 77057
Google地图
function grab_image($url,$saveto){
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$raw=curl_exec($ch);
curl_close ($ch);
if(file_exists($saveto)){
unlink($saveto);
}
$fp = fopen($saveto,'x');
fwrite($fp, $raw);
fclose($fp);
}
$address = $data['address1'].$data['address2']." ".$data['state']." ".$data['city']." ".$data['zip'];
$address = urlencode($address);
$url = "http://maps.googleapis.com/maps/api/staticmap?size=710x300&sensor=true";
$url .= "&path=color:0x0000ff|weight:10|6100+Richmond+Ave+Houston+TX+77057";
$url .= "|".$address ;
$url .= "&markers=color:red|label:A|6100+Richmond+Ave+Houston+TX+77057";
$url .= "&markers=color:green|label:B|".$address;
grab_image($url, $upload_dir.$plan['ID'].".png");
MapQuest地图
首先废弃经度和纬度的每个来源和目的地地址
function getLatLong($myaddress) {
$myaddress = urlencode($myaddress);
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=".$myaddress."&sensor=false";
//get the content from the api using file_get_contents
$getmap = file_get_contents_curl($url);
//the result is in json format. To decode it use json_decode
$googlemap = json_decode($getmap);
//get the latitute, longitude from the json result by doing a for loop
foreach($googlemap->results as $res){
$address = $res->geometry;
$latlng = $address->location;
//$formattedaddress = $res->formatted_address;
return $latlng;
}
}
http://open.mapquestapi.com/staticmap/v4/getmap?size=600,200&zoom=14&shapeformat=cmp¢er=40.770021,-73.984003&shape=y_zwFjsrbMxWkz@??}DoC??a@}CyBt@ySiN??fDeP&scenter=40.77069,-73.992378&ecenter=40.770935,-73.97644
用于形状检查此链接 http://www.mapquestapi.com/common/encodedecode.html
如何通过公路获取路线,或者我们可以用图像格式说明地图上的行车路线。
答案 0 :(得分:1)
这就是我解决上述问题的方法
function file_get_contents_curl($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$mapquest_key = "YOUR_API_KEY";
$source_address = "6100 Richmond Ave, Houston TX 77057";
$sLat = "29.73189";
$sLan = "-95.48736";
$plan_add = $plan['address1']." ".$plan['address2']." ".$plan['state']." ".$plan['city']." ".$plan['zip'];
# Step 1: Mapquest API url to get session_key and lng and lat
$mapquest_url1 = "http://www.mapquestapi.com/directions/v1/route?key=".$mapquest_key."&outFormat=json";
$mapquest_url1 .= "&from=".urlencode($wocg_address)."&to=".urlencode($plan_add);
$mapquest_data = file_get_contents_curl($mapquest_url1);
$mapquest_data_array = json_decode($mapquest_data);
$session_id = isset($mapquest_data_array->route->sessionId) ? $mapquest_data_array->route->sessionId : "";
$eLat = isset($mapquest_data_array->route->locations[1]->latLng->lat) ? $mapquest_data_array->route->locations[1]->latLng->lat : "";
$eLan = isset($mapquest_data_array->route->locations[1]->latLng->lng) ? $mapquest_data_array->route->locations[1]->latLng->lng : "";
# Step 2: Get images based on start and end location and session id
if(!empty($session_id) && !empty($eLat) && !empty($eLan)) {
$mapquest_url2 = "http://www.mapquestapi.com/staticmap/v3/getmap?key=".$mapquest_key;
$mapquest_url2 .= "&size=710,300&type=map";
$mapquest_url2 .= "&session=".$session_id;
$mapquest_url2 .= "&scenter=".$sLat.",".$sLan;
$mapquest_url2 .= "&ecenter=".$eLat.",".$eLan;
grab_image($mapquest_url2, $upload_dir.$plan['ID'].".png");
} else {
# If failure with mapquest then scrap image from google map
$plan_address = urlencode($plan_add);
$url = "http://maps.googleapis.com/maps/api/staticmap?size=710x300&sensor=true";
$url .= "&path=color:0x0000ff|weight:10|77001+TX+Houston+USA";
$url .= "|".$plan_address;
$url .= "&markers=color:red|label:A|77001+TX+Houston+USA";
$url .= "&markers=color:green|label:B|".$plan_address;
grab_image($url, $upload_dir.$plan['ID'].".png");
}