More Related Content
More from Eakapong Kattiya
More from Eakapong Kattiya (8)
Iphone developer advance location based
- 2. ความรู้พื้นฐานที่จําเป็น
- Objective-C for iPhone Programming
- Basic UI iPhone Programming
- Model View Controller
- XML Parser
- Property List / SQLite
Monday, March 26, 12
- 3. เนื้อหาในหลักสูตร Location Based Application
Core Location
- CLLocationManager
CLLocationManagerDelegate
CLLocation
- CLLocationCoordinate2D
CLLocationDistance
MapKit
- MKMapView
MKMapViewDelegate
MKCoordinateRegion
MKCoordinateSpan
- MKReverseGeocoder
MKReverseGeocoderDelegate
Monday, March 26, 12
- 11. Core Location
CLLocationManager และ CLLocation
เป็น Class ที่ใช้ในการหาตําแหน่งพิกัดของเครื่อง
iPhone , iPod Touch หรือ iPad ว่าอยู่ที่ใดในโลก
(Geolocation) ซึ่งจะทําให้เราสามารถใช้ตําแหน่งนี้ใน
การเชื่อมต่อกับฐานข้อมูล เพื่อหาข้อมูลสถานที่ใกล้เคียง
หรือ เพื่อนของเราได้
โดยการหาพิกัดจะมีอยู่ 3 วิธี
1. GPS เฉพาะเครื่อง iPhone 3G ขึ้นไป
โดยเลือกจับสัญญานดาวเทียม 3 ตัวมาคํานวนหา
ตําแหน่ง ความแม่นยํา < 100 เมตร.
2. WiFi ผ่าน SkyHook ใช้ MAC Address Router ไป
ค้นหาในฐานข้อมูล ความแม่นยํา 600 - 900 เมตร.
3. เสาสัญญาณมือถือ 4-5 สถานีใกล้เคียง ผ่านฐาน
ข้อมูล Google Map ความแม่นยํา 1 กิโลเมตร.
Monday, March 26, 12
- 12. CLLocation
ใช้สําหรับจัดการเก็บข้อมูลพิกัด 2D Latitude , Longitude , หาค่าความเร็วในการเคลื่อนที่
โดยวิธีการสร้าง Class ดังนี้
[[CLLocation alloc] initWithLatitude:13.804992 longitude:100.560758];
Monday, March 26, 12
- 13. CLLocation Property
- coordinate
ใช้สําหรับเก็บ พิกัด Latitude , Longitude ซึ่งจะเป็น Property ของ CLLocation อีกที
CLLocationCoordinate2D coordinate
coordinate.latitude = 13.804992 ;
coordinate.longitude = 100.560758 ;
Monday, March 26, 12
- 14. CLLocation Property
- speed
- timestamp
ใช้สําหรับคํานวนหาค่าความเร็วในการเคลื่อนที่ระหว่าง 2 พิกัด โดยเปรียบเทียบระยะห่าง ของ
2 พิกัด และเวลา TimeStamp
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation
*)newLocation fromLocation:(CLLocation *)oldLocation
{
//simply get the speed provided by the phone from newLocation
double gpsSpeed = newLocation.speed;
// alternative manual method
if(oldLocation != nil)
{
CLLocationDistance distanceChange = [newLocation getDistanceFrom:oldLocation];
NSTimeInterval sinceLastUpdate = [newLocation.timestamp
timeIntervalSinceDate:oldLocation.timestamp];
double calculatedSpeed = distanceChange / sinceLastUpdate;
}
}
Monday, March 26, 12
- 15. CLLocationDistance
ใช้สําหรับคํานวนหาระยะห่างระหว่าง 2 พิกัด มีหน่วยเป็นเมตร โดยเปรียบเทียบระยะห่าง 2D
ของ 2 พิกัด
CLLocationDistance distanceChange = [newLocation getDistanceFrom:oldLocation];
Monday, March 26, 12
- 16. CLLocationManager
วิธีใช้งาน
1. Add Framework <Core Location>
2. #import <CoreLocation/CoreLocation.h>
#import <CoreLocation/ CLLocationManagerDelegate.h>
//สร้าง Object
3. CLLocationManager *myLocationManager =
[[CLLocationManager alloc] init ] ;
//กําหนด delegate
4. [myLocationManager setDelegate:self];
//กําหนด ระดับความแม่นยํา
5. [myLocationManager setDesiredAccuracy:kCLLocationAccuracyBest];
//เริ่มหาพิกัด
6. [myLocationManager startUpdatingLocation];
//เมื่อพบพิกัดจะส่งข้อมูลมาที่ delegate
7. didUpdateToLocation delegate
Monday, March 26, 12
- 17. CLLocationManagerDelegate
didUpdateToLocation
ถูกเรียกใช้ตอนที่ CLLocationManager ตรวจพบว่ามีการเปลี่ยนแปลงพิกัด โดยเรา
สามารถสร้าง Object CLLocation มาเก็บค่าพิกัดที่เปลี่ยนแปลงนี้ได้
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation fromLocation:
(CLLocation *)oldLocation
{
self.myLocation = newLocation ;
NSLog(@”My latitude = %f”,myLocation.coordinate.latitude);
}
Monday, March 26, 12
- 20. Basic of MapKit
1. Init Map (IB / Storyboard)
2. Set Region (พื้นที)
่
3. Add Annotation Pin (ปักหมุด)
4. Reverse Geo Coder
5. Routing
Monday, March 26, 12
- 21. 1. Init Map (IB / Storyboard)
-(void)viewDidAppear:(BOOL)animated{
! [super viewDidAppear:YES];
!
! //Show UserLocation
[myMapView setDelegate:self];
[myMapView setMapType:MKMapTypeStandard];
myMapView.showsUserLocation = YES;
}
Monday, March 26, 12
- 22. 2. Set Region (พื้นที)
่
-(void)viewDidAppear:(BOOL)animated{
! [super viewDidAppear:YES];
!
! //Show UserLocation
[myMapView setDelegate:self];
[myMapView setMapType:MKMapTypeStandard];
myMapView.showsUserLocation = YES;
//span with Kilometre
! MKCoordinateRegion *region ;
region = MKCoordinateRegionMakeWithDistance
(myMapView.coordinate, 1000, 1000);
!
! [myMapView setRegion:region animated:YES];
}
#pragma mark MKMapView Delegate
- (void)mapView:(MKMapView *)mapView
! didUpdateUserLocation:(MKUserLocation *)userLocation
{
!
! [self setNewLocationCenter:userLocation.location];
Monday, March 26, 12
- 23. 3. Add Annotation Pin (ปักหมุด)
-(void)addPin{
MyLocation *annotation = [[MyLocation alloc]
initWithName:crimeDescription
address:address
coordinate:coordinate] ;
[myMapView addAnnotation:annotation];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *identifier = @"MyLocation";
if ([annotation isKindOfClass:[MyLocation class]]) {
MKPinAnnotationView *annotationView =
(MKPinAnnotationView *)[myMapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView =
[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.image=[UIImage imageNamed:@"arrest.png"];
//here we use a nice image instead of the default pins
return annotationView;
}
return nil;
}
Monday, March 26, 12
- 24. 4. Reverse Geo Coder
CLGeocoder แปลง พิกัดเป+นชื่อสถานที่
-(void)reverseGeo{
CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error)
{
CLPlacemark *placemark = [placemarks objectAtIndex:0];
NSLog(@"Address =%@",placemark.name) ;
NSLog(@"Address =%@",placemark.administrativeArea) ;
NSLog(@"Address =%@",placemark.locality) ;
NSLog(@"Address =%@",placemark.postalCode) ;
NSLog(@"Address =%@",placemark.subLocality) ;
NSLog(@"Address =%@",placemark.subAdministrativeArea) ;
NSLog(@"Address =%@",placemark.locality) ;
NSLog(@"Address =%@",placemark.thoroughfare) ;
NSLog(@"Address =%@",placemark.subThoroughfare) ;
NSString *currentAddress = [NSString stringWithFormat:@"%@,%@,%@",placemark.name,
placemark.administrativeArea,
placemark.locality,
placemark.postalCode];
}];
}
Monday, March 26, 12
- 25. 5. Routing
-(void)showRoute{
NSString* url =
[NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%@@%f,%f&daddr=%@@%f,%f",
[currentAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
currentLocation.latitude,
currentLocation.longitude,
[place.title stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
place.coordinate.latitude,
place.coordinate.longitude];
NSLog(@"Call openURL at %@",url);
! [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
}
Monday, March 26, 12