The public class written here is called:GPScombineClass mainly displays GPS location positioning, acquisition of GPS coordinates, and then converting the coordinates from the mobile phone to the Mars coordinates, and then converting from Mars to Baidu as needed, detailed Baidu-to-Mars 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;
// Place your location on the map-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 whether the locating service is available
LocationManager = [[CLLocationManager alloc] init];
LocationManager. delegate = self;
Location manager. distanceFilter = 0.5;
LocationManager. desiredAccuracy = kCLLocationAccuracyBest;
[LocationManager startUpdatingLocation]; // start locating
}
NSLog (@ "GPS start ");
}
// Called when positioning is successful
-(Void) locationManager :( CLLocationManager *) manager
DidUpdateToLocation :( CLLocation *) newLocation
FromLocation :( CLLocation *) oldLocation
{
CLLocationCoordinate2D mylocation = newLocation. coordinate; // mobile GPS
Mylocation = [self zzTransGPS: mylocation]; // convert to Mars GPS
DeleeverLocation = mylocation;
Baidulocation = [self hhTrans_bdGPS: mylocation]; // convert to a hundred-degree Map
/*
// Display the coordinates of Mars
[Self SetMapPoint: mylocation MKMapView: mainMapView];
//// // Obtain the 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];
*/
}
// Call when locating failure
-(Void) locationManager :( CLLocationManager *) manager
DidFailWithError :( NSError *) error {
NSLog (@ "failed to locate ");
}
// Convert the mobile phone GPS coordinates to the Mars coordinates (google coordinates)
-(CLLocationCoordinate2D) zzTransGPS :( CLLocationCoordinate2D) yGps
{
Int TenLat = 0;
Int TenLog = 0;
TenLat = (int) (yGps. latitude * 10 );
TenLog = (int) (yGps. longbench * 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 nsunsql: 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. longbench = yGps. longbench + offLog * 0.0001;
Return yGps;
}
// Place your location on the map-external interface
-(Void) setMyMapPonitByMKMapView :( MKMapView *) MyMap {
// Display the coordinates of Mars
[Self SetMapPoint: deleeverLocation MKMapView: MyMap];
MyMap = mainMapView;
}
// Place your location 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 coordinates of Mars to a hundred-degree Coordinate
-(CLLocationCoordinate2D) hhTrans_bdGPS :( CLLocationCoordinate2D) fireGps
{
CLLocationCoordinate2D bdGps;
Double huo_x = fireGps. longbench;
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. longpolling = z * cos (theta) + 0.0065;
BdGps. latitude = z * sin (theta) + 0.006;
Return bdGps;
}
# Pragma mark displays 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 to Google map-Mars Coordinate
-(CLLocationCoordinate2D) hhTrans_GCGPS :( CLLocationCoordinate2D) baiduGps
{
CLLocationCoordinate2D googleGps;
Double bd_x = baiduGps. longbench-0.0065;
Double bd_y = baiduGps. Longitude-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. longpolling = 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