Iphone development: Map Tracing

Source: Internet
Author: User

The MapKit in iOS integrates many functions of the google map api and the positioning function of iOS, so that we can draw your running track to the map. This function is very useful, such as gprs tracking, personnel tracking, express tracking, and so on. In this article, we will use the positioning function of Map Kit and iOS to draw your running track on the Map.
Implementation
In a previous article: iOS developer's location display on google map describes how to display its location on the map. If we save these locations first, then draw the map in tandem, which is our running track.
First, let's look at how to draw a curve on a map. A class named MKPolyline is provided in Map Kit. We can use it to draw curves. Let's take a look at a simple example.
Use the following code to read the longitude and latitude from a file and create a path: MKPolyline instance.

-(Void) loadRoute
{
NSString * filePath = [[NSBundle mainBundle] pathForResource: @ "route" ofType: @ "csv"];
NSString * fileContents = [NSString stringWithContentsOfFile: filePath encoding: NSUTF8StringEncoding error: nil];
NSArray * pointStrings = [fileContents componentsSeparatedByCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet];

// While we create the route points, we will also be calculating the bounding box of our route
// So we can easily zoom in on it.
MKMapPoint northEastPoint;
MKMapPoint southWestPoint;

// Create a c array of points.
MKMapPoint * pointArr = malloc (sizeof (CLLocationCoordinate2D) * pointStrings. count );

For (int idx = 0; idx <pointStrings. count; idx ++)
{
// Break the string down even further to latitude and long1_fields.
NSString * currentPointString = [pointStrings objectAtIndex: idx];
NSArray * latLonArr = [currentPointString componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString: @ ","];

CLLocationDegrees latitude = [[latLonArr objectAtIndex: 0] doubleValue];
CLLocationDegrees longpolling = [[latLonArr objectAtIndex: 1] doubleValue];

// Create our coordinate and add it to the correct spot in the array
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake (latitude, longpolling );

MKMapPoint point = MKMapPointForCoordinate (coordinate );

//
// Adjust the bounding box
//

// If it is the first point, just use them, since we have nothing to compare to yet.
If (idx = 0 ){
NorthEastPoint = point;
SouthWestPoint = point;
}
Else
{
If (point. x> northEastPoint. x)
NorthEastPoint. x = point. x;
If (point. y> northEastPoint. y)
NorthEastPoint. y = point. y;
If (point. x <southWestPoint. x)
SouthWestPoint. x = point. x;
If (point. y <southWestPoint. y)
SouthWestPoint. y = point. y;
}

PointArr [idx] = point;

}

// Create the polyline based on the array of points.
Self. routeLine = [MKPolyline polylineWithPoints: pointArr count: pointStrings. count];

_ RouteRect = MKMapRectMake (southWestPoint. x, southWestPoint. y, northEastPoint. x-southWestPoint. x, northEastPoint. y-southWestPoint. y );

// Clear the memory allocated earlier for the points
Free (pointArr );

}

Add this path MKPolyline object to the map
[Self. mapView addOverlay: self. routeLine];
Displayed on the map:

-(MKOverlayView *) mapView :( MKMapView *) mapView viewForOverlay :( id) overlay
{
MKOverlayView * overlayView = nil;

If (overlay = self. routeLine)
{
// If we have not yet created an overlay view for this overlay, create it now.
If (nil = self. routeLineView)
{
Self. routeLineView = [[[MKPolylineView alloc] initWithPolyline: self. routeLine] autoreleline];
Self. routeLineView. fillColor = [UIColor redColor];
Self. routeLineView. strokeColor = [UIColor redColor];
Self. routeLineView. lineWidth = 3;
}

OverlayView = self. routeLineView;

}

Return overlayView;

}

Let's take a look at the effect of the track path drawn by reading data from the file:

Then we can change the method of reading the location from the file to obtaining the current location using gprs and other methods.
Step 1: Create a CLLocationManager instance
Step 2: Set the CLLocationManager instance delegation and precision
Step 3: Set the distance filter distanceFilter
Step 4: Start the request www.2cto.com
The Code is as follows:

-(Void) viewDidLoad {
[Super viewDidLoad];

NoUpdates = 0;
Locations = [[NSMutableArray alloc] init];

LocationMgr = [[CLLocationManager alloc] init];
LocationMgr. delegate = self;
LocationMgr. desiredAccuracy = kCLLocationAccuracyBest;
LocationMgr. distanceFilter = 1.0f;
[LocationMgr startUpdatingLocation];


}

The code above defines an array to save the longitude and latitude of the running track.
Every time we update the current location, we put the longitude and latitude of the current location in this array and re-draw the path. The Code is as follows:

-(Void) locationManager :( CLLocationManager *) manager
DidUpdateToLocation :( CLLocation *) newLocation
FromLocation :( CLLocation *) oldLocation {
NoUpdates ++;
 
[Locations addObject: [NSString stringWithFormat: @ "% f, % f", [newLocation coordinate]. latitude, [newLocation coordinate]. longpolling];
 
[Self updateLocation];
If (self. routeLine! = Nil ){
Self. routeLine = nil;
}
If (self. routeLine! = Nil)
[Self. mapView removeOverlay: self. routeLine];
Self. routeLine = nil;
// Create the overlay
[Self loadRoute];

// Add the overlay to the map
If (nil! = Self. routeLine ){
[Self. mapView addOverlay: self. routeLine];
}

// Zoom in on the route.
[Self zoomInOnRoute];

}

We can modify the code used to obtain the trajectory from the file and create the trajectory from the latitude and longitude to take the value from this array:

// Creates the route (MKPolyline) overlay
-(Void) loadRoute
{
 

// While we create the route points, we will also be calculating the bounding box of our route
// So we can easily zoom in on it.
MKMapPoint northEastPoint;
MKMapPoint southWestPoint;

// Create a c array of points.
MKMapPoint * pointArr = malloc (sizeof (CLLocationCoordinate2D) * locations. count );
For (int idx = 0; idx <locations. count; idx ++)
{
// Break the string down even further to latitude and long1_fields.
NSString * currentPointString = [locations objectAtIndex: idx];
NSArray * latLonArr = [currentPointString componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString: @ ","];

CLLocationDegrees latitude = [[latLonArr objectAtIndex: 0] doubleValue];
CLLocationDegrees longpolling = [[latLonArr objectAtIndex: 1] doubleValue];

CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake (latitude, longpolling );

MKMapPoint point = MKMapPointForCoordinate (coordinate );


If (idx = 0 ){
NorthEastPoint = point;
SouthWestPoint = point;
}
Else
{
If (point. x> northEastPoint. x)
NorthEastPoint. x = point. x;
If (point. y> northEastPoint. y)
NorthEastPoint. y = point. y;
If (point. x <southWestPoint. x)
SouthWestPoint. x = point. x;
If (point. y <southWestPoint. y)
SouthWestPoint. y = point. y;
}

PointArr [idx] = point;

}

Self. routeLine = [MKPolyline polylineWithPoints: pointArr count: locations. count];

_ RouteRect = MKMapRectMake (southWestPoint. x, southWestPoint. y, northEastPoint. x-southWestPoint. x, northEastPoint. y-southWestPoint. y );

Free (pointArr );

}

In this way, we can draw a google Map of our running trajectory.

 


From cloud huaikong-abel

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.