【问题标题】:can you give me this code in objective c?你能给我这个目标c中的代码吗?
【发布时间】:2016-08-07 02:07:30
【问题描述】:

嗨,我正在提供堆栈溢出的参考链接

How to update Google Maps marker position in swift/iOS

【问题讨论】:

  • 我试试,但我不知道 swift。
  • 我是新开发人员,我不知道 swift。有没有错
  • 你能展示你尝试过的代码吗
  • 看到这个对你有帮助stackoverflow.com/questions/19115293/…
  • 重新格式化您的问题并说明您面临的问题。

标签: ios objective-c


【解决方案1】:

您可以尝试在线 swift to Objective c 转换器。喜欢 http://objc2swift.me 这些转换器用于将目标 c 转换为 Swift。我知道您想将 Swift 转换为目标 C。但我认为您可以在这里学到一些想法。只写代码。 Swift 和objective c 非常相似,只是语法不同。你可以很容易地理解。 *堆栈溢出不适用于编写代码。如果您在代码中有任何问题。我们会帮助你。

【讨论】:

    【解决方案2】:

    这是代码,创建一个按钮,将选择器添加到该按钮。希望你能设置你的谷歌地图。

      NSMutableData *webData;
     @property NSURLConnection *connection;
    
    -(void) getResult  //call this in button selector
    {
      marker=nil;   //create a GMSMarker globally.
     [self.mapView clear];
      NSString *strUrl = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=%@&type=atm&sensor=true&name=%@&key=%@",latitude,longitude,_radius.text,_searchTxt.text,googleAPI_Key ];
     [self addMarkerDataUrl:strUrl];
    
    }
    -(void)addMarkerDataUrl:(NSString *)urlString
    
    {
      [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
      NSString* urlTextEscaped = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
      NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlTextEscaped]
    cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10];
     [request setHTTPMethod: @"GET"];
      self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
      if(self.connection)
      {
        webData = [[NSMutableData alloc]init];
      }
    }
    
    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
     {
      [webData setLength:0];
     }
    
    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
     {
       [webData appendData:data];
     }
    
     -(void)connectionDidFinishLoading:(NSURLConnection *)connection
      {
        if (webData)
        {
          [self gettingData:webData ];
        }
       [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
       webData=nil;
     }
    
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
      {
       [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;    
       NSLog(@"  response  %d",kCFURLErrorNotConnectedToInternet);
    
    
       if ([error code] == kCFURLErrorNotConnectedToInternet)
        {
    
          NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"No Connection Error"
                                                             forKey:NSLocalizedDescriptionKey];
         NSError *noConnectionError = [NSError errorWithDomain:NSCocoaErrorDomain code:kCFURLErrorNotConnectedToInternet userInfo:userInfo];
         NSLog(@"error %@",noConnectionError);
    
         [self handleError:noConnectionError];
    }
     else
     {
        [self handleError:error];
     }
     self.connection = nil;
    [self errorInConnection];
    

    }

    - (void)handleError:(NSError *)error
     {
       UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"SUBCOMMUNE"  message:@"Timed Out" delegate:nil cancelButtonTitle:@"OK"   otherButtonTitles:nil];
      [alertView show];
    }
    
    -(void)errorInConnection
    {
      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"ERROR"  message:@"Try again after some time" delegate:nil cancelButtonTitle:@"OK"   otherButtonTitles:nil];
      [alertView show];
    
    }
    
    - (void) gettingData:(NSData *)data
      {
    
       NSError* error;
       NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
       NSArray* places = [json objectForKey:@"results"];
       NSLog(@"Google Data: %@", places);
       tempArray = [[NSMutableArray alloc]init];
       NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
       if([[NSString stringWithFormat:@"%@",[allDataDictionary objectForKey:@"status"]]isEqualToString:@"OK"])  
        {
          collectionArray =[[NSMutableArray alloc]init];
          locArray = [[NSMutableArray alloc]init];
          NSMutableArray *legsArray = [[NSMutableArray alloc] initWithArray:allDataDictionary[@"results"]];
          for (int i=0; i< [legsArray count]; i++)  
          {
            [collectionArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:legsArray[i][@"icon"],@"icon", legsArray[i][@"name"],@"name",legsArray [i][@"vicinity"],@"vicinity",legsArray[i][@"id"],@"id",nil]];
            [locArray addObject:legsArray[i][@"geometry"][@"location"]];
            [tempArray addObject:legsArray[i][@"vicinity"]];
    
          }
         NSLog(@"CollectionArray =%@",collectionArray);
         NSLog(@"LocationArray =%lu",(unsigned long)locArray.count);
         [self addMarkers];
        }
       else
       {
         NSString *msg=[allDataDictionary objectForKey:@"error_message"];
         UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error !\n Check Radius or ATM Name" message:msg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
         [alert show];
       }
    }
    
    -(void)addMarkers
     {
       for(int i=0;i<locArray.count;i++)
       {
         starting = [NSMutableDictionary dictionaryWithObjectsAndKeys:locArray[i][@"lat"],@"lat",locArray[i][@"lng"],@"lng", nil];
    
         name = [NSMutableDictionary dictionaryWithObjectsAndKeys:collectionArray[i][@"name"],@"name",nil];
         CLLocationCoordinate2D position = CLLocationCoordinate2DMake([[starting objectForKey:@"lat"] doubleValue] , [[starting objectForKey:@"lng"] doubleValue]);
         marker1 = [GMSMarker markerWithPosition:position];
         GMSCameraUpdate *updatedCamera=[GMSCameraUpdate setTarget:CLLocationCoordinate2DMake(latitude, longitude) zoom:15];
         [self.mapView animateWithCameraUpdate:updatedCamera];
         marker1.title = [name objectForKey:@"name"];
         [marker1 setIcon:[UIImage imageNamed:@"Map Pin-48.png"]];
         marker1.appearAnimation = YES;
         marker1.map = self.mapView;
       }
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-04
      • 1970-01-01
      • 1970-01-01
      • 2012-06-03
      • 1970-01-01
      • 2021-11-14
      相关资源
      最近更新 更多