在Google Map Sdk上搜索 [英] Search on Google Map Sdk
问题描述
我需要在我的应用中实现地图视图以找到所需的位置.我曾尝试使用SVGeocoder概念.
I need to implement the map view in my app to locate the required place. I had tried with the SVGeocoder concept.
[SVGeocoder geocode:searchfield.text
completion:^(NSArray *placemarks, NSHTTPURLResponse *urlResponse, NSError *error) {
}
但是假设我正在尝试搜索任何餐馆,那么结果为nil.
But suppose I am trying to search any restaurent then the result is nil.
我当时在Google Map sdk上查找,但不知道如何在GMSCameraPosition类上执行搜索功能.
I was looking on Google map sdk but don't know how to do search functionality on GMSCameraPosition class.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:latitude
longitude:longitude
zoom:5];
如何使用google sdk搜索地址.
how to search with the address using google sdk.
先谢谢了.
推荐答案
如果我正确理解它,则需要地址字符串中的位置坐标.其正向地理编码.您可以为此查看Google的免费api: Link1
If I understood it correctly, you need the location co-ordinates from a address string. Its Forward geo-coding. You can take a look at Google's free api for this: Link1
您将需要Google帐户中的API密钥才能访问此api,并且可以根据您的请求数量来选择免费或商业计划.
You will need a API key from your google account to access this api and there is way to select a free or business plan depending on your number of requests.
您需要使用CLLocation对象从地址中获取坐标.我写了类似的功能.
CLLocation* temp_location=[[CLLocation alloc]init];
temp_location=[GeoCoding findAddressCordinates:sourceAddressTxtField.text];
You need to use a CLLocation object for getting co-ordinates from your address. I wrote a similar function.
CLLocation* temp_location=[[CLLocation alloc]init];
temp_location=[GeoCoding findAddressCordinates:sourceAddressTxtField.text];
//对GeoCoding进行分类以找到坐标
// Class GeoCoding to find Co-ordinates
#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地图中使用此坐标来聚焦相机的位置.
You can then use this co-ordinates in your Google Map to focus your camera position.
这篇关于在Google Map Sdk上搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!