var gps = new NativeWrapperGps();
gps.LocationChanged += delegate ( object sender, NativeWrapperGps.LocationChangedEventArgs args)
{
//..
};
gps.Open();
// ..
gps.Close();
* This source code was highlighted with Source Code Highlighter .
public static class RilInterop
{
public delegate void RilResultCallback( uint dwCode, IntPtr hrCmdID, IntPtr lpData, uint cbData, uint dwParam);
public delegate void RilNotifyCallback( uint dwCode, IntPtr lpData, uint cbData, uint dwParam);
[DllImport( "ril.dll" , EntryPoint = "RIL_Initialize" )]
public static extern IntPtr Initialize( uint dwIndex, RilResultCallback pfnResult, RilNotifyCallback pfnNotify, uint dwNotificationClasses, uint dwParam, out IntPtr lphRil);
[DllImport( "ril.dll" , EntryPoint = "RIL_GetCellTowerInfo" )]
public static extern IntPtr GetCellTowerInfo( IntPtr hRil);
[DllImport( "ril.dll" , EntryPoint = "RIL_Deinitialize" )]
public static extern IntPtr Deinitialize( IntPtr hRil);
}
* This source code was highlighted with Source Code Highlighter .
public class RilWrapper
{
private static RilCellTowerInfo _towerDetails;
private static readonly AutoResetEvent WaitHandle = new AutoResetEvent( false );
public static CellTower GetCellTowerInfo()
{
IntPtr rilHandle;
if (RilInterop.Initialize(1, CellDataCallback, null , 0, 0, out rilHandle) != IntPtr .Zero)
{
return null ;
}
RilInterop.GetCellTowerInfo(rilHandle);
WaitHandle.WaitOne();
RilInterop.Deinitialize(rilHandle);
return new CellTower
{
TowerId = Convert .ToInt32(_towerDetails.DwCellID),
LocationAreaCode = Convert .ToInt32(_towerDetails.DwLocationAreaCode),
MobileCountryCode = Convert .ToInt32(_towerDetails.DwMobileCountryCode),
MobileNetworkCode = Convert .ToInt32(_towerDetails.DwMobileNetworkCode),
};
}
public static void CellDataCallback( uint dwCode, IntPtr hrCmdID, IntPtr lpData, uint cbData, uint dwParam)
{
_towerDetails = new RilCellTowerInfo();
Marshal.PtrToStructure(lpData, _towerDetails);
WaitHandle.Set();
}
}
* This source code was highlighted with Source Code Highlighter .
public class OpenCellIdProvider : ICellToGpsProvider
{
public GpsLocation GetCoordinates( int cellId, int locationAreaCode, int mobileCountryCode, int mobileNetworkCode)
{
GpsLocation result = null ;
var doc = XDocument .Load( String .Format( "http://www.opencellid.org/cell/get?key={4}&mnc={3}&mcc={2}&lac={1}&cellid={0}" , cellId, locationAreaCode, mobileCountryCode, mobileNetworkCode, Key));
if ((doc.Root != null ) && (doc.Root. Attribute (XName.Get( "stat" )).Value == "ok" ))
{
var cellInfoElem = doc.Root.Element(XName.Get( "cell" ));
if (cellInfoElem != null )
{
result = new GpsLocation
{
Latitude = Convert .ToDouble(cellInfoElem. Attribute (XName.Get( "lat" )).Value.Replace('.', ',')),
Longitude = Convert .ToDouble(cellInfoElem. Attribute (XName.Get( "lon" )).Value.Replace('.', ','))
};
}
}
return result;
}
}
* This source code was highlighted with Source Code Highlighter .
public GpsLocation GetLocation()
{
double ? lat = null ;
double ? lon = null ;
var html = GetHtml( "http://www.geoiptool.com/en/" );
var lines = Regex.Matches(html.Replace( "\r" , " " ).Replace( "\n" , " " ), @"<tr>(.)+?</tr>" , RegexOptions.IgnoreCase);
foreach (Match line in lines)
{
try
{
if (line.Value.Contains( "Longitude:" ))
{
lon = Convert .ToDouble(Regex.Match(line.Value, @"<td(.)*?>(?<val>[0-9\.]+)</td>" ).Groups[ "val" ].Value.Replace('.', ','));
}
if (line.Value.Contains( "Latitude:" ))
{
lat = Convert .ToDouble(Regex.Match(line.Value, @"<td(.)*?>(?<val>[0-9\.]+)</td>" ).Groups[ "val" ].Value.Replace('.', ','));
}
}
catch (FormatException)
{
}
}
return (lat != null ) && (lon != null ) ? new GpsLocation { Latitude = ( double )lat, Longitude = ( double )lon } : null ;
}
* This source code was highlighted with Source Code Highlighter .
public interface ILocationProvider
{
GpsLocation GetLocation();
string ProviderName { get ; }
}
* This source code was highlighted with Source Code Highlighter .
public class LocationManager : ILocationProvider
{
private readonly List <ILocationProvider> _providers = new List <ILocationProvider>();
private string _lastProviderName = null ;
public void ClearProviders()
{
_providers.Clear();
}
public void RegisterProvider(ILocationProvider provider)
{
if (_providers.Contains(provider)== false )
{
_providers.Add(provider);
}
}
public void RemoveProvider(ILocationProvider provider)
{
if (_providers.Contains(provider) == true )
{
_providers.Remove(provider);
}
}
#region ILocationProvider Members
public GpsLocation GetLocation()
{
GpsLocation result = null ;
_lastProviderName = null ;
foreach ( var provider in _providers)
{
try
{
result = provider.GetLocation();
if (result != null )
{
_lastProviderName = provider.ProviderName;
break ;
}
}
catch (Exception ex)
{
continue ;
}
}
return result;
}
public string ProviderName
{
get { return _lastProviderName; }
}
#endregion
}
* This source code was highlighted with Source Code Highlighter .
LocationManager manager = new LocationManager();
manager.RegisterProvider( new GpsLocationProvider());
manager.RegisterProvider( new CellLocationProvider( new OpenCellIdProvider()));
manager.RegisterProvider( new GeoIpLocationProvider());
var location = manager.GetLocation();
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/78724/