无法使用php获取谷歌地图上的地址

时间:2015-12-08 11:30:54

标签: php google-maps

我正在网站上使用谷歌地图。我必须在谷歌地图上显示不同的地址,因为我写了一些代码。这个代码在localhost和谷歌地图上正常工作正在成功检索,但在实时服务器上我什么都没有。这是我的map.php文件的代码

<!DOCTYPE html>
<html lang="en">
<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Live Demo of Google Maps Geocoding Example with PHP</title>

    <style>
    body{
        font-family:arial;
        font-size:.8em;
    }
    #gmap_canvas{
        width:100%;
        height:30em;
    }

    #map-label,
    #address-examples{
        margin:1em 0;
    }
    </style>
</head>
<body>
 <?php

function geocode($address1)
{
    $address1 = urlencode($address1);
    //echo $address1;
    $url = "http://maps.google.com/maps/api/geocode/json?address={$address1}";
    //echo $url;
    $resp_json = file_get_contents($url);
     echo $resp_json;
    $resp = json_decode($resp_json, true); 
    //print_r($resp);
    if($resp['status']=='OK')
    {
        $lati = $resp['results'][0]['geometry']['location']['lat'];
        //echo $lati.'<br />';
        $longi = $resp['results'][0]['geometry']['location']['lng'];

        $formatted_address = $resp['results'][0]['formatted_address'];

        if($lati && $longi && $formatted_address)
        {
            $data_arr = array();            
            array_push(
                $data_arr, 
                    $lati, 
                    $longi, 
                    $formatted_address
                );

            return $data_arr;             
        }
        else
        {
            return false;
        }        
    }
    else
    {
        return false;
    }
}
?>
<?php

    $post_adres = "Sec 66";
    $post_city = "Mohali";
    $address1 = $post_adres.','.$post_city;

    $address1 = str_replace(" ", "+", $address1);
    $data_arr = geocode($address1);
    print_r($data_arr);
    if($data_arr)
    {        
        $latitude = $data_arr[0];
        //echo $latitude.'<br />';
        $longitude = $data_arr[1];
        //echo $longitude.'<br />';
        $formatted_address = $data_arr[2]; 
        //echo $formatted_address.'<br />';     
    ?>

    <div id="gmap_canvas">Loading map...</div>
    <div id='map-label'>Map shows approximate location.</div>

    <script type="text/javascript" src="http://maps.google.com/maps/api/js"></script>    
    <script type="text/javascript">
        function init_map() {
            var myOptions = {
                zoom: 14,
                center: new google.maps.LatLng(<?php echo $latitude; ?>, <?php echo $longitude; ?>),
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            map = new google.maps.Map(document.getElementById("gmap_canvas"), myOptions);
            marker = new google.maps.Marker({
                map: map,
                position: new google.maps.LatLng(<?php echo $latitude; ?>, <?php echo $longitude; ?>)
            });
            infowindow = new google.maps.InfoWindow({
                content: "<?php echo $formatted_address; ?>"
            });
            google.maps.event.addListener(marker, "click", function () {
                infowindow.open(map, marker);
            });
            infowindow.open(map, marker);
        }
        google.maps.event.addDomListener(window, 'load', init_map);
    </script>

    <?php
    }
    else
    {
        echo "No map found.";
    }

?>

</body>
</html>

和实时网址是

http://www.chandigarhpropertyonline.com/map.php

它让我处于其他状态,并说没有找到地图。它在localhost上工作得很好。有人有任何想法吗?

1 个答案:

答案 0 :(得分:0)

这似乎有效,希望它会有用:

<?php
    function curlrequest( $url=NULL, $options=NULL ){

        /* Download cacert.pem and change path here to suit */
        $cacert='c:/wwwroot/cacert.pem';    


        $curl=curl_init();
        if( parse_url( $url,PHP_URL_SCHEME )=='https' ){
            curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, FALSE );
            curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 2 );
            curl_setopt( $curl, CURLOPT_CAINFO, realpath( $cacert ) );
        }
        curl_setopt( $curl, CURLOPT_URL,trim( $url ) );
        curl_setopt( $curl, CURLOPT_AUTOREFERER, TRUE );
        curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE );
        curl_setopt( $curl, CURLOPT_FRESH_CONNECT, TRUE );
        curl_setopt( $curl, CURLOPT_HEADER, FALSE );
        curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE );
        curl_setopt( $curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0' );
        curl_setopt( $curl, CURLOPT_ENCODING, '' );

        if( isset( $options ) && is_array( $options ) ){
            foreach( $options as $param => $value ) curl_setopt( $curl, $param, $value );
        }
        $res=array(
            'response'  =>  curl_exec( $curl ),
            'info'      =>  curl_getinfo( $curl ),
            'errors'    =>  curl_error( $curl )
        );
        curl_close( $curl );
        return (object)$res;
    }


    function geocode( $address, $curl=true ){
        $url="http://maps.google.com/maps/api/geocode/json?address=".urlencode( $address );

        if( $curl ){
            $data = curlrequest( $url );
            $resp = json_decode( $data->response,true );        
        } else {
            $resp = json_decode( file_get_contents( $url ), true );
        }

        if( $resp['status']=='OK' ){
            $lat = $resp['results'][0]['geometry']['location']['lat'];
            $long = $resp['results'][0]['geometry']['location']['lng'];
            $address = $resp['results'][0]['formatted_address'];

            return ( $lat && $long && $address ) ? (object)array('lat'=>$lat, 'lng'=>$long, 'address'=>$address) : false; 
        }
        return false;
    }



    $post_address = "Sec 66";
    $post_city = "Mohali";
    $findaddress = str_replace( " ", "+", $post_address . ', ' . $post_city );
    $data = geocode( $findaddress );


    $lat=$data ? $data->lat : false;
    $lng=$data ? $data->lng : false;
    $address=$data ? $data->address : false;


