Google地圖實現之一
在iphone中可以用core location功能來實現地理定位,並可用mapkit 框架加載google地圖。
一、 Core Location 實現定位
Core Location主要應用了GPS, 蜂窩基站三角網以及Wi_Fi(WPS)三種技術。一代iphone之後,有的把這稱之爲Assistant GPS(A_GPS),第一代iphone不具備GPS功能。想得到定點的信息,其實不難,只需要涉及到幾個類,CLLocationManager, CLLocation, CLLocationManagerdelegate協議,CLLocationCoodinate2D, CLLocationDegrees。
<一>先實例化一個CLLocationManager,同時設置委託及精確度等。
CCLocationManager *manager = [[CLLocationManager alloc] init];
[manager setDelegate: self];
[manager setDesiredAccuracy: kCLLocationAccuracyBest];
其中desiredAccuracy屬性表示精確度,有利5種選擇如下:
desiredAccuracy屬性 |
描述 |
kCLLocationAccuracyBest |
精確度最佳 |
kCLLocationAccuracynearestTenMeters |
精確度10m以內 |
kCLLocationAccuracyHundredMeters |
精確度100m以內 |
kCLLocationAccuracyKilometer |
精確度1000m以內 |
kCLLocationAccuracyThreeKilometers |
精確度3000m以內 |
NOTE:精確度越高,用點越多,就要根據實際情況而定。
manager.distanceFilter = 250;這個表示在地圖上每隔250m才更新一次定位信息。
[manager startUpdateLocation]; 啓動定位器,如果不用的時候就必須調用stopUpdateLocation以關閉定位功能。
<二>CCLocation對像中包含着定點的相關信息數據。其屬性主要包括coordinate, altitude,horizontalAccuracy,verticalAccuracy, timestamp等,分別如下:
coordinate 用來存儲地理位置的latitude和longitude,分別表示緯度和經度,都是float類型.如可這樣: float latitude = location.coordinat.latitude; location是CCLocation的實例。這裏也把上面提到的CLLocationDegrees,它其實是一個double類型,在core Location框架中是用來儲存 CLLocationCoordinate2D實例coordinate的latitude 和longitude,
typedef double CLLocationDegrees;
typedef struct
{CLLocationDegrees latitude;
CLLocationDegrees longitude} CLLocationCoordinate2D;
altitude 表示位置的海拔高度,這個值是極不準確的。
horizontalAccuracy 表示水平準確度,這麼理解,它是以coordinate爲圓心的半徑,返回的值越小,證明準確度越好,如果是負數,則表示core location定位失敗。
verticalAccuracy表示垂直準確度,它的返回值與altitude相關,所以不準確。
Timestamp 返回的是定位時的時間,是NSDate類型。
<三>CLLocationMangerDelegate協議
我們只需實現兩個方法就可以了,如下:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation ;
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error;
上面第一個是定位時候回訪調,後者定位出錯時被調。
<四>現在可以去實現定位了:
新建一個view-based application模板的工程,假設項目名稱爲coreLocation.我們在contronller的頭文件和源文件中的代碼大概有如下:
.h
#import
#import
@interface CoreLocationViewController : UIViewController
e>{
CLLocationManager *locManager;
}
@property (nonatomic, retain) CLLocationManager *locManager;
@end
.m
#import "CoreLocationViewController.h"
@implementation CoreLocationViewController
@synthesize locManager;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
locManager = [[CLLocationManager alloc] init];
locManager.delegate = self;
locManager.desiredAccuracy = kCLLocationAccuracyBest;
[locManager startUpdatingLocation];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[locManager stopUpdatingLocation];
[locManager release];
[textView release];
[super dealloc];
}
#pragma mark -
#pragma mark CoreLocation Delegate Methods
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
CLLocationCoordinate2D locat = [newLocation coordinate];
float lattitude = locat.latitude;
float longitude = locat.longitude;
float horizon = newLocation.horizontalAccuracy;
float vertical = newLocation.verticalAccuracy;
NSString *strShow = [[NSString alloc] initWithFormat:
@"currentpos: 經度=%f 維度=%f 水平準確讀=%f 垂直準確度=%f ",
lattitude, longitude, horizon, vertical];
UIAlertView *show = [[UIAlertView alloc] initWithTitle:@"coreLoacation"
message:strShow delegate:nil cancelButtonTitle:@"i got it"
otherButtonTitles:nil];
[show show];
[show release];
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error{
NSString *errorMessage;
if ([error code] == kCLErrorDenied){
errorMessage = @"你的訪問被拒絕";}
if ([error code] == kCLErrorLocationUnknown) {
errorMessage = @"無法定位到你的位置!";}
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:nil message:errorMessage
delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil];
[alert show];
[alert release];
}
@end
運行的效果如下:
這一節我將用看到那個google的地圖,在實現上也相當簡便。嵌入地圖時需要MKMapView這個類,
它有很多方法和屬性,不過如果只是想得到基本的定位功能的話,只需實例化一個對像然後加到當前的
view上就可以了。
<一>先介紹一下,它的幾個常用的屬性。
region 用來設置地圖的那一部份被顯示,它是一個結構體,定義如下:
typedef struct{
CLLocationCoordinate2D center;//表示顯示的中心
MKCoordinateSpan span; //表示比例
}MKCoordinateRegion;
對於MKCoordinateSpan其定義如下:
typedef struct{
CLLocationDegrees latitudeDelta;//這類型在前一節中講過了,是double型的
CLLocationDegrees longitudeDlta;
}MKCoordinateSpan;
再看一下maptype屬性,它用於設置地圖的類型,如下所示:
MapType屬性值 描述
MKMapTypeStandard 表示標準的街道級地圖
MKMapTypeSatellite 表示衛星圖
MKMapTypeHybird 表示上面兩者的混合
其餘的就不再一一介紹了,去看看相關的文檔即可,在這裏已經可以把地圖弄出來了。
<二>下面我們把上一節中的代碼改一下:
.h頭文件
#import
#import
#import
@interface CoreLocationViewController : UIViewController
e,MKMapViewDelegate>{
MKMapView *map;
CLLocationManager *locManager;
CLLocationCoordinate2D loc;
}
@property (nonatomic, retain) MKMapView *map;
@property (nonatomic, retain) CLLocationManager *locManager;
- (void)setCurrentLocation:(CLLocation *)location;
@end
.m源文件
#import "CoreLocationViewController.h"
@implementation CoreLocationViewController
@synthesize map;
@synthesize locManager;
- (void)viewDidLoad {
map = [[MKMapView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 411.0f)];
map.showsUserLocation = YES;
[self.view addSubview:map];
locManager = [[CLLocationManager alloc] init];
locManager.delegate = self;
locManager.desiredAccuracy = kCLLocationAccuracyBest;
locManager.distanceFilter = 100;
[locManager startUpdatingLocation];
[super viewDidLoad];
}
- (void)dealloc {
[map release];
[locManager release];
[super dealloc];
}
#pragma mark -
#pragma mark Core Location Delegate Methods
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSLog(@"---------------");
loc = [newLocation coordinate];
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.1; //zoom level
span.longitudeDelta=0.1; //zoom level
NSLog(@"%f",loc.latitude);
NSLog(@"%f",loc.longitude);
region.span=span;
region.center=loc;
// map.showsUserLocation=NO;
map.mapType = MKMapTypeStandard;
[map setRegion:region animated:YES];
[map regionThatFits:region];
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error{
NSString *errorMessage;
if ([error code] == kCLErrorDenied){
errorMessage = @"被拒絕訪問";
}
if ([error code] == kCLErrorLocationUnknown) {
errorMessage = @"";
}
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:nil
message:errorMessage
delegate:self
cancelButtonTitle:@"紜畾"
otherButtonTitles:nil];
[alert show];
[alert release];
}
- (void)setCurrentLocation:(CLLocation *)location {
MKCoordinateRegion region ;
region.center = location.coordinate;
region.span.longitudeDelta = 0.15f;
region.span.latitudeDelta = 0.15f;
[map setRegion:region animated:YES];
}
@end
效果如下圖所示
Google地圖實現之三添加註解
2010-09-20 17:34:42| 分類:iphone/ipad開發|字號 訂閱
這一節將會講到添加地圖註解,這個需要用到MKAnnotation這個協議,主要有兩個UILabel類型的屬性,title和subtitle,當用戶點擊小別針時候就會把相關信息顯示出來,如下圖:
大概的操作是這樣的,先定義一個繼承了MKAnnotation的類,第當需要加上註解的時候,就根據當前的region等信息,實例化出一個對像,然後把它addAnnotation到googleMap上去就可了。
爲了實現MKAnnotation我們重新定義一個類來操作。新建objectiv-c的NSObject類
.h頭文件
#import
#import
#import
@interface MapAnnotations : NSObject {
CLLocationCoordinate2D coordinate;//這個表示一點,在map中就是中心點。
NSString *subtitle;
NSString *title;
}
-(id)initWithCoordinate:(CLLocationCoordinate2D) coordinate;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) NSString *subtitle;
@property (nonatomic, retain) NSString *title;
@end
.m源文件
#import "MapAnnotations.h"
@implementation MapAnnotations
@synthesize coordinate;
@synthesize title;
@synthesize subtitle;
-(id)initWithCoordinate:(CLLocationCoordinate2D) c{
coordinate=c;
NSLog(@"%f,%f",c.latitude,c.longitude);
return self;
}
- (void) dealloc
{
[title release];
[subtitle release];
[super dealloc];
}
@end
好了,有了這個類,我們就可以在數據更新的地方,實例化它的對像,然後加在MKMapview的實例上,就可以了,如下:
mapAnnotations=[[MapAnnotations alloc] initWithCoordinate:loc];
mapAnnotations.title=@"TEST";
mapAnnotations.subtitle=@"havea try";
[map addAnnotation:mapAnnotations];
[mapAnnotations release];
ios利用MKMapView顯示自己當前位置的地圖
在 ios利用MKMapView實現簡單的地圖一文中只是介紹了使用最簡單的地圖,這篇文章主要是介紹怎麼把地圖顯示到自己的當前位置和是地圖擴大。下面是我們實現的界面: 位置相當準確,
在ios利用MKMapView實現簡單的地圖一文中只是介紹了使用最簡單的地圖,這篇文章主要是介紹怎麼把地圖顯示到自己的當前位置和是地圖擴大。下面是我們實現的界面:
位置相當準確,和我用google map或得的信息一樣。
下面介紹一下實現的過程,是在 ios利用MKMapView實現簡單的地圖例子的基礎上實現的。DEVDIV博客
首先要獲取自己的經緯度,要使用CLLocationManager,CLLocationManager在CoreLocation.framework中,所以先在工程中添加CoreLocation.framework。
然後添加相關代碼:
添加CLLocationManagerDelegate協議
@interface
iphone_MapViewController : UIViewController
{
IBOutlet MKMapView *mapView;
}
實現代碼:
-
(void)viewDidLoad {
[super viewDidLoad];
mapView.showsUserLocation=YES;
CLLocationManager *locationManager
= [[CLLocationManager alloc] init];//創建位置管理器
locationManager.delegate=self;//設置代理
locationManager.desiredAccuracy=kCLLocationAccuracyBest;//指定需要的精度級別
locationManager.distanceFilter=1000.0f;//設置距離篩選器
[locationManager startUpdatingLocation];//啓動位置管理器
MKCoordinateSpan theSpan;
//地圖的範圍 越小越精確
theSpan.latitudeDelta=0.05;
theSpan.longitudeDelta=0.05;
MKCoordinateRegion theRegion;
theRegion.center=[[locationManager location] coordinate];
theRegion.span=theSpan;
[mapView setRegion:theRegion];
[locationManager release];
}
運行就可以得到如圖所示的效果了。
目前主流的智能手機大部分都支持GoogleMap地圖程序,而手機上的地圖程序確實能給我們的出行帶來很大的方便。在iPhone中利用MapKit框架可以很方便的顯示Google地圖,並且可以在地圖上添加標註。
二.具體介紹
1.MKMapView的顯示
(1)創建MKMapView
CGRect rect = CGRectMake(0, 20, 320, 460);
MKMapView *mapView = [[MKMapView alloc] initWithFrame:rect];
(2)設定經緯度
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude=24.148926;
theCoordinate.longitude=120.715542;
(3)設定顯示範圍
MKCoordinateSpan theSpan;
theSpan.latitudeDelta=0.1;
theSpan.longitudeDelta=0.1;
(4)設置地圖顯示的中心及範圍
MKCoordinateRegion theRegion;
theRegion.center=theCoordinate;
theRegion.span=theSpan;
(5)設置地圖顯示的類型及根據範圍進行顯示[mapView setMapType:MKMapTypeStandard];
[mapView setRegion:theRegion];
完成這些步驟,再把mapView添加到當前view中就可以顯示了。
2.在MKMapView上添加標註
(1)和標註相關的類及協議
(a)MKAnnotation Protocol
標註必須實現這個協議,有三個屬性,coordinate,title和subtitle,其中coordinate屬性必須設置。
@property (nonatomic, readonly) CLLocationCoordinate2D
coordinate
(b)MKAnnotationView
設置好Annotation後就可以用這個把標註在地圖上顯示出來,
- (id)initWithAnnotation:(id )annotation reuseIdentifier:(NSString
*)reuseIdentifier
其比較重要的屬性有
@property (nonatomic, retain) UIImage *image
自定義在地圖上標註的圖片
@property (nonatomic) BOOL canShowCallout
設置點擊後能否彈出標註
@property (retain, nonatomic) UIView
*rightCalloutAccessoryView
property (retain, nonatomic) UIView *leftCalloutAccessoryView
設置在標註的左右邊點擊後進一步彈出附屬的View
(c)MKPinAnnotationView
這是以大頭針的方式顯示標註,繼承自MKAnnotationView,同時添加了兩個屬性
@property (nonatomic) MKPinAnnotationColor pinColor
設置大頭針的顏色,有紅綠紫三種顏色可選擇
@property (nonatomic) BOOL animatesDrop
設置大頭針是否以掉下來的動畫方式顯示
(2)在地圖上添加Annotation的步驟
(a)創建一個實現MKAnnotation協議的類,在該類的初始化函數中給其coordinate屬性設置
(b)用上述方法創建Annotation
(c)把創建的Annotation用addAnnotation的方法添加到MapView中
(d)實現MKMapViewDelegate代理,在代理函數
- (MKAnnotationView *)mapView:(MKMapView *)mView
viewForAnnotation:(id )annotation中把Annotation以MKPinAnnotationView或
MKAnnotationView的方式標註在地圖上上顯示。
[文檔教程]使用MKMapView時,點擊iPhone屏幕,大頭針落在觸點的代碼
- - (MKAnnotationView
*)mapView:(MKMapView *)mV viewForAnnotation:(id
)annotation
- {
- MKPinAnnotationView
*pinView = nil;
- if(annotation !=
mapView.userLocation)
- {
- static NSString
*defaultPinID = @”目的地”;
- pinView =
(MKPinAnnotationView *)[mapView
dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
- if ( pinView == nil
)
- pinView =
[[[MKPinAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:defaultPinID] autorelease];
- pinView.pinColor =
MKPinAnnotationColorPurple;
- pinView.canShowCallout =
YES;
- pinView.animatesDrop =
YES;
-
pinView.draggable=YES;
-
pinView.selected=YES;
- }
- else
- {
- [mapView.userLocation
setTitle:@"I am here"];
- }
- return
pinView;
- }
google地圖http參數
無論是在android下還是通過瀏覽器,訪問google地圖是相同的參數。具體參數含義可以參見:
對我目前比較有用的參數是:
- q,查詢語句,我使用的是經緯度座標;
- dirflg,路線類型,比如r表示乘車(公交),t表示避開收費站,h避開高速公路,w步行,什麼都不選則是駕車(不避開收費站和高速公路);
- t,地圖類型,m地圖,k衛星,h地圖和衛星混合,p地形
- 和方向相關的:saddr … 從哪裏開始,終點在哪裏。
dirflg默認情況,駕車,無限制:
避開高速公路:
公交:
步行:
混合模式:
衛星模式:
地形模式:
Google Places API
https://developers.google.com/maps/documentation/places/?hl=zh-CN#PlaceSearchRequests