简体   繁体   中英

Google Maps JS API equivalent of URL

I just realized that I cannot call directly on a URL from my AngularJS application due to CORS. Therefore, I expect I will have to use the Javascript API.

The following link provides me with the data I want:

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=' + myPosition.lat + ',' + myPosition.lng + '&rankby=distance&key=' + key  + '&sensor=false&type=clothing_store

However, I cannot figure how to achieve the same using the Javascript API. I found this in the documentation, but I do not need a map - just the names and coordinates of the nearby stores.

How can I get a hold of the same data using the Javascript API?

If you've already got a Google Maps for JS API Map Key, you can do a Places query from the server side. Note, however, that you're limited to 1000 queries per day .

You can probably do this directly in your client and avoid CORS issues, but here's how I've done it previously using PHP:

<?php
    function ReturnEmpty()
    {
        // Something's wrong, return an empty set.
        echo '{"d":[]}';
    }

    $q = (isset($_GET["query"])) ? $_GET["query"] : 'NULL';


    if( $q == "" )
    {
        ReturnEmpty();
    }
    else
    {
        // I'm showing the "textsearch" option, but there is also a "nearbysearch" option..
        $url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query="
                . urlencode($q)
                . "&key=YOUR_GOOGLE_MAPS_KEY_HERE";

        $json = file_get_contents($url);

        $data = json_decode($json, true);

        echo $data;
    }
?>

You can use the nearbySearch(request, callback) method on google.maps.places.PlacesService(map) .

In your case, the request should have a location (that contains the lat and lng) and a radius .

There are code samples here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM