如何使用 UISearchBar 搜索 MKMapView? [英] How to search MKMapView with UISearchBar?

查看:24
本文介绍了如何使用 UISearchBar 搜索 MKMapView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序需要具有类似 Apple地图"应用程序(包含在 iPhone、iPod Touch 和 iPad 中)的搜索功能.

I have an application that needs to have a similar search feature like the Apple "Maps" application (included with iPhone, iPod Touch and iPad).

有问题的功能应该不难做,但我真的不知道如何在搜索栏中输入街道地址,然后获取该地址的坐标或可以帮助我实际移动的东西地图和那个地方的中心.

The feature in question should not be a hard thing to do, but I'm really clueless about how to input a Street Address in the search bar, and then obtaining coordinates for that address or something that can help me to actually move the map and center in that place.

我的意思是,我必须查询什么,Apple 是否提供地址搜索 API 方法"?还是我需要直接使用谷歌地图 API?

I mean, what do I have to query, does Apple provide an "address searching API method" ? or I need to use the google maps API directly ?

我很想知道应该怎么做.

I would love to hear how should it be done.

推荐答案

好的,回答我自己的问题:

Ok, to answer my own question:

如前所述,最好的做法是使用 Google Maps API,它支持多种格式,但出于多种原因,我选择使用 JSON.

As was mentioned before, the best thing to do is to use the Google Maps API, it supports a lot of formats but for several reasons I chose to go with JSON.

这里是对 Google Maps 执行 JSON 查询并获取查询坐标的步骤.请注意,并非所有正确的验证都已完成,这只是概念证明.

So here are the steps to perform a JSON query to Google Maps and obtain the coordinate of the query. Note that not all the correct validations are done, this is only a Proof of concept.

1) 为 iPhone 下载一个 JSON 框架/库,有几个,我选择了 这个,它非常好,似乎是一个活跃的项目,而且似乎有几个商业应用程序在使用它.因此,将其添加到您的项目中(说明 此处).

1) Download a JSON framework/library for the iPhone, there are several, I chose to go with this one, it's very good and seems an active project, plus several comercial applications seem to be using it. So add it to your project ( instructions here ).

2) 要在 Google 地图中查询地址,我们需要构建这样的请求 URL:http://maps.google.com/maps/geo?q=Paris+France

2) To query Google Maps for an address we need to build a request URL like this: http://maps.google.com/maps/geo?q=Paris+France

这个 url,将为查询Paris+France"返回一个 JSON 对象.

This url, will return a JSON object for the query "Paris+France".

3) 代码:

//Method to handle the UISearchBar "Search", 
- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar 
{
    //Perform the JSON query.
    [self searchCoordinatesForAddress:[searchBar text]];

    //Hide the keyboard.
    [searchBar resignFirstResponder];
}

在我们处理完 UISearchBar 搜索之后,我们必须向 Google Maps 发出请求:

After we handle the UISearchBar search, we must make the request to Google Maps:

- (void) searchCoordinatesForAddress:(NSString *)inAddress
{
    //Build the string to Query Google Maps.
    NSMutableString *urlString = [NSMutableString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@?output=json",inAddress];

    //Replace Spaces with a '+' character.
    [urlString setString:[urlString stringByReplacingOccurrencesOfString:@" " withString:@"+"]];

    //Create NSURL string from a formate URL string.
    NSURL *url = [NSURL URLWithString:urlString];

    //Setup and start an async download.
    //Note that we should test for reachability!.
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    [connection release];
    [request release];
}

然后我们当然必须处理 GoogleMaps 服务器的响应(注意:缺少很多验证)

We must of course then handle the response of the GoogleMaps server ( Note: a lot of validations missing)

//It's called when the results of [[NSURLConnection alloc] initWithRequest:request delegate:self] come back.
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{   
    //The string received from google's servers
    NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    //JSON Framework magic to obtain a dictionary from the jsonString.
    NSDictionary *results = [jsonString JSONValue];

    //Now we need to obtain our coordinates
    NSArray *placemark  = [results objectForKey:@"Placemark"];
    NSArray *coordinates = [[placemark objectAtIndex:0] valueForKeyPath:@"Point.coordinates"];

    //I put my coordinates in my array.
    double longitude = [[coordinates objectAtIndex:0] doubleValue];
    double latitude = [[coordinates objectAtIndex:1] doubleValue];

    //Debug.
    //NSLog(@"Latitude - Longitude: %f %f", latitude, longitude);

    //I zoom my map to the area in question.
    [self zoomMapAndCenterAtLatitude:latitude andLongitude:longitude];

    [jsonString release];
}

最后是缩放我的地图的功能,现在应该是一件微不足道的事情.

Finally the function to zoom my map, which should by now be a trivial thing.

- (void) zoomMapAndCenterAtLatitude:(double) latitude andLongitude:(double) longitude
{
    MKCoordinateRegion region;
    region.center.latitude  = latitude;
    region.center.longitude = longitude;

    //Set Zoom level using Span
    MKCoordinateSpan span;
    span.latitudeDelta  = .005;
    span.longitudeDelta = .005;
    region.span = span;

    //Move the map and zoom
    [mapView setRegion:region animated:YES];
}

希望这对某人有所帮助,因为 JSON 部分很难弄清楚,在我看来,该库没有很好的文档记录,但它仍然非常好.

Hope this helps someone because the JSON part was a real pain to figure out, the library is not very well documented in my opinion, still it's very good.

由于@Leo 的问题,将一个方法名称修改为searchCoordinatesForAddress:".我不得不说,这种方法作为概念证明很好,但是如果您计划下载大型 JSON 文件,则必须附加到 NSMutableData 对象以保存对谷歌服务器的所有查询.(请记住,HTTP 查询是由碎片组成的.)

Modified one method name to "searchCoordinatesForAddress:" because of @Leo question. I have to say that this method is good as a proof of concept but if you plan to download big JSON files , you will have to append to a NSMutableData object to hold all the query to the google server. ( remember that HTTP queries come by pieces . )

这篇关于如何使用 UISearchBar 搜索 MKMapView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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