forked from organicmaps/organicmaps
[iOS] Some location routine simplification - factor out common functions.
This commit is contained in:
parent
30fd3b9805
commit
b0733ba8e8
3 changed files with 107 additions and 50 deletions
|
@ -97,17 +97,6 @@ static void OnSearchResultCallback(search::Results const & res, int queryId)
|
|||
@implementation SearchVC
|
||||
|
||||
|
||||
+ (BOOL)isLocationValid:(CLLocation *)l
|
||||
{
|
||||
if (l == nil) return false;
|
||||
|
||||
// do not use too old locations
|
||||
static NSTimeInterval const SECONDS_TO_EXPIRE = 300.0;
|
||||
|
||||
// timeIntervalSinceNow returns negative value - because of "since now"
|
||||
return [l.timestamp timeIntervalSinceNow] > (-SECONDS_TO_EXPIRE);
|
||||
}
|
||||
|
||||
- (id)initWithFramework:(Framework *)framework andLocationManager:(LocationManager *)lm
|
||||
{
|
||||
if ((self = [super initWithNibName:nil bundle:nil]))
|
||||
|
@ -115,17 +104,8 @@ static void OnSearchResultCallback(search::Results const & res, int queryId)
|
|||
m_framework = framework;
|
||||
m_locationManager = lm;
|
||||
|
||||
// get current location
|
||||
CLLocation * l = m_locationManager.lastLocation;
|
||||
|
||||
double lat, lon;
|
||||
bool const hasPt = [SearchVC isLocationValid:l];
|
||||
if (hasPt)
|
||||
{
|
||||
lat = l.coordinate.latitude;
|
||||
lon = l.coordinate.longitude;
|
||||
}
|
||||
|
||||
bool const hasPt = [m_locationManager getLat:lat Lon:lon];
|
||||
m_framework->PrepareSearch(hasPt, lat, lon);
|
||||
}
|
||||
return self;
|
||||
|
@ -149,11 +129,10 @@ static void OnSearchResultCallback(search::Results const & res, int queryId)
|
|||
params.m_callback = bind(&OnSearchResultCallback, _1, g_queryId);
|
||||
// Set current keyboard input mode
|
||||
params.SetInputLanguage([[UITextInputMode currentInputMode].primaryLanguage UTF8String]);
|
||||
|
||||
CLLocation * l = m_locationManager.lastLocation;
|
||||
|
||||
if ([SearchVC isLocationValid:l])
|
||||
params.SetPosition(l.coordinate.latitude, l.coordinate.longitude);
|
||||
|
||||
double lat, lon;
|
||||
if ([m_locationManager getLat:lat Lon:lon])
|
||||
params.SetPosition(lat, lon);
|
||||
|
||||
params.SetNearMeMode(false);
|
||||
}
|
||||
|
@ -469,20 +448,17 @@ static void OnSearchResultCallback(search::Results const & res, int queryId)
|
|||
}
|
||||
else
|
||||
{
|
||||
CLLocation * loc = [m_locationManager lastLocation];
|
||||
CLHeading * heading = [m_locationManager lastHeading];
|
||||
if (loc == nil || heading == nil)
|
||||
double lat, lon, northR;
|
||||
if ([m_locationManager getLat:lat Lon:lon] && [m_locationManager getNorthRad:northR])
|
||||
{
|
||||
compass.showArrow = NO;
|
||||
}
|
||||
else
|
||||
{
|
||||
double const northDeg = (heading.trueHeading < 0) ? heading.magneticHeading : heading.trueHeading;
|
||||
m2::PointD const center = r.GetFeatureCenter();
|
||||
compass.angle = ang::AngleTo(m2::PointD(MercatorBounds::LonToX(loc.coordinate.longitude),
|
||||
MercatorBounds::LatToY(loc.coordinate.latitude)), center) + northDeg / 180. * math::pi;
|
||||
compass.angle = ang::AngleTo(m2::PointD(MercatorBounds::LonToX(lon),
|
||||
MercatorBounds::LatToY(lat)), center) +
|
||||
northR;
|
||||
compass.showArrow = YES;
|
||||
}
|
||||
else
|
||||
compass.showArrow = NO;
|
||||
}
|
||||
}
|
||||
return cell;
|
||||
|
@ -576,8 +552,8 @@ static void OnSearchResultCallback(search::Results const & res, int queryId)
|
|||
|
||||
- (void)onCompassUpdate:(location::CompassInfo const &)info
|
||||
{
|
||||
CLLocation * loc = m_locationManager.lastLocation;
|
||||
if (loc == nil)
|
||||
double lat, lon;
|
||||
if (![m_locationManager getLat:lat Lon:lon])
|
||||
return;
|
||||
|
||||
double const northDeg = (info.m_trueHeading < 0) ? info.m_magneticHeading : info.m_trueHeading;
|
||||
|
@ -602,8 +578,9 @@ static void OnSearchResultCallback(search::Results const & res, int queryId)
|
|||
{
|
||||
CompassView * compass = (CompassView *)cell.accessoryView;
|
||||
m2::PointD const center = res.GetFeatureCenter();
|
||||
compass.angle = ang::AngleTo(m2::PointD(MercatorBounds::LonToX(loc.coordinate.longitude),
|
||||
MercatorBounds::LatToY(loc.coordinate.latitude)), center) + northDeg / 180. * math::pi;
|
||||
compass.angle = ang::AngleTo(m2::PointD(MercatorBounds::LonToX(lon),
|
||||
MercatorBounds::LatToY(lat)), center) +
|
||||
northDeg / 180. * math::pi;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,4 +26,7 @@
|
|||
- (CLHeading *)lastHeading;
|
||||
// Fixes compass angle orientation when rotating screen to landscape
|
||||
- (void)setOrientation:(UIInterfaceOrientation)orientation;
|
||||
|
||||
- (bool)getLat:(double &)lat Lon:(double &)lon;
|
||||
- (bool)getNorthRad:(double &)rad;
|
||||
@end
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
#import "LocationManager.h"
|
||||
|
||||
#import "../../base/math.hpp" // for math::pi constant
|
||||
|
||||
|
||||
@implementation LocationManager
|
||||
|
||||
- (id)init
|
||||
|
@ -70,7 +73,8 @@
|
|||
if (m_isStarted)
|
||||
{
|
||||
if ([m_observers count] == 0)
|
||||
{ // stop only if no more observers are subsribed
|
||||
{
|
||||
// stop only if no more observers are subsribed
|
||||
m_isStarted = NO;
|
||||
m_reportFirstUpdate = YES;
|
||||
if ([CLLocationManager headingAvailable])
|
||||
|
@ -98,16 +102,17 @@
|
|||
info.m_longitude = location.coordinate.longitude;
|
||||
info.m_timestamp = [location.timestamp timeIntervalSince1970];
|
||||
info.m_source = location::EAppleNative;
|
||||
// info.m_verticalAccuracy = location.verticalAccuracy;
|
||||
// info.m_altitude = location.altitude;
|
||||
// info.m_course = location.course;
|
||||
// info.m_speed = location.speed;
|
||||
|
||||
//info.m_verticalAccuracy = location.verticalAccuracy;
|
||||
//info.m_altitude = location.altitude;
|
||||
//info.m_course = location.course;
|
||||
//info.m_speed = location.speed;
|
||||
}
|
||||
|
||||
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
|
||||
{
|
||||
if (location::IsLatValid(newLocation.coordinate.latitude)
|
||||
&& location::IsLonValid(newLocation.coordinate.longitude))
|
||||
if (location::IsLatValid(newLocation.coordinate.latitude) &&
|
||||
location::IsLonValid(newLocation.coordinate.longitude))
|
||||
{
|
||||
if (m_reportFirstUpdate)
|
||||
{
|
||||
|
@ -129,9 +134,9 @@
|
|||
newInfo.m_magneticHeading = newHeading.magneticHeading;
|
||||
newInfo.m_trueHeading = newHeading.trueHeading;
|
||||
newInfo.m_accuracy = newHeading.headingAccuracy;
|
||||
// newInfo.m_x = newHeading.x;
|
||||
// newInfo.m_y = newHeading.y;
|
||||
// newInfo.m_z = newHeading.z;
|
||||
//newInfo.m_x = newHeading.x;
|
||||
//newInfo.m_y = newHeading.y;
|
||||
//newInfo.m_z = newHeading.z;
|
||||
newInfo.m_timestamp = [newHeading.timestamp timeIntervalSince1970];
|
||||
for (id observer in m_observers)
|
||||
[observer onCompassUpdate:newInfo];
|
||||
|
@ -170,4 +175,76 @@
|
|||
m_locationManager.headingOrientation = (CLDeviceOrientation)orientation;
|
||||
}
|
||||
|
||||
- (bool)getLat:(double &)lat Lon:(double &)lon
|
||||
{
|
||||
CLLocation * l = [self lastLocation];
|
||||
|
||||
static NSTimeInterval const SECONDS_TO_EXPIRE = 300.0;
|
||||
|
||||
// timeIntervalSinceNow returns negative value - because of "since now"
|
||||
if ((l != nil) && ([l.timestamp timeIntervalSinceNow] > (-SECONDS_TO_EXPIRE)))
|
||||
{
|
||||
lat = l.coordinate.latitude;
|
||||
lon = l.coordinate.longitude;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
// New York
|
||||
//lat = 40.7306;
|
||||
//lon = -73.9866;
|
||||
|
||||
// San Francisco
|
||||
//lat = 37.779;
|
||||
//lon = -122.4192;
|
||||
|
||||
// Paris
|
||||
//lat = 48.858261;
|
||||
//lon = 2.294499;
|
||||
|
||||
// Rome
|
||||
//lat = 41.8933;
|
||||
//lon = 12.4831;
|
||||
|
||||
// Moscow
|
||||
//lat = 55.7516;
|
||||
//lon = 37.6187;
|
||||
|
||||
// Madrid
|
||||
//lat = 40.4167;
|
||||
//lon = -3.7036;
|
||||
|
||||
// Barselona
|
||||
//lat = 41.3857;
|
||||
//lon = 2.1702;
|
||||
|
||||
// Berlin
|
||||
//lat = 52.517;
|
||||
//lon = 13.3889;
|
||||
|
||||
// Minsk
|
||||
//lat = 53.9023;
|
||||
//lon = 27.5619;
|
||||
|
||||
//return true;
|
||||
}
|
||||
|
||||
- (bool)getNorthRad:(double &)rad
|
||||
{
|
||||
CLHeading * h = [self lastHeading];
|
||||
|
||||
if (h != nil)
|
||||
{
|
||||
rad = (h.trueHeading < 0) ? h.magneticHeading : h.trueHeading;
|
||||
rad = rad / 180.0 * math::pi;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
//rad = 0.0;
|
||||
//return true;
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
Loading…
Add table
Reference in a new issue