向前的地理编码不会给出正确的结果 [英] Forward geocoding doesnt give correct results

查看:193
本文介绍了向前的地理编码不会给出正确的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些情况下,我没有得到正确的转发地理编码结果。当我搜索某些地方或酒店时,会显示其他地方或地区的结果。我有以下代码。我研究了以下链接



我们应该如何执行以下站点中给出的以下内容:

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


包含Vict的地址请求以法语结果:

https://地图.googleapis.com / maps / api / place / autocomplete / json?input = Vict& types = geocode& language = fr& key = API_KEY
包含Vict
葡萄牙语:



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


我执行了以下操作,但没有给出结果,因为我认为

  - (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地点纬度经度* /
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;
}其他{

}


}其他{

UIAlertView * alertview = [[UIAlertView alloc] initWithTitle: @找不到地址消息:@请确认你输入了一个有效的地址delegate:self cancelButtonTitle:@OKotherButtonTitles:nil];
[alertview show];

NSLog(@出错了,找不到地址);
[self.tableView reloadData];
}

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

返回GogLocation;
}




当我在 maps.google.com 它显示如下




但是,当我在我的搜索应用程序显示不同的位置,然后我搜索



解决方案

我已经完成了你在我的一个应用程序中所要求的内容,但url不同。 b
$ b

我发布每次在文本字段中输入内容时被调用的函数。

   - (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:@OKotherButtonTitles:nil] show];
} else {
NSError * error = nil;
self.searchResult = [NSJSONSerialization JSONObjectWithData:data options:0 error:& error];

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

}];
}


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

But when i search in my app it shows different locations then what I search

解决方案

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

    }];
}

这篇关于向前的地理编码不会给出正确的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