2010-08-23 153 views
0
http://maps.google.com/maps/api/staticmap?center=Berkeley,CA&zoom=14&size=280x280&sensor=false&markers=icon:http://chart.apis.google.com/chart%3Fchst%3Dd_map_spin%26chld%3D1%257C0%257Cfff%257C11%257C_%257CHere|Berkeley,CA 

如果我們打開上面的圖像效果很好。谷歌靜態地圖圖像幫助

http://maps.google.com/maps/api/staticmap?center=cabindeck,Startrack&zoom=14&size=280x280&sensor=false&markers=icon:http://chart.apis.google.com/chart%3Fchst%3Dd_map_spin%26chld%3D1%257C0%257Cfff%257C11%257C_%257CHere|cabindeck,Startrack 

問題是我如何給出一個錯誤回聲,如果它失敗了?有任何想法嗎 ? 回覆111期運用PHP

function get($address){ 
    echo '<img src="http://maps.google.com/maps/api/staticmap?center=' . $address . ' 
    &zoom=14&size=280x280&sensor=false&markers=icon:http://chart.apis.google.com/chart%3Fchst%3Dd_map_spin%26chld%3D1%257C0%257Cfff%257C11%257C_%257CHere|' . $address . '" alt="" />'; 
} 
+0

或者可能有一個map如果參數未知,我們可以使用它來顯示none參數嗎? – 2010-08-23 00:25:30

回答

0

如果你必須這樣做沒有Ajax,下面可能會工作。但這取決於curl從谷歌回來的迴應。

function getMap($address){ 

    //Target Url 
    $target = "http://maps.google.com/maps/api/staticmap?center={$address}&zoom=14&size=280x280&sensor=false&markers=icon:http://chart.apis.google.com/chart%3Fchst%3Dd_map_spin%26chld%3D1%257C0%257Cfff%257C11%257C_%257CHere|{$address}"; 

    //run curl 
    $curl_instance = curl_init($target); 
    curl_setopt($curl_instance, CURLOPT_RETURNTRANSFER, true); 
    curl_exec($curl_instance); 

    //if the img did not come back 
    if(curl_errno($curl_instance)) 
     return '<p class="error">Map Error: ' . curl_error($ch) . '</p>'; 
    else 
     return "<img src=\"{$target}\" alt=\"Map\" />"; 

    //kill curl 
    curl_close($curl_instance); 
} 

echo getMap($address); 
+0

仍然無法正常工作:)不錯的嘗試 – 2010-08-23 01:25:30

+0

無論如何$ check_size = curl_getinfo($ target,CURLINFO_SIZE_DOWNLOAD); if($ check_size == 1350){work :) – 2010-08-23 03:36:44