【问题标题】:How to Update or Change Camera Position between two Locations using GoogleMaps GMSCamera in iOS?如何在 iOS 中使用 GoogleMaps GMSCamera 更新或更改两个位置之间的相机位置?
【发布时间】:2017-03-15 21:41:06
【问题描述】:

在 viewDidLoad 中,我将当前位置显示在地图中。

CLLocationCoordinate2D coordinate = [self getLocation];
strForCurLatitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
strForCurLongitude= [NSString stringWithFormat:@"%f", coordinate.longitude];
GMSCameraUpdate  *updatedCamera = [GMSCameraUpdate setTarget:coor zoom:14];
[mapView_ animateWithCameraUpdate:updatedCamera];

但是在那之后,当我在两个文本字段 textFieldDidEndEditing 中给出两个不同的位置时,我有两个文本字段,然后相机没有移动或更新或更改谷歌地图中该位置的位置,这里我给出了我尝试过的代码但是它没有用。

GMSCameraUpdate *updatedCamera;
if(textField==self.txtAddress)
{        
    CLLocationCoordinate2D coor;
    coor.latitude=[strForLatitude doubleValue];
    coor.longitude=[strForLongitude doubleValue];
    pref = [NSUserDefaults standardUserDefaults];
    [pref setObject:strForLatitude forKey:@"Pickup_Latitude"];
    [pref setObject:strForLongitude forKey:@"Pickup_Longitude"];
    [pref synchronize];
    updatedCamera = [GMSCameraUpdate setTarget:coor zoom:14];
    [mapView_ animateWithCameraUpdate:updatedCamera];
}
if(textField==self.txtDropoffAddress)
{
    CLLocationCoordinate2D coor;
    coor.latitude=[[pref objectForKey:@"Destination_Latitude"] doubleValue];
    coor.longitude=[[pref objectForKey:@"Destination_Longitude"] doubleValue];
    updatedCamera = [GMSCameraUpdate setTarget:coor zoom:14];
    [mapView_ animateWithCameraUpdate:updatedCamera];
}

请帮我解决这个问题..

【问题讨论】:

  • 你想做什么??
  • 你在这里得到了pref objectForKey:@"Destination_Latitude"的值
  • 如果条件pref = [NSUserDefaults standardUserDefaults];,您首先为pref 赋值,如果条件[[pref objectForKey:@"Destination_Latitude"] doubleValue];,第二个尝试从中获取值
  • 您在哪里以及如何设置键 Destination_LatitudeDestination_Longitude 的值?
  • @nikdange_me 我编辑并更新了我的问题。

标签: ios objective-c google-maps


【解决方案1】:

根据我从您的解释中了解到的情况;然后建议任何其他要求。

使用 UITextFieldDelegate 扩展您的课程:

@interface YourViewController: UIViewController<UITextFieldDelegate>

并添加此方法:

 -(void)textFieldDidEndEditing:(UITextField *)textField {
          if((textField==self.txtAddress || textField==self.txtDropoffAddress) && (self.txtAddress.length > 0 && self.txtDropoffAddress.length>0))
          {
          // Your code to change position 
          }

【讨论】:

    【解决方案2】:

    最后我尝试使用 GMSCameraPosition 解决问题。

    -(void)textFieldDidEndEditing:(UITextField *)textField{
    
    if(textField==self.txtAddress  && self.txtAddress.text.length > 0)
    {  
        [CATransaction begin];
        [CATransaction setValue:[NSNumber numberWithFloat:2.0] forKey:kCATransactionAnimationDuration];
        GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude: [strForLatitude doubleValue]
                                                                longitude: [strForLongitude doubleValue] zoom: 14];
        [mapView_ animateToCameraPosition: camera];
        [CATransaction commit];
        self.ImageMarkerMapCentre.hidden = NO;
    }
    
    if(textField==self.txtDropoffAddress  && self.txtDropoffAddress.text.length > 0)
    {
        [CATransaction begin];
        [CATransaction setValue:[NSNumber numberWithFloat:2.0] forKey:kCATransactionAnimationDuration];
        GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:[[pref objectForKey:@"Destination_Latitude"] doubleValue] longitude:[[pref objectForKey:@"Destination_Longitude"] doubleValue] zoom:14];
        [mapView_ animateToCameraPosition: camera];
        [CATransaction commit];
        self.ImageMarkerMapCentre.hidden = YES;
    
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-02
      • 1970-01-01
      • 1970-01-01
      • 2015-04-17
      • 2014-09-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多