Cors Api Google Maps

1

I'm developing a system made in Silex / PHP and the frontEnd of it is being done in AngularJs, I have a method in it that I'm wanting it to do a search in the Google Maps API to return me a certain route, but every time I returns the following error:

Therequestheader:

If I enable cors with the Chrome extension it works perfectly:

Ihaveanindex.phpwhereallrequestsgothroughit,soinsertthefollowing:

I've tried everything but nothing works, has anyone had it?

    
asked by anonymous 07.07.2015 / 18:59

1 answer

0

The Google API does not support either CORS or JSONP, so you'll have to do a proxy with php. Something like this:

$http.get('goog.php').then(
    function(res){
        console.info(res);
        if(res.status == 200){

        }
    }, function(error){
        //mensagem de erro
        console.info(error);
    }
);

PHP:

<?php
$url = 'https://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC|Seattle&destinations=San+Francisco|Victoria+BC&mode=bicycling&language=fr-FR&key=SUA_KEY';
$returned_content = get_data($url);

header('Content-Type: application/json');
echo $returned_content;

function get_data($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;
}
?>

If you do not have curl , use some other way.

    
08.07.2015 / 13:39