Difference between geocoder.setInput() and geocoder.query()? - javascript

MapboxGeocoder provides seInput and query methods both of which can be used to set an initial address in the autocomplete dropdown. But the problem is both sets the input and fetch similar locations and show the results. Is it possible to just set the input value without showing the places suggestions list?

But the problem is both sets the input and fetch similar locations and show the results
Unlike query, which is used to "set & query the input", setInput only sets input.
var geocoder = new MapboxGeocoder({
accessToken: mapboxgl.accessToken,
mapboxgl: mapboxgl,
});
map.addControl(geocoder);
geocoder.setInput("New York");

Related

Change countries option after init with Mapbox Geocoder

I'm using the MapboxGeocoder with the countries option set to a default country, but I would like to update this option if the user changes the country they want to search in.
Current code:
// Add the control to the map.
const geocoder = new MapboxGeocoder({
accessToken: mapboxgl.accessToken,
mapboxgl: mapboxgl,
marker: false,
countries: 'nz'
});
I'm using the chosen plugin from harvest, so I have the on change function working, I need to know if it's possible to change the countries option when they change the country so that results searched for will only return for the current country they have selected
// Country selection changed
$('#country_id').on('change', function(evt, params) {
// Check it's not empty
if (params) {
// code to go in here to change 'countries' option
}
});
Ok, after some digging I found out how you can do this. I know this is an old question, but I'll post it anyway in case someone else ends up here trying to do the same thing! Looking at the API references here:
https://github.com/mapbox/mapbox-gl-geocoder/blob/main/API.md#setcountries
It turns out there is a setCountries option - so just invoke it with:
placesAutocompleteBilling.setCountries(this.value)
with this.value being the country code (ES, IT, FR, GB etc). It seems to do the trick

Google Maps API Geocoder doesn't return location by placeId

I use AutocompleteService.getPlacePredictions method to get autocomplete items for my drop down. Very often that method returns "long" placeIds that look like this (actual placeId):
ChIJx55rzdwQ9YgRHB-NARRWZyuaBjAxNzExIERvbm5hIEx5bm4gRHJpdmUsIFNteXJuYSwgR0EsIFVuaXRlZCBTdGF0ZXM
If I then try to pass that place ID to Geocoder I get status of ZERO_RESULTS and no places:
new google.maps.Geocoder().geocode(
{ placeId: "thatLongPlaceId"},
function (results, status) { /* no place data returned */ });
But at the same time if I make the following GET call the same placeId returns the expected address:
https://maps.googleapis.com/maps/api/place/details/json?key=MyApiKey&placeid=ChIJx55rzdwQ9YgRHB-NARRWZyuaBjAxNzExIERvbm5hIEx5bm4gRHJpdmUsIFNteXJuYSwgR0EsIFVuaXRlZCBTdGF0ZXM
What am I doing wrong? What's the proper way of using that long place ID to get place data?
I think the autocomplete endpoint is not returning valid placeids as of today or yesterday. I'm running into a similar problem.
See Google Place Autocomplete API returning wrong place id
The ZERO_RESULTS problem isn't because of the long place ids. To see this, try using the (long) place id from an autocomplete result for an address in Sydney, for e.g.
ChIJr-__yjeuEmsR3RMEBNOoUn-aBjQ0OCBQaXJyYW1hIFJvYWQsIFB5cm1vbnQsIE5ldyBTb3V0aCBXYWxlcywgQXVzdHJhbGlh
The call to Geocoder.geocode should return 1 result.
Long story short, the Geocoding API (that google.maps.Geocoder uses) hasn't been fixed for the problem in https://code.google.com/p/gmaps-api-issues/issues/detail?id=11107.

Geofire-js: Return all key's whose instantaneous location matches geoQuery

I’m working with Firebase on a project, and I’m having trouble wrapping my head around how the querying works. I’m trying to query and find any other user’s that are within a certain radius of the current user’s location.
I’m able to create the the geoQuery, but I’m not sure how to return the user's whose locations are within the radius.
From the examples, it seems that key_entered is only triggered when a location in the DB changes. All my location’s are all static once set, so I just want to do a query, then return all the user’s whose instantaneous location falls within the queries radius.
Is this possible with Geofire?
My location information is saved in Firebase like this:
users {
uid: {
location: {
g: 'GeoHash',
l: [lat, long]
}
}
}
Here's my Query:
let geoQuery = geoFire.query({
center: [currentUser.location.l[0], currentUser.location.l[1]],
radius: 1000
});
The issue was that I hadn't be storing the locations using geoFire's set method, so the geoHash was never created, giving geoFire nothing to query off of. Thanks for the help #jacob!

Javascript get data from sql using object with openlayers 3

I added some points on the map using javascript(OpenLayers3). These points' positions are fetching from sql(randomly generated). I have to create a pop-up menu about each object on the map. This pop-up includes the data information of each selected point but ı dont know how to get the data from sql using the object on the map.
Points Img
How to get data ? Img
One way to do it is to transfer ID from your sql for each object in map. You can add custom property in that way:
var pointFeature = new ol.Feature({
geometry: new ol.geom.Point([0, 0]),
id: 25
});
Next use click event to get feature and ID:
map.on('click', function(evt) {
var feature = map.forEachFeatureAtPixel(evt.pixel,
function(feature) {
return feature;
});
if (feature) {
alert(feature.get('id'));
}
});
Here you go! All that remain is to make ajax request to your asp.net with ID as parameter.
Another way is to transfer ALL attributes from your sql on map from the beginning, so you won't need to make ajax request - you could just get properties from feature. But be careful as this solution will increase your point data greatly.

Google Maps API V3 - Anyway to retrieve Autocomplete results instead of dropdown rendering it?

I need to render the results of google.maps.places.Autocomplete in a custom way, instead of the default drop down menu.
Is there any way I can use the Google Maps API V3 to retrieve the same info of Autocomplete, but in an Array per request instead of the API do the rendering for me?
PS1:
I wondered I could use the google.maps.places.PlaceSearchRequest. But, differently from Autocomplete, it restricts my location. It cannot be used to search worldwide.
http://code.google.com/apis/maps/documentation/javascript/reference.html
http://code.google.com/apis/maps/documentation/places/index.html
PS2:
The default way I am (currently) using Autocomplete restricts me. I must pass an input element, and the API renders the dropdown, which I do NOT want.
var autocomplete, input_el, place_changed_trigger_func;
input_el = <an input element>;
place_changed_trigger_func = function() {
var place = autocomplete.getPlace();
// do whatever you want with place
};
autocomplete = new google.maps.places.Autocomplete(input_el, {types: ["geocode"]});
google.maps.event.addListener(autocomplete, 'place_changed', place_changed_trigger_func);
I believe this is the link that #jegnag meant to include.
https://developers.google.com/maps/documentation/javascript/places-autocomplete#place_autocomplete_service
You can access the array of results (places in addition to suggested search terms), you can even add your own custom results to this array, and display everything however you choose.
Have you explored the Autocomplete function of the Places API webservice? That appears to be what you're looking for.
Try the AutocompleteService class:
"You can use the AutocompleteService class to retrieve the query or place prediction data returned from the Autocomplete service. Calling either the getPlacePredictions() or getQueryPredictions() methods will return an array of five prediction objects of the form:"

Categories