Find closest airport based on latitude and longitude - geolocation

How do I find closest airport using longitude and latitude ?
Any specific web services and any database to achieve ?

One WebService I found is airports.pidgets.com
This is an example:
XML format
http://airports.pidgets.com/v1/airports?near=45.3515,9.3753
JSon format
http://airports.pidgets.com/v1/airports?near=45.3515,9.3753&format=json
[Edit]
Found another webservice on aviationweather.gov (only XML and CSV)
http://aviationweather.gov/adds/dataserver_current/httpparam?dataSource=stations&requestType=retrieve&format=xml&radialDistance=20;9.3753,45.3515
From both sites you can download a "static" airports list, to perform offline search.
Regards

You need a dataset with fields for the airport`s latitude and
longitude
Use the calculation for Great-Circle distance (GCD) as outlined on the page linked below
Wikipedia article on GCD
Please provide example code/specify the language if you would like further and more specific help
CODE:
Taken from another webpage (now defunct, used waybackmachine)
using System;
namespace HaversineFormula
{
/// <summary>
/// The distance type to return the results in.
/// </summary>
public enum DistanceType { Miles, Kilometers };
/// <summary>
/// Specifies a Latitude / Longitude point.
/// </summary>
public struct Position
{
public double Latitude;
public double Longitude;
}
class Haversine
{
/// <summary>
/// Returns the distance in miles or kilometers of any two
/// latitude / longitude points.
/// </summary>
/// <param name=”pos1″></param>
/// <param name=”pos2″></param>
/// <param name=”type”></param>
/// <returns></returns>
public double Distance(Position pos1, Position pos2, DistanceType type)
{
double R = (type == DistanceType.Miles) ? 3960 : 6371;
double dLat = this.toRadian(pos2.Latitude - pos1.Latitude);
double dLon = this.toRadian(pos2.Longitude - pos1.Longitude);
double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
Math.Cos(this.toRadian(pos1.Latitude)) * Math.Cos(this.toRadian(pos2.Latitude)) *
Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
double c = 2 * Math.Asin(Math.Min(1, Math.Sqrt(a)));
double d = R * c;
return d;
}
/// <summary>
/// Convert to Radians.
/// </summary>
/// <param name="val"></param>
/// <returns></returns>
private double toRadian(double val)
{
return (Math.PI / 180) * val;
}
}
}
Pseudocode:
This pseudocode should give you the answer you are looking for. I didn't test this and the C# will probably have syntactic errors but the gist of it should be clear.
/* Set parameters */
Position currentPosition = new Position();
Position airportPosition = new Position();
Double minDistance = Double.MaxValue;
String closestAirportName = "UNKNOWN";
Haversine hv = new Haversine();
/* Set current position, remains fixed throughout */
currentPosition.Latitude = 0.000;
currentPosition.Longitude = 0.000;
/* Compare distance to each airport with current location
* and save results if this is the closest airport so far*/
Foreach (airport in airports) {
airportPosition = new Position(airport.Lat, airport.Lon);
Double distanceToAirport = hv.Distance(currentPosition, airportPosition, DistanceType.Kilometers)
if (distanceToAirport < minDistance) {
minDistance = distanceToAirport
closestAirportName = airport.Name
}
}

On which platform are you coding, Durga? Is it Android?
In this case, you could use the Google Maps API:
https://developers.google.com/maps/
and, in particular, Google Places:
https://developers.google.com/places/
Broswe their documentation for details. In particular, check their license.

this.nearestAirport = this.airports.find((airport) => {
return (Math.round(airport.latitude) === Math.round(currentLocation.latitude) &&
Math.round(airport.longitude) === Math.round(currentLocation.longitude));
});

To Find nearest airport and to get directions to reach there from a specified point (Lat,Lan)
Here is a Google method,without any database to achieve this :
onclick="getNeighbourhood('<%= propLat %>','<%= propLan %>');"
For full code visit here FULL NEAREST AIRPORT SCRIPT AND STYLE
function getNeighbourhood(propLatQ,propLanQ) {
propLat=propLatQ;
propLan=propLanQ;
var myLatlng = new google.maps.LatLng(propLat,propLan);
var myOptions = {
zoom: 8,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map"), myOptions);
places = new google.maps.places.PlacesService(map);
google.maps.event.addListener(map, 'tilesloaded', tilesLoaded);
autocomplete = new google.maps.places.Autocomplete(document.getElementById('autocomplete'));
google.maps.event.addListener(autocomplete, 'place_changed', function() {
showSelectedPlace();
});

Related

Dart: How to efficiently initialize multiple final fields that depend on the same calculation?

I have a Dart class with multiple fields that have to be final, because the class extends another class marked with #immutable.
The values of these fields are supposed to be calculated when an instance of the class is created. In Dart, "final instance variables must be initialized before the constructor body starts" (from dartlang.org).
In that scope, you can only call static methods.
That works for me except some fields depend on the same calculation, meaning that the same calculation is done twice.
Is there any way to avoid that, i.e. by saving some temporary result?
My current code:
class _IntegralCurve extends Curve {
static double delta = 0.01;
_IntegralCurve(this.original) :
integral = calculateIntegral(original),
values = calculateNormalizedValues(original);
final Curve original;
final double integral; // Accessible to other classes.
final Map<double, double> values;
/// Does the actual integrating work. Called twice.
static Map<double, double> integrate(Curve original) {
double integral = 0.0;
final values = Map<double, double>();
for (double t = 0.0; t <= 1.0; t += delta) {
integral += original.transform(t) * delta;
values[t] = integral;
}
values[1.0] = integral;
return values;
}
/// Calculates the integral.
static double calculateIntegral(Curve curve) => integrate(curve)[1];
/// Calculates cumulative values.
static Map<double, double> calculateNormalizedValues(Curve curve) {
final values = integrate(curve);
for (final double t in values.keys) {
values[t] = values[t] / values[1];
}
return values;
}
double transform(double t) {
for (final key in values.keys)
if (key > t)
return values[key];
return values[1.0];
}
}
Calculate the values in a factory constructor:
class _IntegralCurve extends Curve {
static double delta = 0.01;
factory _IntegralCurve(Curve original) {
final integral = calculateIntegral(original),
final values = calculateNormalizedValues(original);
return _IntegralCourve._(original, integral, values);
}
_IntegralCurve._(this.original, this.integral, this.values);
You can use a factory to hide calculations within a constructor without loosing the final:
class Foo {
final int computed;
final int copy;
Foo._(this.computed, this.copy);
factory Foo() {
// calculate value
int value = 42;
return Foo._(value, value);
}
}
If you only want this extra value to be looked up by other classes you could use the get keyword to compute other fields. This value will be computed every time it gets called though.
// Declaration
class Foo {
const Foo(this.initialValue);
final double initialValue;
double get computedValue => initialValue + initialValue;
}
// Usage
main() {
final foo = Foo(20);
print("${foo.initialValue} ${foo.computedValue}"); // Would print `20 40`
}
https://dart.dev/guides/language/language-tour#getters-and-setters

DidUpdateUserLocation event never gets called when displaying Map on Xamarin IOS

I am currently trying to display my location using MKMapView on Xamarin IOS without much success. DidUpdateUserLocation event never gets called so no location coordinates are provided. I am using the below permission in my Info.plist file:
NSLocationWhenInUseUsageDescription
location
[Register("MapsViewController")]
public class MapsViewController : UIViewController {
private MKMapView mapView;
private CLLocationManager locationManager = newCLLocationManager();
private CLLocationCoordinate2D coords;
public override void ViewDidLoad() {
base.ViewDidLoad();
mapView = new MKMapView(new CGRect(0, 0, View.Frame.Width, View.Frame.Height));
mapView.ShowsUserLocation = true;
this.View.AddSubview(mapView);
locationManager.RequestWhenInUseAuthorization();
mapView.ShowsUserLocation = true;
mapView.DidUpdateUserLocation += (sender, e) => {
if (mapView.UserLocation != null) {
coords = mapView.UserLocation.Coordinate;
MKCoordinateSpan span = new MKCoordinateSpan(MilesToLatitudeDegrees(2),
MilesToLongitudeDegrees(2, coords.Latitude));
mapView.Region = new MKCoordinateRegion(coords, span);
}
};
}
public double MilesToLatitudeDegrees(double miles) {
double earthRadius = 3960.0;
double radiansToDegrees = 180.0 / Math.PI;
return (miles / earthRadius) * radiansToDegrees;
}
/// <summary>
/// Converts miles to longitudinal degrees at a specified latitude
/// </summary>
public double MilesToLongitudeDegrees(double miles, double atLatitude) {
double earthRadius = 3960.0;
double degreesToRadians = Math.PI / 180.0;
double radiansToDegrees = 180.0 / Math.PI;
// derive the earth's radius at that point in latitude
double radiusAtLatitude = earthRadius * Math.Cos(atLatitude * degreesToRadians);
return (miles / radiusAtLatitude) * radiansToDegrees;
}

OpenISO8583.Net BCD fix format unpack error

I downloaded the code from code.google and get the last version v0.5.2
I set a field in bcd fix format,which is N-6 in bcd format(bit._003_proc_code)
For ex:
*Field definition:
DefaultTemplate =new Template
{
{ Bit._002_PAN, FieldDescriptor.BcdVar(2, 19,Formatters.Ascii) },
{ Bit._003_PROC_CODE, FieldDescriptor.BcdFixed(3)},
{ Bit._004_TRAN_AMOUNT, FieldDescriptor.BcdFixed(6) },
..............
}
usage:
Iso8583 msg =new Iso8584();
msg[3]="000000";
when i unpack the message ,i can only get “0000” from message 3 .
is this a bug or error in definition
I would wait to hear from John Oxley to see if this is just a coding error. Now that I said that, I think it is a bug.
I had problems with the BcdFixed definition and ended up creating a new Fixed Length BCD formatter to work around the problem.
Here is what I did:
I created a class called FixedLengthBcdFormatter which is a variation of the FixedLengthFormatter class.
///
/// Fixed field formatter
///
public class FixedLengthBcdFormatter : ILengthFormatter
{
private readonly int _packedLength;
private readonly int _unPackedLength;
///<summary>
/// Fixed length field formatter
///</summary>
///<param name = "unPackedLength">The unpacked length of the field.</param>
public FixedLengthBcdFormatter(int unPackedLength)
{
_unPackedLength = unPackedLength;
double len = _unPackedLength;
_packedLength = (int)Math.Ceiling(len / 2);
}
#region ILengthFormatter Members
/// <summary>
/// Get the length of the packed length indicator. For fixed length fields this is 0
/// </summary>
public int LengthOfLengthIndicator
{
get { return 0; }
}
/// <summary>
/// The maximum length of the field displayed as a string for descriptors
/// </summary>
public string MaxLength
{
get { return _unPackedLength.ToString(); }
}
/// <summary>
/// Descriptor for the length formatter used in ToString methods
/// </summary>
public string Description
{
get { return "FixedBcd"; }
}
/// <summary>
/// Get the length of the field
/// </summary>
/// <param name = "msg">Byte array of message data</param>
/// <param name = "offset">offset to start parsing</param>
/// <returns>The length of the field</returns>
public int GetLengthOfField(byte[] msg, int offset)
{
return _unPackedLength;
}
/// <summary>
/// Pack the length header into the message
/// </summary>
/// <param name = "msg">Byte array of the message</param>
/// <param name = "length">The length to pack into the message</param>
/// <param name = "offset">Offset to start the packing</param>
/// <returns>offset for the start of the field</returns>
public int Pack(byte[] msg, int length, int offset)
{
return offset;
}
/// <summary>
/// Check the length of the field is valid
/// </summary>
/// <param name = "packedLength">the packed length of the field</param>
/// <returns>true if valid, false otherwise</returns>
public bool IsValidLength(int packedLength)
{
return packedLength == _packedLength;
}
#endregion
}
Modified the FieldDescription class / BcdFixed declaration
///
/// The bcd fixed.
///
///
/// The length.
///
///
///
public static IFieldDescriptor BcdFixed(int unpackedLength)
{
return Create(new FixedLengthBcdFormatter(unpackedLength), FieldValidators.N, Formatters.Bcd, null);
}
Then change your formatter declaration to provide the unpacked length as the paramter.
Bit._003_PROC_CODE, FieldDescriptor.BcdFixed(6)},
Again, all this may have been unnecessary because I didn't know something about the existing code, but it is working for me.
I hope this helps.

Pinch Zoom images bound in Listbox

I am trying to implement pinch zoom in my application. I found this article (Correct Pinch-Zoom in Silverlight) and it works perfectly fine for one image. But the problem is, my images are within listbox as shown in below XAML:
<ListBox x:Name="lstImage" Margin="-20,-23,-12,32" Height="709" Width="480">
<ListBox.ItemTemplate>
<DataTemplate>
<Image Source="{Binding Path=ImageSource}" VerticalAlignment="Top" Margin="10,12,10,10" Width="640" Height="800">
</Image>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I am not able to understand how to implement that solution. Thanks in advance.
Create a class with name PinchZomBehavior.cs and add the following code.
public class PinchZomBehavior : Behavior<Image>
{
private double _totalImageScale = 1d;
private Point _imagePosition = new Point(0, 0);
private const double MaxImageZoom = 5;
private Point _oldFinger1;
private Point _oldFinger2;
private double _oldScaleFactor;
private Image _imgZoom;
protected override void OnAttached()
{
_imgZoom = AssociatedObject;
_imgZoom.RenderTransform = new CompositeTransform { ScaleX = 1, ScaleY = 1, TranslateX = 0, TranslateY = 0 };
var listener = GestureService.GetGestureListener(AssociatedObject);
listener.PinchStarted += OnPinchStarted;
listener.PinchDelta += OnPinchDelta;
listener.DragDelta += OnDragDelta;
listener.DoubleTap += OnDoubleTap;
base.OnAttached();
}
#region Pinch and Zoom Logic
#region Event handlers
/// <summary>
/// Initializes the zooming operation
/// </summary>
private void OnPinchStarted(object sender, PinchStartedGestureEventArgs e)
{
_oldFinger1 = e.GetPosition(_imgZoom, 0);
_oldFinger2 = e.GetPosition(_imgZoom, 1);
_oldScaleFactor = 1;
}
/// <summary>
/// Computes the scaling and translation to correctly zoom around your fingers.
/// </summary>
private void OnPinchDelta(object sender, PinchGestureEventArgs e)
{
var scaleFactor = e.DistanceRatio / _oldScaleFactor;
if (!IsScaleValid(scaleFactor))
return;
var currentFinger1 = e.GetPosition(_imgZoom, 0);
var currentFinger2 = e.GetPosition(_imgZoom, 1);
var translationDelta = GetTranslationDelta(
currentFinger1,
currentFinger2,
_oldFinger1,
_oldFinger2,
_imagePosition,
scaleFactor);
_oldFinger1 = currentFinger1;
_oldFinger2 = currentFinger2;
_oldScaleFactor = e.DistanceRatio;
UpdateImageScale(scaleFactor);
UpdateImagePosition(translationDelta);
}
/// <summary>
/// Moves the image around following your finger.
/// </summary>
private void OnDragDelta(object sender, DragDeltaGestureEventArgs e)
{
var translationDelta = new Point(e.HorizontalChange, e.VerticalChange);
if (IsDragValid(1, translationDelta))
UpdateImagePosition(translationDelta);
}
/// <summary>
/// Resets the image scaling and position
/// </summary>
private void OnDoubleTap(object sender, GestureEventArgs e)
{
ResetImagePosition();
}
#endregion
#region Utils
/// <summary>
/// Computes the translation needed to keep the image centered between your fingers.
/// </summary>
private Point GetTranslationDelta(
Point currentFinger1, Point currentFinger2,
Point oldFinger1, Point oldFinger2,
Point currentPosition, double scaleFactor)
{
var newPos1 = new Point(
currentFinger1.X + (currentPosition.X - oldFinger1.X) * scaleFactor,
currentFinger1.Y + (currentPosition.Y - oldFinger1.Y) * scaleFactor);
var newPos2 = new Point(
currentFinger2.X + (currentPosition.X - oldFinger2.X) * scaleFactor,
currentFinger2.Y + (currentPosition.Y - oldFinger2.Y) * scaleFactor);
var newPos = new Point(
(newPos1.X + newPos2.X) / 2,
(newPos1.Y + newPos2.Y) / 2);
return new Point(
newPos.X - currentPosition.X,
newPos.Y - currentPosition.Y);
}
/// <summary>
/// Updates the scaling factor by multiplying the delta.
/// </summary>
private void UpdateImageScale(double scaleFactor)
{
_totalImageScale *= scaleFactor;
ApplyScale();
}
/// <summary>
/// Applies the computed scale to the image control.
/// </summary>
private void ApplyScale()
{
((CompositeTransform)_imgZoom.RenderTransform).ScaleX = _totalImageScale;
((CompositeTransform)_imgZoom.RenderTransform).ScaleY = _totalImageScale;
}
/// <summary>
/// Updates the image position by applying the delta.
/// Checks that the image does not leave empty space around its edges.
/// </summary>
private void UpdateImagePosition(Point delta)
{
var newPosition = new Point(_imagePosition.X + delta.X, _imagePosition.Y + delta.Y);
if (newPosition.X > 0) newPosition.X = 0;
if (newPosition.Y > 0) newPosition.Y = 0;
if ((_imgZoom.ActualWidth * _totalImageScale) + newPosition.X < _imgZoom.ActualWidth)
newPosition.X = _imgZoom.ActualWidth - (_imgZoom.ActualWidth * _totalImageScale);
if ((_imgZoom.ActualHeight * _totalImageScale) + newPosition.Y < _imgZoom.ActualHeight)
newPosition.Y = _imgZoom.ActualHeight - (_imgZoom.ActualHeight * _totalImageScale);
_imagePosition = newPosition;
ApplyPosition();
}
/// <summary>
/// Applies the computed position to the image control.
/// </summary>
private void ApplyPosition()
{
((CompositeTransform)_imgZoom.RenderTransform).TranslateX = _imagePosition.X;
((CompositeTransform)_imgZoom.RenderTransform).TranslateY = _imagePosition.Y;
}
/// <summary>
/// Resets the zoom to its original scale and position
/// </summary>
private void ResetImagePosition()
{
_totalImageScale = 1;
_imagePosition = new Point(0, 0);
ApplyScale();
ApplyPosition();
}
/// <summary>
/// Checks that dragging by the given amount won't result in empty space around the image
/// </summary>
private bool IsDragValid(double scaleDelta, Point translateDelta)
{
if (_imagePosition.X + translateDelta.X > 0 || _imagePosition.Y + translateDelta.Y > 0)
return false;
if ((_imgZoom.ActualWidth * _totalImageScale * scaleDelta) + (_imagePosition.X + translateDelta.X) < _imgZoom.ActualWidth)
return false;
if ((_imgZoom.ActualHeight * _totalImageScale * scaleDelta) + (_imagePosition.Y + translateDelta.Y) < _imgZoom.ActualHeight)
return false;
return true;
}
/// <summary>
/// Tells if the scaling is inside the desired range
/// </summary>
private bool IsScaleValid(double scaleDelta)
{
return (_totalImageScale * scaleDelta >= 1) && (_totalImageScale * scaleDelta <= MaxImageZoom);
}
#endregion
#endregion
}
And attach the behavior to image control like this
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
<Image Stretch="Uniform" Source="{Binding Image}" CacheMode="BitmapCache">
<i:Interaction.Behaviors>
<Behaviors:PinchZomBehavior/>
</i:Interaction.Behaviors>
</Image>

Blackberry - change latitude and longitude on the device to test app

I want to test my app on the device. Is it possible to hard code the latitude and longitude values somewhere in the device settings so the app reads those instead of the current location?
I want to test my app for different locations other than my current location.
In the BB simulator you can go to Simulate > GPS Location. Click the Add button and enter in a name, latitude and longitude. Click save and the simulator will start feeding your new location to the apps. Note that whatever location is displayed in the drop down is the one that will be reported by the simulator.
Inside GPS mockup
If you have access to your application code, you can always create a mockup implementation for LocationProvider so it will read location and speed data from file or RecordStore and return it as a Location, something like
public class MockupLocationProvider extends LocationProvider {
public MockupLocationProvider() {
//prepare a file or RecordStore with locations here
}
public Location getLocation(int arg0) throws LocationException,
InterruptedException {
//read data from file or RecordStore
double latitude = 321;
double longitude = 34;
float altitude = 21;
//create and return location
Location result = new GPSLocation(latitude,
longitude, altitude);
return result;
}
public int getState() {
// mockup location provider always available
return LocationProvider.AVAILABLE;
}
public void reset() {
// your code
}
public void setLocationListener(LocationListener listener,
int interval, int timeout, int maxAge) {
// your code
}
}
and mockup for your Location
public class GPSLocation extends Location {
double _latitude, _longitude;
float _altitude, _horAcc = 0, _verAcc = 0, _speed;
public GPSLocation(double lat, double lon, float alt) {
init(lat, lon, alt);
}
public GPSLocation(double lat, double lon, float alt, float spd) {
init(lat, lon, alt);
_speed = spd;
}
private void init(double lat, double lon, float alt) {
_latitude = lat;
_longitude = lon;
_altitude = alt;
}
public QualifiedCoordinates getQualifiedCoordinates() {
QualifiedCoordinates c = new QualifiedCoordinates(_latitude,
_longitude, _altitude, _horAcc, _verAcc);
return c;
}
public float getSpeed() {
return _speed;
}
public String toString() {
String result = "Lat:" + String.valueOf(_latitude) + "|Lon:"
+ String.valueOf(_longitude) + "|Alt:"
+ String.valueOf(_altitude);
return result;
}
}
Then somewhere on the screen
MockupLocationProvider gpsProvider = new MockupLocationProvider();
GPSLocation loc = (GPSLocation)gpsProvider.getLocation(0);
add(new RichTextField(loc.toString()));
Outside GPS mockup
Another option is to generally mockup GPS signals.
Steps are:
configure device gps receiver for
bluetooth (for ex.)
setup some
opensource gps server on your desktop
to produce location data over
bluetooth
change configuration/code
of gps server to mockup location data
Other options
There is a possibility to uncontrolled change of location gps data by shielding gps receiver with some radio-material (like alluminium foil or so) :)

Resources