你能在目标c中给我这个代码吗?

时间:2016-04-15 07:58:04

标签: ios objective-c

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

How to update Google Maps marker position in swift/iOS

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

  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;
   }
 }