Iphone 在MKMapView上加载地图视图
我想在Iphone 在MKMapView上加载地图视图,iphone,ios,ipad,google-maps,mkmapview,Iphone,Ios,Ipad,Google Maps,Mkmapview,我想在MKMapView上加载地图 基本上我想做的是 我想在我的地图视图中加载特定的场馆 我的数据库包含许多场馆,根据需要,我正在获取场馆,我想在我的地图视图中加载该场馆 例如:我从数据库中获取的地址是:@“伦敦牛津街100号,W1D 1LL,020 7636 0933”,然后我想在地图视图中加载此位置 提前感谢。 编辑: CLGeocoder *geocoder = [[CLGeocoder alloc] init]; NSString *venue= @"100 Oxfor
MKMapView
上加载地图
基本上我想做的是
我想在我的地图视图中加载特定的场馆
我的数据库包含许多场馆
,根据需要,我正在获取场馆
,我想在我的地图视图中加载该场馆
例如:我从数据库中获取的地址是:@“伦敦牛津街100号,W1D 1LL,020 7636 0933”,然后我想在地图视图中加载此位置
提前感谢。
编辑:
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
NSString *venue= @"100 Oxford Street, London, W1D 1LL, 020 7636 0933";
[geocoder geocodeAddressString:venue completionHandler:^(NSArray *placemarks, NSError *error)
{
if ([placemarks count] > 0)
{
CLPlacemark *placemark = [placemarks objectAtIndex:0];
CLLocation *loc = placemark.location;
CLLocationCoordinate2D _venue = loc.coordinate;
NSLog(@"_venue:=%f",loc.coordinate);
MKPointAnnotation *venueAnnotation = [[MKPointAnnotation alloc]init];
[venueAnnotation setCoordinate:_venue];
[venueAnnotation setTitle:@"Venue"];
[MapVw addAnnotation:venueAnnotation];
}
}
];
步骤:
1.导入
CoreLocation.framework
和MapKit.framework
2.使用
CoreLocation
classGeocoder
的方法:
geoCodeAddressString:completionHandler:
第三步。如果要在场地点上放置MKPointAnnotation
- (void)createVenueAnnotation{
[activity stopAnimating];
[activity removeFromSuperview];
MKPointAnnotation *venueAnnotation = [[MKPointAnnotation alloc]init];
[venueAnnotation setCoordinate:_venue];
[venueAnnotation setTitle:@"Venue"];
[_mapView addAnnotation:venueAnnotation];
}
第四步:将地图放在场地中心
// you need to call this function
- (void)centerMapAroundVenue
{
MKMapRect rect = MKMapRectNull;
MKMapPoint venuePoint = MKMapPointForCoordinate(_venue);
rect = MKMapRectUnion(rect, MKMapRectMake(venuePoint .x, venuePoint .y, 0, 0));
MKCoordinateRegion region = MKCoordinateRegionForMapRect(rect);
[_mapView setRegion:region animated:YES];
}
下面是一个简单的场景:
- 使用
GeoCoder
- 使用坐标,将注释添加到地图视图中李>
- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address
{
double latitude = 0, longitude = 0;
NSString *esc_addr = [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
if (result) {
NSScanner *scanner = [NSScanner scannerWithString:result];
if ([scanner scanUpToString:@"\"lat\" :" intoString:nil] && [scanner scanString:@"\"lat\" :" intoString:nil]) {
[scanner scanDouble:&latitude];
if ([scanner scanUpToString:@"\"lng\" :" intoString:nil] && [scanner scanString:@"\"lng\" :" intoString:nil]) {
[scanner scanDouble:&longitude];
}
}
}
CLLocationCoordinate2D center;
center.latitude = latitude;
center.longitude = longitude;
return center;
}
使用中心只需将注释添加到地图视图中即可
[self.mapView addAnnotation:<annotationName>];
[self.mapView addAnnotation:了解更多信息
希望这能有所帮助。使用地理编码器获取地址的lat和long from,并为您的MKMapView属性(弱、非原子)IBOUTLE MKMapView*MapVw添加注释;
我还连接了代表。[地理编码器地理编码地址字符串:@“伦敦牛津街100号,W1D 1LL,020 7636 0933”completionHandler…但它给出了错误是的,我写了以下内容:CLGeocoder*geocoder=[[CLGeocoder alloc]init];[geocoder-geocoder-addressString:@“伦敦牛津街100号,W1D 1LL,020 7636 0933”completionHandler:^(NSArray*placemarks,NSError*error){if(placemarks&&placemarks.count>0){CLPlacemark*topResult=[placemarks objectAtIndex:0];MKPlacemark*placemark=[MKPlacemark alloc]initWithPlacemark:topResult];[self.MapVw addAnnotation:placemark];}]
查看我的编辑:在问题中,我尝试过,但没有加载任何内容。您是否绑定了MapVw,为什么不将placemark直接添加到注释中…?让我们查看我的编辑:在问题中,我尝试过,但没有加载任何内容。您是否有internet连接?如果您使用的是simulator,请打开safari并检查您是否有internet连接与否,设备相同。不,我在设备上测试,互联网连接也正常,我的日志显示\u地点:=51.516139
,但地图没有加载。你应该使用a.n指示器视图来表示加载。正向地理编码需要时间。我建议你这样使用:[自执行选择器:@selector(drawVenueAnnotation)withObject:nil afterDelay:5];这里给出了5秒的延迟..drawVenueAnnotation是上面第3步中的方法。它只显示了印度地图,而不是我提供的坐标地图。请参见我的编辑:在问题中,我尝试过,但没有加载任何内容。您从NSLog(@“\u地点:=%f”,loc.coordinate)获得了什么;您显示的代码对我来说运行良好。请与mapView相关的。。
- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address
{
double latitude = 0, longitude = 0;
NSString *esc_addr = [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
if (result) {
NSScanner *scanner = [NSScanner scannerWithString:result];
if ([scanner scanUpToString:@"\"lat\" :" intoString:nil] && [scanner scanString:@"\"lat\" :" intoString:nil]) {
[scanner scanDouble:&latitude];
if ([scanner scanUpToString:@"\"lng\" :" intoString:nil] && [scanner scanString:@"\"lng\" :" intoString:nil]) {
[scanner scanDouble:&longitude];
}
}
}
CLLocationCoordinate2D center;
center.latitude = latitude;
center.longitude = longitude;
return center;
}
[self.mapView addAnnotation:<annotationName>];