2017-01-30 7 views
0

私はGoogleマップが初めてで、移動と回転に関するすべての質問と回答を検索しましたが、iOS Googleマップでは、uberのようなマーカーをスムーズに緯度と経度から回転させることができます。

私は2つの側面があるuberのようなアプリケーションに取り組んでいます.1つは助手席側で、もう1つは運転席側です。

私は正常に動くことができていますし、プロパティを見出しCLLocationManagerの助けを借りて、マーカー(車を)回転させるとのみによる1マーカーに。以下はドライバ側でマーカーを移動する方法です。完全に動作します。

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading 
{ 
    CLLocationCoordinate2D pos=CLLocationCoordinate2DMake(manager.location.coordinate.latitude, manager.location.coordinate.longitude); 
    marker.position=pos; 
    marker.appearAnimation = kGMSMarkerAnimationPop; 
    [CATransaction begin]; 
    [CATransaction setAnimationDuration:2.0]; 
    marker.position = pos; 
    marker.rotation = (manager.heading.trueHeading) * M_PI/180.0f; 
    float direction = -newHeading.trueHeading; 

    marker.iconView.transform = CGAffineTransformMakeRotation((direction * M_PI/180)); 

    double heading = newHeading.trueHeading - 65; 
    marker.groundAnchor = CGPointMake(0.5, 0.5); 
    marker.rotation = heading; 
    [CATransaction commit]; 
    marker.map = mapView; 
    [self UpdateDriverLocations:[NSString stringWithFormat:@"%f",heading]]; 
} 

今、助手席側では、ドライバーの位置(緯度と経度)を3秒タイマーで連続して取得しています。

self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(getcar) userInfo:nil repeats:YES]; 

ここで、すべてのドライバの緯度と経度を取得する方法です。

-(void)getcar 
{ 
    NSString *lat = [NSString stringWithFormat:@"%f",latitude]; 
    NSString *log = [NSString stringWithFormat:@"%f",longitude]; 

    if (latitude!=0) 
    { 
     NSString *URLString = [KMainUrl stringByAppendingString:KDriverLocation]; 
     NSDictionary *parameters = @{@"lat":lat,@" longt":log,@"vehiclename":@"Flinkar"}; 
     NSURL *URL = [NSURL URLWithString:URLString]; 
     AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; 
     manager.responseSerializer = [AFHTTPResponseSerializer serializer]; 
     [manager.requestSerializer setTimeoutInterval:150]; 
     [manager GET:URL.absoluteString parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) { 
      // [self hideActivityIndicatior]; 
      NSLog(@"JSON avi: %@", responseObject); 
      NSError* error; 
      NSArray* json = [NSJSONSerialization JSONObjectWithData:responseObject 
                  options:kNilOptions 
                   error:&error]; 
      if ([[[json valueForKey:@"success"]stringValue] isEqualToString:@"1"]) { 
       NSLog(@"Login successful"); 
       btnNext.userInteractionEnabled=YES; 
       aryMain=[json valueForKey:@"driver"]; 
       DictPublicDriverData=[json valueForKey:@"driver"]; 
           //NSString*str=[[aryMain objectAtIndex:0]valueForKey:@"duration"]; 
       // lblEstimatedTime.text= [str substringToIndex:2]; 

       for (int i=0; i<[[aryMain valueForKey:@"latit"] count]; i++){ 
        NSString *Nlat=[[aryMain objectAtIndex:i]valueForKey:@"latit"] ; 
        NSString *Nlong=[[aryMain objectAtIndex:i]valueForKey:@"longit"] ; 
        if (!(_arrMapList.count==aryMain.count)) 
        {      //if marker array and response array not same set marker and add marker on map and array 
         CLLocationCoordinate2D position= CLLocationCoordinate2DMake(Nlat.doubleValue, Nlong.doubleValue); 
         GMSMarker *marker=[GMSMarker markerWithPosition:position]; 
         UIImage *markerIcon = [UIImage imageNamed:@"carIcon"]; 
         marker.icon = markerIcon; 
         marker.map = mapView; 
         [_arrMapList addObject:marker]; 
        } 
        else{ //Move marker from Array 
          CLLocationCoordinate2D position= CLLocationCoordinate2DMake(Nlat.doubleValue, Nlong.doubleValue); 
          NSLog(@"eye %d",i); 
          [self updateLocation:_arrMapList setnewlocation:position setmarkeratintiger:i setheading:heading]; 
        } 
       } 
      } 
      else 
      { 
       //Car not found 
      } 
     }failure:^(NSURLSessionTask *operation, NSError *error) { 
      NSLog(@"Error avi: %@", error); 
     }]; 
    } 
} 

//move and rotate marker 
- (void)updateLocation:(NSArray*)markerarray setnewlocation:(CLLocationCoordinate2D)newlocation1 setmarkeratintiger:(int)i setheading:(NSString*)heading { 
    GMSMarker *marker = self.arrMapList[i]; 
    //Store location 
    CLLocation *A = [[CLLocation alloc] initWithLatitude:marker.position.latitude longitude:marker.position.longitude]; 
    CLLocation *B =[[CLLocation alloc] initWithLatitude:newlocation1.latitude longitude:newlocation1.longitude]; 
    double headinglat = [heading doubleValue]; // only available in ios so not used 
     NSLog(@"new location and old location ===oldlat= %f , old long= %f , newlat= %f , newlong= %f",marker.position.latitude , marker.position.longitude,newlocation1.latitude,newlocation1.longitude); 
     marker.position=newlocation1; 
     marker.appearAnimation = kGMSMarkerAnimationPop; 
     [CATransaction begin]; 
     [CATransaction setAnimationDuration:2.0]; 
    marker.rotation=[self DegreeBearing:A locationB:B]; //rotate marker 
    marker.position = newlocation1; // set marker 

     [CATransaction commit]; 
     marker.map = mapView; 
} 


-(double) DegreeBearing:(CLLocation*) A locationB: (CLLocation*)B{ 
    double dlon = [self ToRad:(B.coordinate.longitude - A.coordinate.longitude)]; 
    double dPhi = log(tan([self ToRad:(B.coordinate.latitude)]/2 + M_PI/4)/tan([self ToRad:(A.coordinate.latitude)]/2 + M_PI/4)); 
    if (fabs(dlon) > M_PI){ 
     dlon = (dlon > 0) ? (dlon - 2*M_PI) : (2*M_PI + dlon); 
    } 
    return [self ToBearing:(atan2(dlon, dPhi))]; 
} 

-(double) ToRad: (double)degrees{ 
    return degrees*(M_PI/180); 
} 

-(double) ToBearing:(double)radians{ 
    double degree = [self ToDegrees:radians]; 
    return degree+360% 360; 
} 

-(double) ToDegrees:(double)radians{ 
    return radians * 180/M_PI; 
} 

Iはフェッチドライバ位置と助手席側の複数のマーカーを移動し、回転させる方法上にしようとしているが、それは正しく移動していないと回転していません。どうすればこれを達成できますか?私のロジックに問題がありますか(ドライバの場所を取得する)。

ここでは、理解を深めるためにペーストボードにコードを貼り付けました。

Fetch Drivers location and show, move and rotate marker

+0

この答えは、マーカーの移動についてあなたhttps://stackoverflow.com/a/42291262/4831524 –

答えて

0
//Create the object of 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); 


- (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; 
} 
+0

と何をするのに役立ちます?これは回転のためのものです。 –

関連する問題