0

when use give a lat and lng ,i should give the city and State of US. try to use google map place api, but it didn't has the place type

search in google, get some answers :

1.create the database of city data,and use the sql to calculate the distance.but I didn't get any accurate data,and some data are not free.

2.use the third database geoNames,get the data, but I worry the website limit the count of request. So did you have any answer ,help

duncan
  • 31,401
  • 13
  • 78
  • 99
Sun
  • 75
  • 7
  • You probably want to use the geocoder. See this fiddle for an example: http://jsfiddle.net/upsidown/5S3ce/ and watch the console for the results. If you have issues getting the information you want, open a new question with code that shows what you have tried. – MrUpsidown Feb 24 '16 at 11:45

1 Answers1

0

If you have lat, and lng, you better got for calling Geocoder.geocode. Once you get OK in resonse, you can check address_componenets with type "administrative_area_level_n"

var latlng = new google.maps.LatLng(lat, lng);
        var geocoder = new google.maps.Geocoder();
        geocoder.geocode({ 'latLng': latlng }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                if (results[1]) {
                    for (var i = 0; i < results[0].address_components.length; i++) {
                        for (var b = 0; b < results[0].address_components[i].types.length; b++) {
                            if (results[0].address_components[i].types[b] == "administrative_area_level_2") {
                                var city = results[0].address_components[i];
                            }
                        }
                    }
                }
            }
  • Please post complete and working code and explain what it does. – MrUpsidown Feb 24 '16 at 11:38
  • @MrUpsidown, looks self explanatory to me, but added as per your request (for the sake of completeness) – farhan iqbal Feb 29 '16 at 10:01
  • 1
    It might be for you and others too. But many people here are beginner programmers and therefore posting complete code is always better. Thanks for updating your code. – MrUpsidown Feb 29 '16 at 10:09