Exemplo n.º 1
0
        public string GetLocation(string ip)
        {
            IPGeolocationAPI  api       = new IPGeolocationAPI("7461e5a511964d22ac7310cf8a9e4f02");
            GeolocationParams geoParams = new GeolocationParams();

            geoParams.SetIp(ip);
            Geolocation geolocation = api.GetGeolocation(geoParams);

            if (geolocation.GetStatus() == 200)
            {
                var location = geolocation.GetCity();
                repo.UpdateLocation(Current_idUser, location);
                return(location);
            }
            return("Default");
        }
Exemplo n.º 2
0
        public static void GetLocation()
        {
            // Create IPGeolocationAPI object, passing your valid API key
            IPGeolocationAPI api = new IPGeolocationAPI("7f568c0011f94605ab23aac3e1aa3ea6");

            // Get geolocation for IP address and fields

            GeolocationParams geoParams = new GeolocationParams();

            geoParams.SetIPAddress("172.220.225.50");
            geoParams.SetFields("geo,time_zone,currency");

            Geolocation geolocation;

            try
            {
                geolocation = api.GetGeolocation(geoParams);
                // Check if geolocation lookup was successful
                if (geolocation.GetStatus() == 200)
                {
                    Console.WriteLine("Last Know Location: ");
                    Console.WriteLine(geolocation.GetTimezone().GetCurrentTime());
                    Console.WriteLine("\nCountry: " + geolocation.GetCountryName());
                    Console.WriteLine("State/Province: " + geolocation.GetStateProvince());
                    Console.WriteLine("City: " + geolocation.GetCity());
                    Console.WriteLine("Lat: " + geolocation.GetLatitude());
                    Console.WriteLine("Long: " + geolocation.GetLongitude());
                }
                else if (geolocation.GetStatus() != 200)
                {
                    Console.WriteLine("Data cannot be retrieved.");
                }
            }
            catch
            {
                Console.WriteLine("Cannot retrieve data.");
            }



            Console.ReadKey();
        }
Exemplo n.º 3
0
 public void LocateAddress(IPAddress address)
 {
     Logger.LogTrace($"address=|{address.ToString()}|");
     if (!IPAddress.IsLoopback(address))
     {
         try
         {
             GeolocationParams geoParams = new GeolocationParams();
             geoParams.SetIp(address.ToString());
             geoInfo = Api.GetGeolocation(geoParams);
             if (geoInfo.GetStatus() != (int)HttpStatusCode.OK)
             {
                 Logger.LogError(geoInfo.GetMessage());
             }
         } catch (Exception e)
         {
             Logger.LogError($"Can't initialize IPGeolocation: |{e.Message}|");
         }
     }
 }
Exemplo n.º 4
0
        protected override Task <Location> GetLocationByIpInner(string ipAddress)
        {
            var api       = new IPGeolocationAPI(ApiKey);
            var geoParams = new GeolocationParams();

            geoParams.SetIp(ipAddress);

            var geolocation = api.GetGeolocation(geoParams);

            if (geolocation.GetStatus() != 200)
            {
                throw new Exception();
            }

            return(Task.FromResult(new Location()
            {
                City = geolocation.GetCity(),
                Country = geolocation.GetCountryName(),
                Latitude = double.Parse(geolocation.GetLatitude(), System.Globalization.CultureInfo.InvariantCulture),
                Longitude = double.Parse(geolocation.GetLongitude(), System.Globalization.CultureInfo.InvariantCulture)
            }));
        }