2010-08-23 119 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