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).
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.