?>
<!doctype html>
<html lang="en">
    <head>
        <meta charset='utf-8'>
        <meta http-equiv='X-UA-Compatible' content='IE=edge'>
        <meta name='viewport' content='width=device-width, initial-scale=1'>
        <title>Live Demo of Google Maps Geocoding Example with PHP</title>
        <style>
            body{
                font-family:arial;
                font-size:.8em;
            }
            #gmap_canvas{
                width:100%;
                height:30em;
                height:600px;
            }

            #map-label,
            #address-examples{
                margin:1em 0;
            }
        </style>
        <script type="text/javascript" src="//maps.google.com/maps/api/js"></script>
        <script type="text/javascript">
        <?php
            if( $data && $lat && $lng ){

                echo "
                var map;
                var lat={$lat};
                var lng={$lng};
                var address='{$address}';

                function init_map() {
                        var options = {
                            zoom:14,
                            center:new google.maps.LatLng( lat, lng ),
                            mapTypeId:google.maps.MapTypeId.ROADMAP
                        };
                        map = new google.maps.Map( document.getElementById('gmap_canvas'), options);

                        marker = new google.maps.Marker({
                            map:map,
                            position:options.center
                        });
                        infowindow = new google.maps.InfoWindow({
                            content:address
                        });
                        google.maps.event.addListener(marker, 'click', function () {
                            infowindow.open(map, marker);
                        });
                        infowindow.open(map, marker);
                    }
                    google.maps.event.addDomListener( window, 'load', init_map );";         

            } else {
                echo "
                window.onload=function(){
                    document.getElementById('gmap_canvas').innerHTML='There was a problem decoding the address';
                };";
            }
        ?>
        </script>
    </head>
    <body>
        <div id='gmap_canvas'>Loading map...</div>
        <div id='map-label'>Map shows approximate location.</div>
    </body>
</html>