I faced this problem,  when trying to reverse geocode with the location information I had and and my current location. So after googling I found that many of the developers faced the same problem and the problem goes this way. At some point in time mostly, right from the morning to around 5.pm in the evening , the google server  does not respond to the requests that go from our apps, for whatever reasons that Apple and Google knows. Is there any alternative? Yes.

you can use the Google ‘s Reverse Geocode API’s to achieve the same what MKReverseGeoCoder does. But we will have to write the webservice functionalities and take care of parsing.

Example :

http://maps.google.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true

webservice = [[ADCWebservice alloc] init];

NSArray *keys = [NSArray arrayWithObjects:@”latlng”, @”sensor”, nil];

NSString *latitudeLongitude = [NSString stringWithFormat:@”%f,%f”,mapView.userLocation.location.coordinate.latitude,mapView.userLocation.location.coordinate.longitude];

NSArray *objects = [NSArray arrayWithObjects:latitudeLongitude,@”true”, nil];

NSDictionary *params =  [NSDictionary dictionaryWithObjects:objects forKeys:keys];

[webservice makeAPICall:params];

I have used JSON data format. And my Webserice class interface and  implementation goes this way:

Interface:

#import <Foundation/Foundation.h>

/*

Protocol for updating the Label.

*/

@protocol CustomWebServiceDelegate

– (void)setPlacemarkInfo:(NSDictionary*)dictionary;

@end

@interface ADCWebservice : NSObject {

id <CustomWebServiceDelegate> delegate;

NSMutableData *receivedData;

id returnDataFromWebservice;

NSDictionary *returnValue;

bool isQuerying;

}

@property (nonatomic, retain)id delegate;

@property bool isQuerying;

@property (nonatomic, retain)  NSDictionary *returnValue;

@property (nonatomic, retain) NSMutableData *receivedData;

– (void)makeAPICall:(NSDictionary *)dict;

@end

Implementation

#import “ADCWebservice.h”

#import “ADCUtilities.h”

#import <JSON/JSON.h>

@implementation ADCWebservice

@synthesize receivedData,isQuerying,returnValue, delegate;

#define kReverseGeoCodingURL @”http://maps.google.com/maps/api/geocode/json?&#8221;

– (void)makeAPICall:(NSDictionary *)params{

NSString *url=@””;

BOOL firstKey=TRUE;

for (NSString *key in params)

{

NSString *value=[params objectForKey:key];

if (!firstKey) url=[url stringByAppendingString:@”&”];

url=[url stringByAppendingString:key];

url=[url stringByAppendingString:@”=”];

url=[url stringByAppendingString: [value stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ];

firstKey=FALSE;

}

NSLog(@”URL Parameter:%@”,url);

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

NSData *postData = [url dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];

[request setURL:[NSURL URLWithString:kReverseGeoCodingURL]];

[request setHTTPMethod:@”POST”];

[request setValue:@”application/x-www-form-urlencoded” forHTTPHeaderField:@”content-type”];

[request setHTTPBody:postData];

/* create the NSMutableData instance that will hold the received data */

receivedData = [[NSMutableData alloc] initWithLength:0];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request

delegate:self

startImmediately:YES];

[connection start];

}

– (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

[receivedData setLength:0];

}

– (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

[receivedData appendData:data];

}

– (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

[ADCUtilities displayAlertView:[error localizedDescription]];

}

– (void)connectionDidFinishLoading:(NSURLConnection *)connection {

[connection release];

NSString *jsonString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];

if([@”” isEqual:jsonString] ||jsonString==nil){

returnValue =  nil;

}

else {

SBJSON *jsonParser = [SBJSON new];

returnValue= (NSDictionary*)[jsonParser objectWithString:jsonString];

}

[jsonString release];

[delegate setPlacemarkInfo:returnValue];

}

– (void)dealloc {

[receivedData release];

[returnValue release];

[super dealloc];

}

@end

Advertisements