【问题标题】:Search on Google Map Sdk在谷歌地图 SDK 上搜索
【发布时间】:2014-05-30 07:32:13
【问题描述】:

我需要在我的应用中实现地图视图来定位所需的位置。我曾尝试使用 SVGeocoder 概念。

[SVGeocoder geocode:searchfield.text
             completion:^(NSArray *placemarks, NSHTTPURLResponse *urlResponse, NSError *error) {
}

但假设我正在尝试搜索任何餐厅,那么结果为零。

我正在查看 Google 地图 sdk,但不知道如何在 GMSCameraPosition 类上执行搜索功能。

GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:latitude
                                                          longitude:longitude
                                                               zoom:5]; 

如何使用 google sdk 搜索地址。

提前致谢。

【问题讨论】:

    标签: ios mkmapview google-maps-sdk-ios


    【解决方案1】:

    如果我理解正确,您需要地址字符串中的位置坐标。它的前向地理编码。你可以看看谷歌的免费api:Link1

    您需要从您的 Google 帐户获取 API 密钥才能访问此 API,并且可以根据您的请求数量选择免费或商业计划。

    您需要使用 CLLocation 对象从您的地址获取坐标。我写了一个类似的函数。

     CLLocation* temp_location=[[CLLocation alloc]init]; temp_location=[GeoCoding findAddressCordinates:sourceAddressTxtField.text];

      // 使用 GeoCoding 类查找坐标

    #import <Foundation/Foundation.h>
    @interface GeoCoding : NSObject  {
    
    }
    
    +(CLLocation*)findAddressCordinates:(NSString*)addressString;
    
    @end
    
      #import "GeoCoding.h"
    #import <CoreLocation/CLAvailability.h>
    
    @implementation GeoCoding
    
    +(CLLocation*)findAddressCordinates:(NSString*)addressString {
        CLLocation *location;
        NSString *url = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=true", addressString];
        url = [url stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    
        NSURL *wurl = [NSURL URLWithString:url];
        NSData *data = [NSData dataWithContentsOfURL: wurl];
    
        // Fail to get data from server
        if (nil == data) {
    
            NSLog(@"Error: Fail to get data");
        }
        else{
            // Parse the json data
            NSError *error;
            NSDictionary *json = [NSJSONSerialization
                                  JSONObjectWithData:data
                                  options:kNilOptions
                                  error:&error];
    
            // Check status of result
            NSString *resultStatus = [json valueForKey:@"status"];
            // If responce is valid
            if ( (nil == error) && [resultStatus isEqualToString:@"OK"] ) {
                NSDictionary *locationDict=[json objectForKey:@"results"] ;
                NSArray *temp_array=[locationDict valueForKey:@"geometry"];
                NSArray *temp_array2=[temp_array valueForKey:@"location"];
                NSEnumerator *enumerator = [temp_array2 objectEnumerator];
                id object;
                  while ((object = [enumerator nextObject])) {
                     double latitude=[[object valueForKey:@"lat"] doubleValue];
                     double longitude=[[object valueForKey:@"lng"] doubleValue];
                     location=[[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
    
                       NSLog(@"CLLocation lat  is  %f  -------------& long   %f",location.coordinate.latitude, location.coordinate.longitude);
    
                 }
            }
       }
    
        return location;
    }
    
    @end
    

    然后,您可以在 Google 地图中使用此坐标来聚焦您的相机位置。

    【讨论】:

    • @Sudha 你觉得这个答案有用吗?
    • [地理编码 findAddressCordinates:sourceAddressTxtField.text];什么是地理编码?
    • 它是一个 NSObject 类。
    • 如果地址不是英文的,记得转“[NSString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-29
    • 1970-01-01
    • 2016-02-16
    • 2015-07-01
    • 1970-01-01
    • 2011-09-18
    相关资源
    最近更新 更多