Google Map Places javascript text search : can't get radius to work -
i need google places results (javascript, not places api) display list of places based on text search.
everything working fine excepts radius parameter can see on jsfiddle.
map = new google.maps.map(document.getelementbyid('map'), { center: pyrmont, zoom: 15 }); var request = { location: pyrmont, radius: '5000', query: cat_search }; service = new google.maps.places.placesservice(map); service.textsearch(request, callback);
if click on "restaurant" ok, if click on "peintre" results on 5000 radius , if click on "fastfood et snack" results going crazy, results 70000 m radius returned.
note: removed google map key script url on jsfiddle. results same key. key not revelant google map javascript api ?
see the documentation, location , radius used bias results, not restrict them. if don't want display results outside of radius, can remove them output displayed.
the documentation states:
bounds type: latlngbounds bounds used bias results when searching places (optional). both location , radius ignored if bounds set. results not restricted inside these bounds; but, results inside rank higher.
location type: latlng|latlngliteral center of area used bias results when searching places.
radius type: number radius of area used bias results when searching places, in meters.
code snippet removes results outside of specified radius
var map; var service; function initmap(param_lat, param_lng, cat_search) { var pyrmont = new google.maps.latlng(param_lat, param_lng); map = new google.maps.map(document.getelementbyid('map'), { center: pyrmont, zoom: 15 }); var request = { location: pyrmont, radius: 5000, query: cat_search }; service = new google.maps.places.placesservice(map); service.textsearch(request, callback); function callback(results, status) { if (status == google.maps.places.placesservicestatus.ok) { var max_iteration = (results.length < 5) ? results.length : 5; var resultcount = 0; (var = 0; < results.length; i++) { if (google.maps.geometry.spherical.computedistancebetween(results[i].geometry.location, pyrmont) < request.radius) { console.log(results[i]); var request2 = { placeid: results[i].place_id }; service = new google.maps.places.placesservice(map); service.getdetails(request2, callback2); resultcount++; } } if (resultcount == 0) { $("#res").prepend("no results inside search area"); } } else { $("#res").prepend("no results, status " + status); } } } function callback2(place, status) { if (status == google.maps.places.placesservicestatus.ok) { var photos = place.photos; if (!photos) { temp_var = "<img width='30' src='" + place.icon + "' />"; } else { temp_var = "<img src='" + photos[0].geturl({ 'maxwidth': 30, 'maxheight': 30 }) + "' />"; } temp_var += place.name; temp_var += " :" + (google.maps.geometry.spherical.computedistancebetween(new google.maps.latlng(50.4167, 4.4333), place.geometry.location) / 1000).tofixed(1) + " km [" + place.geometry.location.lat() + ", " + place.geometry.location.lng() + "]<br />"; $("#res").prepend(temp_var); } } function torad(value) { // converts numeric degrees radians return value * math.pi / 180; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maps.googleapis.com/maps/api/js?libraries=places,geometry"></script> <a onclick="initmap( 50.4167, 4.4333,'fastfood et snack');">fastfood et snack</a> <br /> <a onclick="initmap( 50.4167, 4.4333,'restaurant');">restaurant</a> <br /> <a onclick="initmap( 50.4167, 4.4333,'peintre');">peintre</a> <div id="map" style="display:none;"></div> <div id="res"></div>
Comments
Post a Comment