The method in iOS to locate the current position coordinates and convert them to Mars coordinates
- 2020-05-14 04:58:21
- OfStack
Location and location information acquisition
Location and reverse lookup location information to load two dynamic libraries CoreLocation.framework and MapKit.framework 1 get coordinates 1 provide reverse lookup
// appDelgate.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,CLLocationManagerDelegate,MKReverseGeocoderDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
#import "AppDelegate.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
button.frame = CGRectMake(0, 100, 100, 30);
[button setTitle:@" positioning " forState:UIControlStateNormal];
[button addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 150, 320, 30)];
label.tag = 101;
label.text = @" Pending location ....";
[self.window addSubview:label];
[label release];
[self.window addSubview:button];
return YES;
}
-(void) test {
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
// Set the positioning accuracy, 10 Meters, 100 meters, best
[locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
locationManager.delegate = self;
// Start constant positioning
[locationManager startUpdatingLocation];
}
// The error message
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"error");
}
// 6.0 Call this function above
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
NSLog(@"%d", [locations count]);
CLLocation *newLocation = locations[0];
CLLocationCoordinate2D oldCoordinate = newLocation.coordinate;
NSLog(@" Old longitude: %f, Old latitude: %f",oldCoordinate.longitude,oldCoordinate.latitude);
// CLLocation *newLocation = locations[1];
// CLLocationCoordinate2D newCoordinate = newLocation.coordinate;
// NSLog(@" Longitude: %f, Latitude: %f",newCoordinate.longitude,newCoordinate.latitude);
// Calculate the distance between two coordinates
// float distance = [newLocation distanceFromLocation:oldLocation];
// NSLog(@"%f",distance);
[manager stopUpdatingLocation];
//------------------ Position anticoding ---5.0 After using -----------------
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:newLocation
completionHandler:^(NSArray *placemarks, NSError *error){
for (CLPlacemark *place in placemarks) {
UILabel *label = (UILabel *)[self.window viewWithTag:101];
label.text = place.name;
NSLog(@"name,%@",place.name); // Location name
// NSLog(@"thoroughfare,%@",place.thoroughfare); // The street
// NSLog(@"subThoroughfare,%@",place.subThoroughfare); // The child street
// NSLog(@"locality,%@",place.locality); // The city
// NSLog(@"subLocality,%@",place.subLocality); // area
// NSLog(@"country,%@",place.country); // countries
}
}];
}
// 6.0 Call this function
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(@"%@", @"ok");
}
@end
Convert to Mars coordinates
The written public class is called :GPScombineClass, which mainly shows the location of GPS position, the acquisition of GPS coordinates, and then the transformation from mobile phone coordinates to Mars coordinates, and then, if necessary, from Mars to baidu, baidu to Mars detailed algorithm;
In GPScombineClass. h
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import "CSqlite.h"
#import <MapKit/MapKit.h>
@interface GPScombineClass : NSObject<MKMapViewDelegate>{
CLLocationManager *locationManager;
CSqlite *m_sqlite;
UILabel *m_locationName;
MKMapView *mainMapView;
@public CLLocationCoordinate2D baidulocation;
CLLocationCoordinate2D deleeverLocation;
}
-(void)OpenGPSmapView;
// Put your position on the map -- An external interface
-(void)setMyMapPonitByMKMapView:(MKMapView *)MyMap;
@end
@interface POI : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *subtitle;
NSString *title;
}
@property (nonatomic,readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic,retain) NSString *subtitle;
@property (nonatomic,retain) NSString *title;
-(id) initWithCoords:(CLLocationCoordinate2D) coords;
@end
In GPScombineClass. m
#import "GPScombineClass.h"
const double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
@implementation GPScombineClass
-(void)OpenGPSmapView{
m_sqlite = [[CSqlite alloc]init];
[m_sqlite openSqlite];
if ([CLLocationManager locationServicesEnabled]) { // Check if the location service is available
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter=0.5;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation]; // To locate
}
NSLog(@"GPS Start the ");
}
// Called when the location is successful
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
CLLocationCoordinate2D mylocation = newLocation.coordinate;// Mobile phone GPS
mylocation = [self zzTransGPS:mylocation];/// Convert to Mars GPS
deleeverLocation=mylocation;
baidulocation=[self hhTrans_bdGPS:mylocation];// Convert to baidu map
/*
// Display Mars coordinates
[self SetMapPoint:mylocation MKMapView:mainMapView];
///////// Get location information
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray* placemarks,NSError *error)
{
if (placemarks.count >0 )
{
CLPlacemark * plmark = [placemarks objectAtIndex:0];
NSString * country = plmark.country;
NSString * city = plmark.locality;
NSLog(@"%@-%@-%@",country,city,plmark.name);
self->m_locationName.text =plmark.name;
NSLog(@"%@",self->m_locationName);
}
NSLog(@"%@",placemarks);
}];
//[geocoder release];
*/
}
// Called when the location fails
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
NSLog(@" To locate failure ");
}
// The mobile phone GPS Convert coordinates to Mars coordinates ( google Coordinates)
-(CLLocationCoordinate2D)zzTransGPS:(CLLocationCoordinate2D)yGps
{
int TenLat=0;
int TenLog=0;
TenLat = (int)(yGps.latitude*10);
TenLog = (int)(yGps.longitude*10);
NSString *sql = [[NSString alloc]initWithFormat:@"select offLat,offLog from gpsT where lat=%d and log = %d",TenLat,TenLog];
NSLog(sql);
sqlite3_stmt* stmtL = [m_sqlite NSRunSql:sql];
int offLat=0;
int offLog=0;
while (sqlite3_step(stmtL)==SQLITE_ROW)
{
offLat = sqlite3_column_int(stmtL, 0);
offLog = sqlite3_column_int(stmtL, 1);
}
yGps.latitude = yGps.latitude+offLat*0.0001;
yGps.longitude = yGps.longitude + offLog*0.0001;
return yGps;
}
// Put your position on the map -- An external interface
-(void)setMyMapPonitByMKMapView:(MKMapView *)MyMap{
// Display Mars coordinates
[self SetMapPoint:deleeverLocation MKMapView:MyMap];
MyMap=mainMapView;
}
// Put your position on the map
-(void)SetMapPoint:(CLLocationCoordinate2D)myLocation MKMapView:(MKMapView *)mapView
{
// POI* m_poi = [[POI alloc]initWithCoords:myLocation];
//
// [mapView addAnnotation:m_poi];
MKCoordinateRegion theRegion = { {0.0, 0.0 }, { 0.0, 0.0 } };
theRegion.center=myLocation;
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
theRegion.span.longitudeDelta = 0.01f;
theRegion.span.latitudeDelta = 0.01f;
[mapView setRegion:theRegion animated:YES];
}
// Convert the Mars coordinates to baidu coordinates
-(CLLocationCoordinate2D)hhTrans_bdGPS:(CLLocationCoordinate2D)fireGps
{
CLLocationCoordinate2D bdGps;
double huo_x=fireGps.longitude;
double huo_y=fireGps.latitude;
double z = sqrt(huo_x * huo_x + huo_y * huo_y) + 0.00002 * sin(huo_y * x_pi);
double theta = atan2(huo_y, huo_x) + 0.000003 * cos(huo_x * x_pi);
bdGps.longitude = z * cos(theta) + 0.0065;
bdGps.latitude = z * sin(theta) + 0.006;
return bdGps;
}
#pragma mark Display product information
#pragma mark
-(void)showPurchaseOnMapByLocation:(CLLocationCoordinate2D)baiduGPS MKMapView:(MKMapView*)myMapView{
CLLocationCoordinate2D googleGPS;
googleGPS=[self hhTrans_GCGPS:baiduGPS];// Convert to baidu
[self SetPurchaseMapPoint:googleGPS MKMapView:myMapView];
}
// Convert baidu map into Google map -- Mars coordinates
-(CLLocationCoordinate2D)hhTrans_GCGPS:(CLLocationCoordinate2D)baiduGps
{
CLLocationCoordinate2D googleGps;
double bd_x=baiduGps.longitude - 0.0065;
double bd_y=baiduGps.latitude - 0.006;
double z = sqrt(bd_x * bd_x + bd_y * bd_y) - 0.00002 * sin(bd_y * x_pi);
double theta = atan2(bd_y, bd_x) - 0.000003 * cos(bd_x * x_pi);
googleGps.longitude = z * cos(theta);
googleGps.latitude = z * sin(theta);
return googleGps;
}
-(void)SetPurchaseMapPoint:(CLLocationCoordinate2D)myLocation MKMapView:(MKMapView *)mapView
{
POI* m_poi = [[POI alloc]initWithCoords:myLocation];
[mapView addAnnotation:m_poi];
MKCoordinateRegion theRegion = { {0.0, 0.0 }, { 0.0, 0.0 } };
theRegion.center=myLocation;
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
theRegion.span.longitudeDelta = 0.01f;
theRegion.span.latitudeDelta = 0.01f;
[mapView setRegion:theRegion animated:YES];}
@end