2016-04-15 47 views

回答

0

你可以嘗試在網上迅速向目標C轉換器。像 http://objc2swift.me 這些轉換器用於將目標c轉換爲Swift。我知道你想把Swift轉換成客觀的C,但我認爲你可以在這裏學到一些想法。只需編寫代碼。 Swift非常類似於客觀的c。語法上的差異。你可以很容易理解。 *堆棧溢出不能寫入代碼。如果您有任何代碼問題。我們會幫助你。

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; 
    } 
} 
相關問題