Forward geocoding doesnt give correct results

200 views Asked by At

I didn't get correct results for Forward geocoding in certain cases. When I search for some places or hotel it shows result of some others places or areas. I have got following code. I study the following link. What url should i place to get correct results.

how can we implement the following given in following site

https://developers.google.com/places/webservice/autocomplete

A request for addresses containing "Vict" with results in French:

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Vict&types=geocode&language=fr&key=API_KEY A request for cities containing "Vict" with results in Brazilian Portuguese:

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Vict&types=(cities)&language=pt_BR&key=API_KEY

I have implemented following but it doesn't give the results as i aspected

- (CLLocationCoordinate2D)addressLocation{
     NSError *error = nil;

      //   NSString *lookUpString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=true", SearchtextField];
       //  NSString *API_KEY=@"AIzaSyB27SkGBzvEYKcxvZ5nmOVWvrA-6Xqf-7A";
        NSString *API_KEY=@"AIzaSyCHcqJcqZbP1XpU-WB4VfRct5hpdgqisSY";
        NSString *lookUpString = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/geocode/json?address=%@&region=np&key=%@", SearchtextField,API_KEY];



        lookUpString = [lookUpString stringByReplacingOccurrencesOfString:@" " withString:@"+"];

        NSData *jsonResponse = [NSData dataWithContentsOfURL:[NSURL URLWithString:lookUpString]];

        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonResponse options:kNilOptions error:&error];
        NSArray *locationArray = [[[jsonDict valueForKey:@"results"] valueForKey:@"geometry"] valueForKey:@"location"];


        NSString *statusString = [jsonDict valueForKey:@"status"];

          if ([statusString isEqualToString:@"OK"])
        {
                locationArray = [locationArray objectAtIndex:0];

                Str_Latitude= [locationArray valueForKey:@"lat"];
                Str_Longitude= [locationArray valueForKey:@"lng"];

                NSLog(@"LatitudeString:%@ & LongitudeString:%@", Str_Latitude, Str_Longitude);
                /*Google place latitude Longitude*/
                Arr_LatLong = @[Str_Latitude,Str_Longitude];

                [[NSUserDefaults standardUserDefaults] setValue:Str_Latitude forKey:@"Str_Latitude"];
                [[NSUserDefaults standardUserDefaults] setValue:Str_Longitude forKey:@"Str_Longitude"];
                [[NSUserDefaults standardUserDefaults] synchronize];

                GogLatitude = [Str_Latitude doubleValue];
                Goglongitude = [Str_Longitude doubleValue];

                if (Bool_SearchField) {
                    [self getGoogleAddress];
                    Bool_SearchField=FALSE;
                }else{

                }


        }else{

                UIAlertView *alertview =[[UIAlertView alloc] initWithTitle:@"Address not found" message:@"make sure you enter a valid address" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
                  [alertview show];

                   NSLog(@"Something went wrong, couldn't find address");
                  [self.tableView reloadData];
            }

        GogLocation.latitude = GogLatitude;
        GogLocation.longitude = Goglongitude;

        return GogLocation;
    }

When i search for radission in maps.google.com it shows as below

enter image description here

But when i search in my app it shows different locations then what I search enter image description here

2

There are 2 answers

0
kaho On

You can either take use of the Region Biasing, which according to wiki, should be NP. So add &region=np at the end of your query.

Or you can use the Viewport Biasing to set the bounds of your searches.

3
Kiran Thapa On

I have done what you have asked in one of my apps, but the url is different.

I am posting the function that gets called each time i input something in a text field.

-(void) startAutocomplete{
    NSString* baseUrl = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/queryautocomplete/json?input=%@&key=AIzaSyDz3HAmNY8NsgIhtA8gtbH-QA08Lg9tej4&types=all", self.locationTextfield.text];

    NSURL *url = [NSURL URLWithString:[baseUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSLog(@"Url: %@", url);
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        if (connectionError!=nil) {
            [[[UIAlertView alloc] initWithTitle:nil message:connectionError.localizedDescription delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil] show ] ;
        }else{
            NSError *error = nil;
            self.searchResult= [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

            //NSLog(@"result:%@",self.searchResult);
            [self.tableView reloadData];
        }

    }];
}