How to search MKMapView with UISearchBar?

后端 未结 6 2242
南旧
南旧 2020-11-28 18:54

I have an application that needs to have a similar search feature like the Apple \"Maps\" application (included with iPhone, iPod Touch and iPad).

The feature in que

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 19:51

    If anyone else is having the same issue, heres the link: https://github.com/stig/json-framework/ scroll down to Project renamed to SBJson

    Also, here is the code for getting all the data before your app uses it. Note the delegate method 'did receive data' as it appends the mutable data object with the downloaded data.

    I JUST USED MR GANDOS searchCoodinatesMETHOD AS IT IS AS IT WORKS WELL

    - (void) searchCoordinatesForAddress:(NSString *)inAddress
    {
        //Build the string to Query Google Maps.
        NSMutableString *urlString = [NSMutableString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=false",inAddress];
    
        //Replace Spaces with a '+' character.
        [urlString setString:[urlString stringByReplacingOccurrencesOfString:@" " withString:@"+"]];
    
        //Create NSURL string from a formate URL string.
        NSURL *url = [NSURL URLWithString:urlString];
    
        //Setup and start an async download.
        //Note that we should test for reachability!.
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    
        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    
        [connection release];
        [request release];
    }
    

    // STEP ONE // THIS ONE IS IMPORTANT AS IT CREATES THE MUTABLE DATA OBJECT AS SOON AS A RESPONSE IS RECEIVED

    -(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response
    {
        if (receivedGeoData) 
        {
            [receivedGeoData release];
            receivedGeoData = nil;
            receivedGeoData = [[NSMutableData alloc] init];
        }
        else
        {
            receivedGeoData = [[NSMutableData alloc] init];
        }
    
    }
    

    /// STEP TWO // THIS ONE IS IMPORTANT AS IT APPENDS THE DATA OBJECT WITH THE DATA

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
    {   
        [receivedGeoData appendData:data]; 
    }
    

    // STEP THREE...... // NOW THAT YOU HAVE ALL THE DATA MAKE USE OF IT

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        NSString *jsonResult = [[NSString alloc] initWithData:receivedGeoData encoding:NSUTF8StringEncoding];
        NSError *theError = NULL;
        dictionary = [NSMutableDictionary dictionaryWithJSONString:jsonResult error:&theError];
    
        NSLog(@"%@",dictionary);
    
        int numberOfSites = [[dictionary objectForKey:@"results"] count];
        NSLog(@"count is %d ",numberOfSites);      
    }
    
    -(void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
    {
        // Handle the error properly
    }
    

提交回复
热议问题