最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - passing bounds to google places API - Stack Overflow

programmeradmin1浏览0评论

I'm just starting to play with the Google Places API (.html) but I can't seem to get the syntax right for passing in bounds rather than a latLng object and radius.

In the API docs it says:

This method takes a request with the following fields: Either of: bounds, which must be a google.maps.LatLngBounds object defining the rectangle in which to search; or a location and radius; the former takes a google.maps.LatLng object, and the radius takes a simple integer, representing the circle's radius in meters.

My code looks like this:

var bnds      = map.getBounds();
console.log(bnds); // this returns a valid bounds object!
var req       = {
                 bounds: bnds
              };
var service = new google.maps.places.PlacesService(map);
service.search(req, callback);

When I run the page, I get an error Uncaught Error: Property bounds not specified from the Google service. If I change the req object to

var req    = {
               location: latlngobject,
               radius: '500'
             }

it works properly. Any idea on the proper syntax for passing in the bounds object rather than the lat/lng/radius?

I'm just starting to play with the Google Places API (http://code.google./apis/maps/documentation/javascript/places.html) but I can't seem to get the syntax right for passing in bounds rather than a latLng object and radius.

In the API docs it says:

This method takes a request with the following fields: Either of: bounds, which must be a google.maps.LatLngBounds object defining the rectangle in which to search; or a location and radius; the former takes a google.maps.LatLng object, and the radius takes a simple integer, representing the circle's radius in meters.

My code looks like this:

var bnds      = map.getBounds();
console.log(bnds); // this returns a valid bounds object!
var req       = {
                 bounds: bnds
              };
var service = new google.maps.places.PlacesService(map);
service.search(req, callback);

When I run the page, I get an error Uncaught Error: Property bounds not specified from the Google service. If I change the req object to

var req    = {
               location: latlngobject,
               radius: '500'
             }

it works properly. Any idea on the proper syntax for passing in the bounds object rather than the lat/lng/radius?

Share Improve this question asked Sep 12, 2011 at 19:39 juliojulio 6,76815 gold badges65 silver badges82 bronze badges 2
  • Also should add that I get the same error when submitting var req. . .location: bnds – julio Commented Sep 14, 2011 at 20:18
  • I get "Property location is not specified." when passing in bounds only. Did you ever resolve this? Seems to be a bug in the API. – Gady Commented Jul 20, 2012 at 17:52
Add a ment  | 

5 Answers 5

Reset to default 2

2020 Update: Maps v3 has nearbySearch and textSearch, which both accept bounds as a location.

The docs do shows that bounds is an option:

https://developers.google./maps/documentation/javascript/places#place_search_requests

Either of: bounds, which must be a google.maps.LatLngBounds object defining the rectangular search > area; or a location and radius; the form .....

I had this issue, so I took out the other parameters as the full error shows:

Uncaught Error: Property bounds is invalid (perhaps because of other properties).

So take out the rankBy or keywords or types parameters, until it works - then add them back

These parameters work for me: var arg = { bounds:map.getBounds(), types:['bar','airport'] };

Make the types as full as you want it.

Bounds works better for me in the radar search than in the nearbySearch

I am pretty sure the Google Places API does not have a bounds option/parameter. I don't see it mentioned anywhere here at least: https://developers.google./places/documentation/

radius only it seems.

The following example shows you how to obtain the bounds from a Geocoder() result and limit your PlacesService() results to it and assign the appropriate default Places Markers.

var geocoder = new google.maps.Geocoder(),
    map = new google.maps.Map(document.getElementById('map'), {
        zoom: 10,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }),
    rect = new google.maps.Rectangle({
        strokeColor: '#FF0000',
        strokeOpacity: 0.3,
        strokeWeight: 2,
        fillColor: '#FF0000',
        fillOpacity: 0.33,
        map: map
    }),
    markers_amount = 10,
    markers = [];

// GEOCODE
geocoder.geocode({
    'address': 'Vienna'
}, function (results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        var geometry = results[0].geometry,
            places = new google.maps.places.PlacesService(map);

        map.setCenter( bounds.getCenter() );
        // SETS THE BOUNDS HERE
        rect.setBounds( bounds );

        places.search({
            bounds: bounds,
            // https://developers.google./places/documentation/supported_types
            types: [
                // Until there's enough meta data for Google, 
                // they label everything as an 'establishment'.
                'establishment'
            ]
        }, function (results, status) {
            if (status == google.maps.places.PlacesServiceStatus.OK) {
                console.log( results );
                for (var i = 0; i < markers_amount; i++) {
                    markers.push(new google.maps.Marker({
                        position: results[i].geometry.location,
                        map: map,
                        icon: image = {
                            url: results[i].icon,
                            size: new google.maps.Size(71, 71),
                            origin: new google.maps.Point(0, 0),
                            anchor: new google.maps.Point(17, 34),
                            scaledSize: new google.maps.Size(25, 25)
                        }
                    }));
                }
            }
        });
        //console.log(markers);
    } else {
        return $('#map').html('could not locate you');
    }
});

maybe this code snippet will help you

var bounds = map.getBounds();
var southWest = bounds.getSouthWest();
var northEast = bounds.getNorthEast();
var lngSpan = northEast.lng() - southWest.lng();
var latSpan = northEast.lat() - southWest.lat();

发布评论

评论列表(0)

  1. 暂无评论