zoukankan      html  css  js  c++  java
  • 异步获取地址

    <p class="p1"><span class="s1">/</span>根绝经纬度获取地点信息</p> <p class="p2">- (<span class="s2">NSString</span>*)GetAddressByCoordinate:(<span class="s3">double</span>)latitude longitude:(<span class="s3">double</span>)longitud</p> <p class="p2">{</p> <p class="p3"><span class="s4"><span class="Apple-tab-span"> </span></span><span class="s5">NSLog</span><span class="s4">(</span>@"<span class="s6">加载</span>eeeeee"<span class="s4">);</span></p> <p class="p2"><span class="Apple-tab-span"> </span><span class="s2">NSString</span> *ld = [[<span class="s2">NSString</span> <span class="s5">alloc</span>] <span class="s5">initWithFormat</span>:<span class="s7">@"%f"</span>,latitude];</p> <p class="p2"><span class="Apple-tab-span"> </span><span class="s2">NSString</span> *lt = [[<span class="s2">NSString</span> <span class="s5">alloc</span>] <span class="s5">initWithFormat</span>:<span class="s7">@"%f"</span>,longitud];</p> <p class="p4"><span class="s4"><span class="Apple-tab-span"> </span></span><span class="s2">NSString</span><span class="s4"> *ldt=[</span><span class="s2">NSString</span><span class="s4"> </span><span class="s5">stringWithFormat</span><span class="s4">:</span><span class="s7">@"%@,%@"</span><span class="s4">,ld,lt];</span>//[[ld stringByAppendingFormat:@","] stringByAppendingFormat:lt];</p> <p class="p3"><span class="s4"><span class="Apple-tab-span"> </span></span><span class="s2">NSString</span><span class="s4"> *url1=</span>@"http://maps.google.com/maps/api/geocode/json?latlng="<span class="s4">;</span></p> <p class="p3"><span class="s4"><span class="Apple-tab-span"> </span></span><span class="s2">NSString</span><span class="s4"> *url2=</span>@"&sensor=true&language=zh-CN"<span class="s4">;</span></p> <p class="p4"><span class="s4"><span class="Apple-tab-span"> </span></span><span class="s2">NSString</span><span class="s4"> *urlRequest=[</span><span class="s2">NSString</span><span class="s4"> </span><span class="s5">stringWithFormat</span><span class="s4">:</span><span class="s7">@"%@%@%@"</span><span class="s4">,url1,ldt,url2];</span>//[[url1 stringByAppendingFormat:ldt] stringByAppendingFormat:url2];</p> <p class="p3"><span class="s4"><span class="Apple-tab-span"> </span></span><span class="s5">NSLog</span><span class="s4">(</span>@"urlRequest:%@"<span class="s4">,urlRequest);</span></p> <p class="p2"><span class="Apple-tab-span"> </span><span class="s2">NSURLRequest</span> *theRequest=[<span class="s2">NSURLRequest</span> <span class="s5">requestWithURL</span>:[<span class="s2">NSURL</span> <span class="s5">URLWithString</span>:urlRequest]</p> <p class="p5"><span class="s4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>  </span>cachePolicy<span class="s4">:</span>NSURLRequestUseProtocolCachePolicy</p> <p class="p2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>  <span class="s5">timeoutInterval</span>:<span class="s8">60.0</span>];</p> <p class="p4"><span class="s4"><span class="Apple-tab-span"> </span></span>// create the connection with the request</p> <p class="p4"><span class="s4"><span class="Apple-tab-span"> </span></span>// and start loading the data</p> <p class="p2"><span class="Apple-tab-span"> </span><span class="s2">NSURLConnection</span> *theConnection=[[<span class="s2">NSURLConnection</span> <span class="s5">alloc</span>] <span class="s5">initWithRequest</span>:theRequest <span class="s5">delegate</span>:<span class="s3">self</span>];</p> <p class="p2"><span class="Apple-tab-span"> </span><span class="s3">if</span> (theConnection) {</p> <p class="p4"><span class="s4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// Create the NSMutableData to hold the received data.</p> <p class="p4"><span class="s4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// receivedData is an instance variable declared elsewhere.</p> <p class="p6"><span class="s4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span><span class="s9">receivedData</span><span class="s4"> = [[</span>NSMutableData<span class="s4"> </span><span class="s5">data</span><span class="s4">] </span><span class="s5">retain</span><span class="s4">];</span></p> <p class="p2"><span class="Apple-tab-span"> </span>} <span class="s3">else</span> {</p> <p class="p4"><span class="s4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// Inform the user that the connection failed.</p> <p class="p3"><span class="s4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span><span class="s5">NSLog</span><span class="s4">(</span>@"fail"<span class="s4">);</span></p> <p class="p2"><span class="Apple-tab-span"> </span>}<span class="Apple-tab-span"> </span></p> <p class="p7"><span class="Apple-tab-span"> </span></p> <p class="p4"><span class="s4"><span class="Apple-tab-span"> </span></span>//NSString *jsonString = [[NSString alloc] </p> <p class="p4">//<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>initWithBytes:[receivedData bytes] </p> <p class="p4">//<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>length:[receivedData length] </p> <p class="p4">//<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>encoding:NSASCIIStringEncoding];</p> <p class="p4">//<span class="Apple-tab-span"> </span>NSLog(@"jsonString",jsonString);</p> <p class="p4">//<span class="Apple-tab-span"> </span>NSString *jsonString = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];  </p> <p class="p4">//<span class="Apple-tab-span"> </span>NSDictionary * jsonDic = [jsonString JSONValue];  </p> <p class="p4">//    NSArray *dict = [jsonDic valueForKeyPath:@"results.formatted_address"];</p> <p class="p4">//<span class="Apple-tab-span"> </span></p> <p class="p4">//<span class="Apple-tab-span"> </span>NSString *address=[dict objectAtIndex:0];</p> <p class="p7"><span class="Apple-tab-span"> </span></p> <p class="p3"><span class="s4"><span class="Apple-tab-span"> </span></span><span class="s3">return</span><span class="s4"> </span>@"address"<span class="s4">;</span></p> <p class="p2">}</p> <p class="p7"/> <p class="p2">- (<span class="s3">void</span>)connection:(<span class="s2">NSURLConnection</span> *)connection didReceiveResponse:(<span class="s2">NSURLResponse</span> *)response</p> <p class="p2">{</p> <p class="p4"><span class="s4">    </span>// This method is called when the server has determined that it</p> <p class="p4"><span class="s4">    </span>// has enough information to create the NSURLResponse.</p> <p class="p4"><span class="s4">    </span>// It can be called multiple times, for example in the case of a</p> <p class="p4"><span class="s4">    </span>// redirect, so each time we reset the data.</p> <p class="p4"><span class="s4">    </span>// receivedData is an instance variable declared elsewhere.</p> <p class="p8"><span class="s4">    [</span>receivedData<span class="s4"> </span><span class="s5">setLength</span><span class="s4">:</span><span class="s8">0</span><span class="s4">];</span></p> <p class="p2">}</p> <p class="p7"/> <p class="p4">//<span class="s6">接收</span>NSData<span class="s6">数据</span></p> <p class="p2">- (<span class="s3">void</span>)connection:(<span class="s2">NSURLConnection</span> *)connection didReceiveData:(<span class="s2">NSData</span> *)data</p> <p class="p2">{</p> <p class="p4"><span class="s4">    </span>// Append the new data to receivedData.</p> <p class="p4"><span class="s4">    </span>// receivedData is an instance variable declared elsewhere.</p> <p class="p2">    [<span class="s9">receivedData</span> <span class="s5">appendData</span>:data];</p> <p class="p2">}</p> <p class="p2">- (<span class="s3">void</span>)connection:(<span class="s2">NSURLConnection</span> *)connection</p> <p class="p2">  didFailWithError:(<span class="s2">NSError</span> *)error</p> <p class="p2">{</p> <p class="p4"><span class="s4">    </span>// release the connection, and the data object</p> <p class="p2">    [connection <span class="s5">release</span>];</p> <p class="p4"><span class="s4">    </span>// receivedData is declared as a method instance elsewhere</p> <p class="p8"><span class="s4">    [</span>receivedData<span class="s4"> </span><span class="s5">release</span><span class="s4">];</span></p> <p class="p7"><span class="Apple-tab-span"> </span></p> <p class="p4"><span class="s4">    </span>// inform the user</p> <p class="p3"><span class="s4">    </span><span class="s5">NSLog</span><span class="s4">(</span>@"Connection failed! Error - %@ %@"<span class="s4">,</span></p> <p class="p5"><span class="s4">          [error </span>localizedDescription<span class="s4">],</span></p> <p class="p6"><span class="s4">          [[error </span><span class="s5">userInfo</span><span class="s4">] </span><span class="s5">objectForKey</span><span class="s4">:</span>NSURLErrorFailingURLStringErrorKey<span class="s4">]);</span></p> <p class="p2">}</p> <p class="p1"><span class="s1">//</span>接收完毕<span class="s1">,</span>显示结果</p> <p class="p7"/> <p class="p7"/> <p class="p2">- (<span class="s3">void</span>)connectionDidFinishLoading:(<span class="s2">NSURLConnection</span> *)connection {</p> <p class="p7"><span class="Apple-tab-span"> </span></p> <p class="p7"><span class="Apple-tab-span"> </span></p> <p class="p3"><span class="s4">    </span><span class="s5">NSLog</span><span class="s4">(</span>@"<span class="s6">加载次数</span>:dddddd"<span class="s4">);</span></p> <p class="p2">    <span class="s2">NSString</span> *jsonString = [[<span class="s2">NSString</span> <span class="s5">alloc</span>] </p> <p class="p2">                         <span class="s5">initWithBytes</span>:[<span class="s9">receivedData</span> <span class="s5">bytes</span>] </p> <p class="p2">                         <span class="s5">length</span>:[<span class="s9">receivedData</span> <span class="s5">length</span>] </p> <p class="p2">                         <span class="s5">encoding</span>:<span class="s5">NSUTF8StringEncoding</span>];</p> <p class="p2"><span class="Apple-tab-span"> </span><span class="s5">NSLog</span>(<span class="s7">@"jsonString"</span>,jsonString);</p> <p class="p7"><span class="Apple-tab-span"> </span></p> <p class="p4"><span class="s4"><span class="Apple-tab-span"> </span></span>//<a href="http://maps.google.com/maps?saddr="><span class="s10">http://maps.google.com/maps?saddr=</span></a>{%E6%B7%B1%E5%9C%B3}&daddr={%E5%A4%A9%E6%B4%A5}  <span class="s6">导航</span></p> <p class="p4"><span class="s4"><span class="Apple-tab-span"> </span></span>//NSString *jsonString = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];  </p> <p class="p2"><span class="Apple-tab-span"> </span><span class="s2">NSDictionary</span> * jsonDic = [jsonString <span class="s11">JSONValue</span>];  </p> <p class="p4"><span class="s4"><span class="Apple-tab-span"> </span></span>//: format not a string literal and no format arguments</p> <p class="p4"><span class="s4"><span class="Apple-tab-span"> </span></span>//NSDictionary * dict = [jsonDic objectForKey:@"results"];//valueForKeyPath</p> <p class="p3"><span class="s4">    </span><span class="s2">NSArray</span><span class="s4"> *dict = [jsonDic </span><span class="s5">valueForKeyPath</span><span class="s4">:</span>@"results.formatted_address"<span class="s4">];</span></p> <p class="p7"><span class="Apple-tab-span"> </span></p> <p class="p2"><span class="Apple-tab-span"> </span><span class="s2">NSString</span> *address=[dict <span class="s5">objectAtIndex</span>:<span class="s8">0</span>];</p> <p class="p2"><span class="Apple-tab-span"> </span><span class="s9">curAddress</span>=address;</p> <p class="p7"><span class="Apple-tab-span"> </span></p> <p class="p8"><span class="s4"><span class="Apple-tab-span"> </span>[</span>addAnnotation<span class="s4"> </span><span class="s5">setTitle</span><span class="s4">:</span><span class="s7">@"</span><span class="s12">当前位置</span><span class="s7">"</span><span class="s4">];</span></p> <p class="p7"><span class="Apple-tab-span"> </span></p> <p class="p2"><span class="Apple-tab-span"> </span>[<span class="s9">addAnnotation</span> <span class="s5">setSubtitle</span>:address];</p> <p class="p7"><span class="Apple-tab-span"> </span></p> <p class="p2"><span class="Apple-tab-span"> </span>[connection <span class="s5">release</span>];</p> <p class="p8"><span class="s4">    [</span>receivedData<span class="s4"> </span><span class="s5">release</span><span class="s4">];</span></p> <p class="p7"><span class="Apple-tab-span"> </span></p> <p class="p7"><span class="Apple-tab-span"> </span></p> <p class="p2">}    </p><p class="p2"/><p class="p2">系统自带方法</p><p class="p2"/><p class="p2"> </p><title/><p class="p1"><span class="s1"><span class="Apple-tab-span"> </span></span>//MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc]  initWithCoordinate:newLocation.coordinate ];</p> <p class="p1">//<span class="Apple-tab-span"> </span>geoCoder.delegate = self;</p> <p class="p1">//<span class="Apple-tab-span"> </span>[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"zh-CN", nil] forKey:@"AppleLanguages"];</p> <p class="p1">//<span class="Apple-tab-span"> </span>[geoCoder start];</p><p/><p class="p2"/><p class="p2"> </p><title/><p class="p1">- (<span class="s1">void</span>)reverseGeocoder:(<span class="s2">MKReverseGeocoder</span> *)geocoder didFailWithError:(<span class="s2">NSError</span> *)error{</p> <p class="p2"><span class="s3">    </span><span class="s4">NSLog</span><span class="s3">(</span>@"MKReverseGeocoder has failed."<span class="s3">);</span></p> <p class="p1">}</p> <p class="p3"/> <p class="p1">- (<span class="s1">void</span>)reverseGeocoder:(<span class="s2">MKReverseGeocoder</span> *)geocoder didFindPlacemark:(<span class="s2">MKPlacemark</span> *)placemark {</p> <p class="p3"/> <p class="p1">    <span class="s2">NSString</span> *address = [<span class="s2">NSString</span> <span class="s4">stringWithFormat</span>:<span class="s5">@"%@ %@ %@ %@ %@%@"</span>,</p> <p class="p3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span> </p> <p class="p1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span> placemark.<span class="s2">country</span>,</p> <p class="p3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span> </p> <p class="p1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span> placemark.<span class="s2">administrativeArea</span>,</p> <p class="p3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span> </p> <p class="p1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span> placemark.<span class="s2">locality</span>,</p> <p class="p3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span> </p> <p class="p1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span> placemark.<span class="s2">subLocality</span>,</p> <p class="p3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span> </p> <p class="p1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span> placemark.<span class="s2">thoroughfare</span>,</p> <p class="p3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span> </p> <p class="p1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span> placemark.<span class="s4">subThoroughfare</span>];</p> <p class="p3"><span class="Apple-tab-span"> </span></p> <p class="p1">    <span class="s4">NSLog</span>(<span class="s5">@"</span><span class="s6">经纬度所对应的详细</span><span class="s5">:%@"</span>, address);</p> <p class="p3"><span class="Apple-tab-span"> </span></p> <p class="p4"><span class="s3"><span class="Apple-tab-span"> </span>[</span>addAnnotation<span class="s3"> </span><span class="s4">setTitle</span><span class="s3">:</span><span class="s5">@"</span><span class="s6">当前位置</span><span class="s5">"</span><span class="s3">];</span></p> <p class="p3"><span class="Apple-tab-span"> </span></p> <p class="p1"><span class="Apple-tab-span"> </span>[<span class="s7">addAnnotation</span> <span class="s4">setSubtitle</span>:address];</p> <p class="p3"><span class="Apple-tab-span"> </span></p> <p class="p1">    geocoder = <span class="s1">nil</span>;</p> <p class="p3"><span class="Apple-tab-span"> </span></p> <p class="p1">}</p><p/><p/>
  • 相关阅读:
    制作你的第一个HTML 5游戏
    拒绝臆想,让我们脚踏实地做设计!
    HTML 5或者Silverlight?
    如何替换掉回车键
    杀死团队的武器与修复团队的方法
    Google趋势中显示jQuery是最流行的JavaScript框架
    关于脚本载入器的思考
    简化Web开发的12个HTML5CSS框架
    线框图(demo草图)制作的总结
    江苏南通周边经纬度
  • 原文地址:https://www.cnblogs.com/zzxap/p/2175625.html
Copyright © 2011-2022 走看看