Reputation: 8414
I'm using the version 3 API and I have a text input that the user enters a search string that is used to geocode via the API. The map is updated and a marker is placed at the resulting match. This all works fine except for a few cases. Here is the code:
geocoder.geocode({
'address': address,
region: 'AU'
}, function(results, status) {
// do stuff with results
}
The issue is where the user enters a lat/lng pair, e.g. 22°53'21"S, 118°28'36"E
. When the coordinates fall into a rural area, the returning match is not an exact representation of the entered coordinates but appears to be the center of the matched locality from Google. E.g. result is -22.4951839, 118.73455850000005
. I.e., the marker is shifted from what the user entered.
So, how can I prevent this locality matching/centering in the cases where users enter a lat/lng? In these cases I want the marker to be placed at the lat/lng for the parsed coordinates, not the center of the matched locality (but I want to keep using the API as it handles/parses all the various lat/lng formats and I also store the locality/address information).
Upvotes: 1
Views: 1425
Reputation: 8414
I had to revisit this issue today and I found a JS library that did a fairly good job at parsing user input for coordinates (decimal and DMS):
http://dbarbalato.github.io/magellan/
Will only handle lat
and lng
separately though - so I had to do a split(',')
on the input.
Upvotes: 0
Reputation: 331
I faced a similar issue. If you decide to write your own parsing code, then if the only alpha characters you have are NSEWnsew, then the user likely entered coordinates.
You can then change all non-numbers to spaces and analyze the string: 6 numbers means you have degrees, minutes, seconds 4 numbers means you have degrees, decimal minutes 2 numbers means you have decimal degrees
The only other issue is to handle the characters: NSEW+-
Upvotes: 1
Reputation: 8819
You could do pattern matching on the input box, test for numbers plus punctuation and °. If it matches, convert to decimal latitude/longitude and create the marker, otherwise run through the geocoder.
Upvotes: 1