[iPhone]MKMapViewで住所から地図を表示
●openURL でgoogle の機能を使って表示する方法
NSString *str = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", @"住所"];
NSURL *url = [NSURL URLWithString:str];
[UIApplication sharedApplication] openURL:url];
これだと地図アプリが起動して、自分のアプリが終了しています。
●MKMapViewで自作表示
検索してみると GMGeocoder が良いらしい という事でサイトを見てみたけど
http://code.google.com/p/gmgeocoder/
今はダウンロード出来ない と表示される。
それでも周辺をうろつくとソースが見つかったので使ってみたけど 処理のキモになっている
http://tinygeocoder.com/create-api.php
の精度が極めて悪くて 使い物にならない。
(このためダウンロード禁止にしたのか?)
例によって画面を作る
MKMapView はUIMapView を配置
肝心のソース
address に住所のもじれるを入れると表示する。
地図がロード出来ない時の処理などは省いているのでちゃんとしたアプリを作る時には必要
NSString *str = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", @"住所"];
NSURL *url = [NSURL URLWithString:str];
[UIApplication sharedApplication] openURL:url];
これだと地図アプリが起動して、自分のアプリが終了しています。
●MKMapViewで自作表示
検索してみると GMGeocoder が良いらしい という事でサイトを見てみたけど
http://code.google.com/p/gmgeocoder/
今はダウンロード出来ない と表示される。
それでも周辺をうろつくとソースが見つかったので使ってみたけど 処理のキモになっている
http://tinygeocoder.com/create-api.php
の精度が極めて悪くて 使い物にならない。
(このためダウンロード禁止にしたのか?)
例によって画面を作る
MKMapView はUIMapView を配置
肝心のソース
- (void)viewDidLoad {
[super viewDidLoad];
self.mapView.mapType = MKMapTypeStandard;
NSString *urlString =
[NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv",
[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
NSArray *listItems = [locationString componentsSeparatedByString:@","];
double latitude = 0.0;
double longitude = 0.0;
if ([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
latitude = [[listItems objectAtIndex:2] doubleValue];
longitude = [[listItems objectAtIndex:3] doubleValue];
} else {
//Show error
}
MKCoordinateSpan span;
span.latitudeDelta = 0.02;
span.longitudeDelta = 0.02;
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;
MKCoordinateRegion region;
region.center = location;
region.span = span;
[mapView setRegion:region animated:YES];
[mapView regionThatFits:region];
}
address に住所のもじれるを入れると表示する。
地図がロード出来ない時の処理などは省いているのでちゃんとしたアプリを作る時には必要