Google Maps API v3 Geocoder.geocode() argument "location" not works - google-maps-api-3

There is "location" property in GeocoderRequest object, that defines "LatLng about which to search. Optional.", I'm trying to use it, but it does not work:
geocoder.geocode( { address: control.value, location: new google.maps.LatLng(59.938531, 30.313497)}, function(results, status) {....})
For example, when I'm searching for some address in 59.938531, 30.313497, so results contains 2 objects, and the right address has index [1] but not [0].

Language parameter in JavaScript call by geocoder not supported:
http://code.google.com/intl/th-TH/apis/maps/documentation/javascript/reference.html#Geocoder

The location is not valid. You want either address or latLng. The GeocodeRequest object literal contains the following fields:
{
address: string,
latLng: LatLng,
bounds: LatLngBounds,
language: string,
region: string
}

Related

Filter by type in Google AutocompleteService

I want to filter away establishments in my autocomplete service.
I have tried
var service = new google.maps.places.AutocompleteService;
var request = {
"input": "Nørregade",
"componentRestrictions": { "country": "dk" },
"types": ["(cities)", "(regions)", "geocode"]
};
service.getPlacePredictions(request, function(predictions, status) {
console.log(status) # => INVALID_REQUEST
});
(http://jsfiddle.net/gdk0j9eg/1/)
Following this link (https://developers.google.com/maps/documentation/javascript/reference#AutocompletionRequest) it seems that the autocomplete service does indeed take these types.
What am I doing wrong here?
See the documentation, you are only allowed to use a single type or type collection:
You may restrict results from a Place Autocomplete request to be of a certain type by passing a types parameter. The parameter specifies a type or a type collection, as listed in the supported types below. If nothing is specified, all types are returned. In general only a single type is allowed. The exception is that you can safely mix the geocode and establishment types, but note that this will have the same effect as specifying no types.

Google map places API getPlace() only return name attribute for some addresses

I have an address search box using the google map autocomplete library:
var autocompleter = new google.maps.places.Autocomplete(item);
There is an odd occasion that an address only returns a name attribute:
Object {name: "138 Manukau Road, Pukekohe, New Zealand"}
But other addresses are giving more data, such as:
Object {address_components: Array[7], adr_address: "<span class="street-address">430 Queen St</span>, …n>, <span class="country-name">New Zealand</span>", formatted_address: "430 Queen St, Auckland, Auckland 1010, New Zealand", geometry: Object, icon: "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png"…}address_components: Array[7]adr_address: "<span class="street-address">430 Queen St</span>, <span class="extended-address">Auckland</span>, <span class="locality">Auckland</span> <span class="postal-code">1010</span>, <span class="country-name">New Zealand</span>"formatted_address: "430 Queen St, Auckland, Auckland 1010, New Zealand"geometry: Objecthtml_attributions: Array[0]icon: "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png"id: "00fce9b1c43ac960068949cbf32eecb587b0b020"name: "430 Queen St"place_id: "ChIJQfHW8OVHDW0RyHgQRLy8fKc"reference: "CqQBlgAAAIDnkWNQ4cmU624FV6l_bAxmI27czZoytmzrrEWVaXgR5LcZuFqt1cL3WIMzoWhmZNhftRzhLUVwpFjqmw3qwKIqugj02HrvU5x6PtUvepPNPV-08pin_PvRU-__mMMH3N2vILIOLM_AnYFMqNG5MArF4ChZXJxZj6vk7PI3ORJe1W6QjIXoPgesL379E4WUCjrZ0fjv3KgqzB-G4f-8A5MSEN5S47-QZqkY5sl37cIQFWQaFLg4InSVLpYGg8n1gGO958TcA4UK"scope: "GOOGLE"types: Array[1]url: "https://maps.google.com/maps/place?q=430+Queen+St,+Auckland,+Auckland+1010,+New+Zealand&ftid=0x6d0d47e5f0d6f141:0xa77cbcbc441078c8"vicinity: "Auckland"__proto__: Object
I have found a similar issue which raised by someone in 2012, and looks like it has not been attended.
This problem was happening intermittently for me and usually out of the blue.
Turns out that if you get a result from Autocomplete with only a name property, you can use the google.maps.places.AutocompleteService to finish the job.
For example, call this if you only get a name back (sending the input element in el)
function getPlace(result, el, callback) {
var autocompleteService = new google.maps.places.AutocompleteService();
if (result.name.length > 0) {
var d = { input: result.name, offset: result.name.length };
autocompleteService.getPlacePredictions(d, function (list, status) {
if (list == null || list.length == 0) callback(null);
var placesService = new google.maps.places.PlacesService(el);
var ref = { 'reference': list[0].reference }
placesService.getDetails(ref, function (detailsResult, placesServiceStatus) {
if (placesServiceStatus == google.maps.GeocoderStatus.OK) {
callback(detailsResult);
}
else {
callback(null);
}
});
});
}
}
This helped me a lot
http://plnkr.co/edit/GF3nM3XfYX9El2w11pGo?p=preview
Surprisingly the very same address now returning correct data, google must be watching these errors and fix them as soon as possible.
Maybe the google service is not able to geocode your address and only return to you the name of the address found. The reason could be that the service has no aditional data to give you for those adress.
You can just not show those incomplete address to the user or try to geocode that address by another service like OSM Geocoder
http://wiki.openstreetmap.org/wiki/Nominatim
From Google Maps Autocomplete Reference:
Returns the details of the Place selected by user if the details were
successfully retrieved. Otherwise returns a stub Place object, with
the name property set to the current value of the input field.
So the answer is that the getPlace method is just failing for specific places. Not sure how to solve this, but gets us one step closer to the answer.
EDIT: FIXED IT!
For my app, I'm loading multiple libraries for google maps (geometry and places). Switching the order that the libraries were loaded fixed the issue, and I don't know why.
Change:
maps.googleapis.com/maps/api/js?v=3&key=[KEY]&libraries=geometry,places
to
maps.googleapis.com/maps/api/js?v=3&key=[KEY]&libraries=places,geometry

How do I use ObjectID from a mongoimport?

I use a mongoimport to import a bunch of large csv files into a meteor collection, however when they do the insertions the _id values are ObjectID, whereas meteor uses string ids. There's a small blurb on ObjectIDs in the meteor docs here but I don't really understand what I am supposed to do. For example, using Iron Router I have a single route like so
this.route('profileView', {
path: '/profiles/:_id',
notFoundTemplate: 'notFound',
fastRender: true,
waitOn: function() {
return [Meteor.subscribe('singleProfile', this.params._id, Meteor.userId())];
},
data: function() {
Session.set('currentProfileId', this.params._id);
return Profiles.findOne({
_id: this.params._id
}, {
fields: {
submitted: 0
}
});
}
but the url of the route is of type object and looks like http://localhost:3000/profiles/ObjectID(%22530845da3621faf06fcb0802%22). It also doesn't return anything and the page renders blank. Here's the publication.
Meteor.publish('singleProfile', function(id, userId) {
return Profiles.find({
_id: id,
userId: userId,
forDel: {
$ne: true
}
});
});
I guess my question is, how am I supposed to use ObjectIDs so that the routes use just the string portion of the ObjectID, and how do I return the data properly?
Update: I've managed to get the ObjectID out of the url by changing the link to the view from Details to Details so the url is now http://localhost:3000/profiles/530845da3621faf06fcb0802. Unfortunately, the page still renders blank, and I am not sure if that's because of the way I am subscribing, publishing, or finding the collection item.
Summing up the comment thread as an answer:
The string part of the ObjectID can be obtained by simply calling ._str on the id as
id._str
You can also craft an ObjectID from a hex string by using
new Meteor.Colletion.ObjectID(hexstring)
So when you access your route using Details you can craft your find like:
Profiles.findOne({
_id: new Meteor.Collection.ObjectID(this.params._id)
});
Generally speaking, when working with ObjectID's, you will find yourself needing some antipatterns to convert from string to objectId or vice versa, so a utility like the following will come in handy:
IDAsString = this._id._str ? this._id._str : this._id
and
IDAsObjectId = this._id._str ? this._id : new Meteor.Collection.ObjectID(this._id)
Please also take a look at github.com/meteor/meteor/issues/1834 and groups.google.com/forum/#!topic/meteor-talk/f-ljBdZOwPk for pointers and issues around using ObjectID's.

Google maps direction renderer exact match only

I am using DirectionRenderer(gmap3) to show the user directions. The problem is it shows a match even if it cannot find an exact match. Eg: SomeFakePlace, myRealCity will match myRealCity even if it cannot match SomeFakePlace.
So it shows the directions from City's center to the place, instead. The destination is fixed(myLatLng)
I want it to return null and not show a route if cannot find one. I have decent error display to handle that.
$("#map-canvas-single").gmap3({
getroute:{
options:{
origin:$("#directions-from").val(),
destination:myLatlng,
travelMode: google.maps.DirectionsTravelMode.DRIVING
},
callback: function(results){
console.log(results);
var point= results.routes[0].overview_path[0]
window.directionMarker = new google.maps.Marker({
position: new google.maps.LatLng(point.jb,point.kb),
title:$("#directions-from").val(),
//icon:"http://maps.google.com/mapfiles/ms/icons/<?php if($this->listing->type=="pg"):?>green<?php else: ?>purple<?php endif;?>-dot.png"
});
window.directionMarker.setMap($(this).gmap3("get"));
if(!results)
noty({text:"Place not found!",type:"error"});
else
{
$(this).gmap3({
directionsrenderer:{
container: $("#directions-container"),
id:"directions",
options:{
directions:results,
suppressMarkers :true //<<Look here>>
}
}
});
}
}
}
});
The code works fine and all. I think this the fault of direction renderer service, not gmaps. I am sure htere must be some parameter for an exact match
I'm not familiar with Google Maps API, but what I'd do is do a geocode lookup on the source address and find the lat, lng. You'll usually get coordinates with levels of confidence, so you can have a minimum threshold below which to throw an error.
https://developers.google.com/maps/documentation/geocoding/
Also, don't forget mapquest.
http://developer.mapquest.com/web/products/dev-services/geocoding-ws

How to search for cities in an area using Google Maps v3?

I tried to search for all cities within a visible map's bounds. How can I do that?
Below is what I tried to do:
$.fn.gmap3.geocoder.geocode({ 'address': 'Georgia' }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
globalMap.setCenter(results[0].geometry.location);
var resultBounds = new google.maps.LatLngBounds(
results[0].geometry.viewport.getSouthWest(),
results[0].geometry.viewport.getNorthEast()
);
globalMap.fitBounds(resultBounds);
// get cities in the map
var service = new google.maps.places.PlacesService(globalMap);
var request = {
bounds: resultBounds,
types: ['locality']
};
service.search(request, function (results, status) {
debugger;
});
}
});
But the result is ZERO_RESULTS. Maybe the reason is that the results are restricted to a radius of 50.000 meters?
Anyone knows how to solve my problem? Thanks a lot.
--UPDATE--
Thank, Sean, for reading my post carefully and give detail feedback.
This is how I refer to the lib:
src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"
I also added more detail to the geocode function to get more precise result. But I still don't get the results I want.
Check the list in this page: https://developers.google.com/places/documentation/supported_types, I realize that almost all items in the first list returns values but not for the second list. The only item return value is 'political' and it returns only 1 instead of 20.
This is my code after modifing:
this.setCenterByAddress = function (address, region) {
$.fn.gmap3.geocoder.geocode({ 'address': address, 'region': region }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
globalMap.setCenter(results[0].geometry.location);
var resultBounds = new google.maps.LatLngBounds(
results[0].geometry.viewport.getSouthWest(),
results[0].geometry.viewport.getNorthEast()
);
globalMap.fitBounds(resultBounds);
// get cities in the map
var service = new google.maps.places.PlacesService(globalMap);
var request = {
bounds: resultBounds,
types: ['country',
'administrative_area_level_1',
'administrative_area_level_2',
'administrative_area_level_3',
'colloquial_area',
'country',
'floor',
'geocode',
'intersection',
'locality',
'natural_feature',
'neighborhood',
'political',
'point_of_interest',
'post_box',
'postal_code',
'postal_code_prefix',
'postal_town',
'premise',
'room',
'route',
'street_address',
'street_number',
'sublocality',
'sublocality_level_4',
'sublocality_level_5',
'sublocality_level_3',
'sublocality_level_2',
'sublocality_level_1',
'subpremise',
'transit_station']
};
service.search(request, function (results, status) {
debugger;
});
}
});
}
MORE INFO: The same value return even when location and radius is used. And I use free map and receive "OVER_QUERY_LIMIT" all the time.
You shouldn't be limited to 50,000 meters unless you use the location and radius option, which you are not; you are using bounds. I suggest backing out a level and digging into the results that are returned from the starting call to geocode, because your usage of the PlacesService appears to be correct. What are the values internal to the resultBounds object? I also notice that you aren't using region biasing when you call the geocoder and it may be that "Georgia" is not sufficiently specific. For example, do you mean the region within the Russian Federation or the US state? And I'm not sure what URL you are using to load the Google Maps API & places library, but that could also be effecting your results.
I'd double-check the results coming back from the geocoder, because unless I am missing something, it looks like your basic approach is sound.
You can't return more than two near by localities around a particular latitude-longitude with Geocoding API/Places API
Please see this thread on Google Places API forum
This is not possible with the Google Places API as political results
such as locality, neighbourhood and sublocality are returned to
identify the area of the request and are limited to two per request.

Resources