Google's Geocoder returns wrong country, ignoring the region hint

后端 未结 13 1015
孤城傲影
孤城傲影 2020-12-07 21:03

I\'m using Google\'s Geocoder to find lat lng coordinates for a given address.

    var geocoder = new google.maps.Geocoder();
    geocoder.geocode(
    {
            


        
13条回答
  •  一生所求
    2020-12-07 21:25

    I have Created Border All Around Uk and check if my Lat and Lng are in the Range. For 3 k addresses I have around 10 up to 20 addresses in US. I just Ignore them(I can do that in my case) I use lat and lng for creating multi markers on static map with auto zoom. I will share my Solution maybe this will be help for someone. Also I'm Happy to hear different Solutions for my case.

        private static string ReturnLatandLng(string GeocodeApiKey, string address)
        {
            string latlng = "";
    
            Geocoder geocoder = new Geocoder(GeocodeApiKey);
    
            var locations = geocoder.Geocode(address);
    
            foreach (var item in locations)
            {
    
                double longitude = item.LatLng.Longitude;
                double latitude = item.LatLng.Latitude;
                double borderSouthLatitude = 49.895878;
                double borderNorthLatitude = 62.000000;
                double borderWestLongitude = -8.207676;
                double borderEastLongitude = 2.000000;
    
                //Check If Geocoded Address is inside of the UK
                if (( (borderWestLongitude < longitude) && (longitude < borderEastLongitude) ) && ( (borderSouthLatitude < latitude) && (latitude < borderNorthLatitude) ) )
                {
                    latlng = item.LatLng.ToString();
                }
                else
                {
                    latlng = "";
                    Console.WriteLine("GEOCODED ADDRESS IS NOT LOCATED IN UK ADDRESSES LIST. DELETING MARKER FROM MAP.....");
                }
            }
            return latlng;
        }
    

提交回复
热议问题