8

我正在開發一個項目,在該項目中,我需要爲UBER和OLA創建基於位置的汽車正在移動的類似iOS應用程序。我正在尋找某種類型的圖書館,它可以使汽車像OLA一樣移動並順利轉彎。現在我能夠將汽車從一個經緯度移動到另一個緯度。但是複雜的部分是如何在轉向時確保汽車面向前方。在Uber iOS應用程序上移動地圖上的註釋

請查閱下面的截圖。

Screenshot

回答

36

其實我也有我以前的iOS應用的一個要求,所以請找到下面的網址下載的代碼,並進行審查。

鏈接爲iOS地圖:https://www.dropbox.com/s/4jj8em1o786qa9j/TestAnnotationMoving.zip?dl=0

環境:8的Xcode和Objective-C,我做了它的代碼

亮點。

爲了實現與Uber iOS應用程序相同的移動功能,您需要首先計算舊位置和新位置之間的角度。請找到如何計算它的下面的代碼。

- (float)angleFromCoordinate:(CLLocationCoordinate2D)first 
       toCoordinate:(CLLocationCoordinate2D)second { 

    float deltaLongitude = second.longitude - first.longitude; 
    float deltaLatitude = second.latitude - first.latitude; 
    float angle = (M_PI * .5f) - atan(deltaLatitude/deltaLongitude); 

    if (deltaLongitude > 0)  return angle; 
    else if (deltaLongitude < 0) return angle + M_PI; 
    else if (deltaLatitude < 0) return M_PI; 

    return 0.0f; 
} 

然後將角度應用於移動的特定註釋。請爲下面的代碼找到相同的代碼。

float getAngle = [self angleFromCoordinate:oldLocation toCoordinate:newLocation]; 

//Apply the new location for coordinate.   
myAnnotation.coordinate = newLocation; 

//For getting the MKAnnotationView. 
AnnotationView *annotationView = (AnnotationView *)[self.mapView viewForAnnotation:myAnnotation]; 

//Apply the angle for moving the car. 
annotationView.transform = CGAffineTransformMakeRotation(getAngle); 

對於谷歌地圖

的谷歌地圖鏈接: https://www.dropbox.com/s/a5ts108lh5rr1gd/GoogleMap.zip?dl=0

如何運行項目?

  1. 從上面的項目下載dropbox鏈接。
  2. link獲取MapsAPIKey。
  3. 運行cocoapods爲iOS安裝google map sdk。

創建GMSMarker的對象。

GMSMarker *marker = [[GMSMarker alloc] init]; 
marker.position = CLLocationCoordinate2DMake(kCameraLatitude, kCameraLongitude); 
marker.map = _mapView; 
marker.icon = [UIImage imageNamed:@"carIcon"]; 

獲取兩個位置之間的角度並應用它們來計算角度請使用上面的函數。

//Get the angle 
float getAngle = [self angleFromCoordinate:oldLocation toCoordinate:newLocation]; 

//Apply the new coordinate 
marker.position = newLocation; 

//Apply the angle and convert to degree from radian. 
marker.rotation = getAngle * (180.0/M_PI); 

正如蘋果地圖.transform採取弧度,但在谷歌地圖.rotation需要的程度。

請在下面找到GIF表情,看它在地圖上的樣子。

GIF

爲了完成我創造,我又增加了1000條記錄經緯度的.csv文件的功能。

注意:您根據位置獲得角度,因此有時會出現由於位置而無法獲得正確角度的情況,因爲它完全取決於您的位置。

希望它適合你!

+0

感謝您的回答,我會檢查並給出我的反饋。 –

+0

當然,如果它適合你,那麼請接受答案。 –

+1

@RamkrishnaSharma加1解釋這一點,我們可以用'gmsmapview'也可以嗎? – vaibhav

相關問題