谷歌静态地图图片帮助

时间:2010-08-23 00:23:24

标签: php google-maps

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

问题是,如果失败,我会如何给出错误回音?有任何想法吗 ? 顺便说一句,我正在使用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="" />';
}

1 个答案:

答案 0 :(得分: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);