2010-12-14 74 views
0

我目前使用下面的代碼:從谷歌地圖將MapKit在iOS

UIApplication *app = [UIApplication sharedApplication]; 
[app openURL:[NSURL URLWithString:@"http://maps.google.com/maps?saddr=Current%20Location&daddr=Chicago"]]; 

它的工作原理在谷歌地圖應用程序開放的方向完全正常。如果我想做同樣的事情,只有在應用程序本身內,我該怎麼做?我目前有一個viewController設置,它有一個MKMapView的實例,但它顯示了整個世界。一切工作正常,但只要我嘗試閱讀關於註釋的Apple文檔,我的頭就開始旋轉。

回答

1

一旦您將頭部纏繞在上面,註釋部分相當簡單,但路徑繪製部分是一個巨大的麻煩。我在one of my apps中做了這個工作,它花費了大量的工作,不僅畫出線條並保持縮放等等,而且更重要的是如果用戶指定一個具有數百步驟的路線(例如,越野高速公路)。

除非路由是你的應用程序的焦點,否則我不打擾。如果你仍然真的想回去看看代碼並提供一些指導。只是預先警告,它有一個驚人的很多。

0

我打算假設您正在查找地址並在您的應用中顯示該地址。您可以使用Geocoder API進行帶邊界(或不帶邊界)的查找。我正在使用這個NSString格式:NSString *geocoderURLFormat = @"http://maps.googleapis.com/maps/api/geocode/json?address=%@&bounds=%@&sensor=true"

此調用的回調將返回該位置的建議視口 - 西南和東北經緯度值。使用這些,你可以設置MKMapView對象的區域是這樣的:

CLLocationCoordinate2D coord; 
coord.latitude = location.latitude; 
coord.longitude = location.longitude; 

MKCoordinateSpan span; 
span.latitudeDelta = location.swLatitude > location.neLatitude ? location.swLatitude - location.neLatitude : location.neLatitude - location.swLatitude; 
span.longitudeDelta = location.swLongitude > location.neLongitude ? location.swLongitude - location.neLongitude : location.neLongitude - location.swLongitude; 

MKCoordinateRegion region; 
region.span = span; 
region.center = coord; 

[mapView setRegion:region animated:YES]; 

請注意,我仍然對MKCoordinateSpanlatitudeDeltalongitudeDelta值有點模糊,代碼因此,醜陋。我使用的location變量是從調用Geocoder的結果中構建的。

希望這會有所幫助!