2013-08-24 35 views
5

Tôi hiện đang xây dựng ứng dụng ios và tôi hy vọng triển khai một chức năng nơi vị trí của người dùng được hiển thị trên chế độ xem Google Map và khi họ di chuyển một đường dẫn hiển thị của polyline đã được người dùng di chuyển cho đến nay. Điều này rõ ràng là cần phải xảy ra trong thời gian thực.Cách theo dõi vị trí của người dùng và hiển thị đường dẫn được sử dụng Google Maps ios SDK

Cho đến giờ tôi đã khởi tạo chế độ xem Google Maps và có thể hiển thị vị trí hiện tại của người dùng bằng chức năng observValueForKeyPath. Chế độ xem và cập nhật điểm đánh dấu vị trí khi người dùng di chuyển.

Tôi đã tìm ra cách tốt nhất để thực hiện việc này là tạo đối tượng GMSMutablePath thêm vị trí hiện tại của người dùng mỗi khi máy ảnh bản đồ cập nhật vị trí mới và sau đó tạo một polyline từ đường dẫn đó. Mã tôi đã sử dụng cho điều này là dưới đây:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
{ 
    if ([keyPath isEqualToString:@"myLocation"] && [object isKindOfClass:[GMSMapView class]]) 
    { 
     [self.myMapView animateToCameraPosition:[GMSCameraPosition cameraWithLatitude:self.myMapView.myLocation.coordinate.latitude 
                       longitude:self.myMapView.myLocation.coordinate.longitude 
                         zoom:18]]; 


     [trackedPath addCoordinate:CLLocationCoordinate2DMake(self.myMapView.myLocation.coordinate.latitude, self.myMapView.myLocation.coordinate.longitude)]; 
     GMSPolyline *testPoly = [GMSPolyline polylineWithPath:trackedPath]; 
     testPoly.strokeWidth = 8; 
     testPoly.strokeColor = [UIColor redColor]; 
     testPoly.map = myMapView; 
    } 
} 

Điều này không tạo ra bất kỳ polyline nào trên bản đồ trong thực tế để mọi trợ giúp/lời khuyên sẽ được đánh giá cao!

Trả lời

6
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{ NSString *pointString=[NSString stringWithFormat:@"%f,%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude]; 
    [self.points addObject:pointString]; 
GMSMutablePath *path = [GMSMutablePath path]; 
for (int i=0; i<self.points.count; i++) 
{   
    NSArray *latlongArray = [[self.points objectAtIndex:i]componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]]; 

[path addLatitude:[[latlongArray objectAtIndex:0] doubleValue] longitude:[[latlongArray objectAtIndex:1] doubleValue]]; 
} 

if (self.points.count>2) 
{ 
    GMSPolyline *polyline = [GMSPolyline polylineWithPath:path]; 
    polyline.strokeColor = [UIColor blueColor]; 
    polyline.strokeWidth = 5.f; 
    polyline.map = mapView_; 
    self.view = mapView_; 
}} 
+0

điểm trong mã của bạn là gì? –

+0

@sandeeptomar point là một mảng NSMutable. – ChenSmile

2

Nếu bạn muốn vẽ hai đường dẫn BTW, hai địa điểm sử dụng phương pháp này. các phương pháp demo nhanh chóng của nó để tìm tuyến đường của hai nơi.

//MARK: API CALL 
func GetRoughtofTwoLocation(){ 
    let originString: String = "\(23.5800),\(72.5853)" 
    let destinationString: String = "\(24.5836),\(72.5853)" 
    let directionsAPI: String = "https://maps.googleapis.com/maps/api/directions/json?" 
    let directionsUrlString: String = "\(directionsAPI)&origin=\(originString)&destination=\(destinationString)&mode=driving" 

    APICall().callApiUsingWithFixURLGET(directionsUrlString, withLoader: true) { (responceData) -> Void in 
     let json = responceData as! NSDictionary 
     let routesArray: [AnyObject] = (json["routes"] as! [AnyObject]) 
     var polyline: GMSPolyline? = nil 
     if routesArray.count > 0 { 
      let routeDict: [NSObject : AnyObject] = routesArray[0] as! [NSObject : AnyObject] 
      var routeOverviewPolyline: [NSObject : AnyObject] = (routeDict["overview_polyline"] as! [NSObject : AnyObject]) 
      let points: String = (routeOverviewPolyline["points"] as! String) 
      let path: GMSPath = GMSPath(fromEncodedPath: points)! 
      polyline = GMSPolyline(path: path) 
      polyline!.strokeWidth = 2.0 
      polyline!.map = self.mapView 
     } 

    } 
} 
Các vấn đề liên quan