Software Developer

Latitude, Longitude With jQuery Autocomplete City Widget, Ajax

When using the jQuery autocomplete widget to fill in city names, you can return more than just the state and country names from within the same ajax function. If you’re using it to pull from GeoNames webservices, then here is how you can also get the latitude and longitude decimal coordinates, as well as the degree, minute formatted coordinates, like 25n11, 80w11. This is only a clip from the entire jQuery for autocomplete.

                    success: function( data ) {
                        response( $.map( data.geonames, function( item ) {
                            return {
                                label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName, 
                                value: item.name,
								lngdeci: item.lng,
								latdeci: item.lat,
								latdms: Math.floor(Math.abs(item.lat)) + 
									// if lat is +, North. if is -, South.
									((item.lat > 0) ? "n" : "s") + 
									// take only decimal, Multiply by 60, take only whole integer of answer for minutes
									(Math.floor((Math.abs(item.lat)-Math.floor(Math.abs(item.lat)))*60)), 
									
								lngdms: Math.floor(Math.abs(item.lng)) + 
									// if lng is +, East. if is -, West.
									((item.lng > 0) ? "e" : "w") + 
									// take only decimal, Multiply by 60, take only whole integer of answer for minutes
 									(Math.floor((Math.abs(item.lng)-Math.floor(Math.abs(item.lng)))*60)),

								
                            }
                        }));
                    }

By

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]