IOS: 按名称在地图上显示多个注释
IOS: Displaying multiple annotations on map by name
在我的项目中,我必须在 MapView
上显示多个注释。我从服务器收到的响应显示纬度和经度)。
有人请建议我编码如何在地图上显示多个注释,因为我将所有地址值放在 NSArray
中。我的数组在控制台中的输出如下:
数组名称:arrayforlocation
(
"[39.4835647,-119.7310213]",
"[42.5629668,-114.4608711]",
"[45.5064511,-122.7756216]",
"[40.2338438,-111.6585337]",
"[45.7832856,-108.5006904]"
)
还请建议一些代码,将此数组拆分为两个数组,以获得单独的纬度和经度值。
我建议,你也可以让你的网络服务开发人员发送位置(以经纬度的形式),如果他们不能,那么你可以通过 CLGeocoder (or can use some other accurate service provider like Google or a library) 请求获取经纬度信息对于您拥有的特定地址。只有在那之后,您才能在地图上显示特定地址的大头针。
例如
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:@"Your Address String"
completionHandler:^(NSArray* placemarks, NSError* error){
for (CLPlacemark* aPlacemark in placemarks)
{
// Process the placemark.
NSString *latitude = [NSString stringWithFormat:@"%.4f",aPlacemark.location.coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:@"%.4f",aPlacemark.location.coordinate.longitude];
}
}];
然后,您可以按照 this 在您的地图上显示注释。
这是在地图视图上添加注释的代码:
CLLocationCoordinate2D location;
MKCoordinateSpan span;
location.latitude = (double) 44.4758;
location.longitude = (double) -73.2125;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2;
MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:@"BCA" andCoordinate:location];
[self.mapView addAnnotation:newAnnotation];
themapView = [[MapView alloc] initWithNibName:@"MapView" bundle:nil];
[self.view addSubview:themapView.view];
希望这对您有所帮助:)
我自己解决了这个问题。
这是我的解决方案:
首先我为纬度和经度创建单独的数组然后
在 .m 文件中,我输入了以下代码
-(id)initWithCoordinate:(CLLocationCoordinate2D) c titleee:(NSString *) t subTitle:(NSString *)timed time:(NSString *)tim
{
self.coordinate=c;
self.time=tim;
self.subTitle=timed;
self.titleee=t;
return self;
}
-(id)initWithCoordinate:(CLLocationCoordinate2D) c titleee:(NSString *)t
{
self.coordinate=c;
self.titleee=t;
return self;
}
-(void)showaddressonmap
{
for ( int i=0; i<[latArray count]; i++)
{
CLLocationCoordinate2D coord;
coord.latitude=[[NSString stringWithFormat:@"%@",[latArray objectAtIndex:i]] floatValue];
coord.longitude=[[NSString stringWithFormat:@"%@",
[longArray objectAtIndex:i]] floatValue];
MKCoordinateRegion region1;
region1.center=coord;
region1.span.longitudeDelta=20 ;
region1.span.latitudeDelta=20;
[mapvw setRegion:region1 animated:YES];
NSString *titleStr =[locationName objectAtIndex:i] ;
NSLog(@"title is:%@",titleStr);
NearestResultDiagnose * annotObj =[[NearestResultDiagnose alloc]initWithCoordinate:coord titleee:titleStr];
[mapvw addAnnotation:annotObj];
}
}
然后在我的 .h 文件中放入以下代码
{
CLLocationCoordinate2D coordinate;
NSString *titleee;
NSString *subTitle;
NSString *time;
}
@property (nonatomic)CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) NSString *titleee;
@property (nonatomic, retain) NSString *subTitle;
@property (nonatomic,retain) NSString *time;
-(id)initWithCoordinate:(CLLocationCoordinate2D) c titleee:(NSString *) t subTitle:(NSString *)timed time:(NSString *)tim;
-(id)initWithCoordinate:(CLLocationCoordinate2D) c titleee:(NSString *)t;
并综合属性
在我的项目中,我必须在 MapView
上显示多个注释。我从服务器收到的响应显示纬度和经度)。
有人请建议我编码如何在地图上显示多个注释,因为我将所有地址值放在 NSArray
中。我的数组在控制台中的输出如下:
数组名称:arrayforlocation
(
"[39.4835647,-119.7310213]",
"[42.5629668,-114.4608711]",
"[45.5064511,-122.7756216]",
"[40.2338438,-111.6585337]",
"[45.7832856,-108.5006904]"
)
还请建议一些代码,将此数组拆分为两个数组,以获得单独的纬度和经度值。
我建议,你也可以让你的网络服务开发人员发送位置(以经纬度的形式),如果他们不能,那么你可以通过 CLGeocoder (or can use some other accurate service provider like Google or a library) 请求获取经纬度信息对于您拥有的特定地址。只有在那之后,您才能在地图上显示特定地址的大头针。
例如
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:@"Your Address String"
completionHandler:^(NSArray* placemarks, NSError* error){
for (CLPlacemark* aPlacemark in placemarks)
{
// Process the placemark.
NSString *latitude = [NSString stringWithFormat:@"%.4f",aPlacemark.location.coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:@"%.4f",aPlacemark.location.coordinate.longitude];
}
}];
然后,您可以按照 this 在您的地图上显示注释。
这是在地图视图上添加注释的代码:
CLLocationCoordinate2D location;
MKCoordinateSpan span;
location.latitude = (double) 44.4758;
location.longitude = (double) -73.2125;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2;
MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:@"BCA" andCoordinate:location];
[self.mapView addAnnotation:newAnnotation];
themapView = [[MapView alloc] initWithNibName:@"MapView" bundle:nil];
[self.view addSubview:themapView.view];
希望这对您有所帮助:)
我自己解决了这个问题。 这是我的解决方案: 首先我为纬度和经度创建单独的数组然后 在 .m 文件中,我输入了以下代码
-(id)initWithCoordinate:(CLLocationCoordinate2D) c titleee:(NSString *) t subTitle:(NSString *)timed time:(NSString *)tim
{
self.coordinate=c;
self.time=tim;
self.subTitle=timed;
self.titleee=t;
return self;
}
-(id)initWithCoordinate:(CLLocationCoordinate2D) c titleee:(NSString *)t
{
self.coordinate=c;
self.titleee=t;
return self;
}
-(void)showaddressonmap
{
for ( int i=0; i<[latArray count]; i++)
{
CLLocationCoordinate2D coord;
coord.latitude=[[NSString stringWithFormat:@"%@",[latArray objectAtIndex:i]] floatValue];
coord.longitude=[[NSString stringWithFormat:@"%@",
[longArray objectAtIndex:i]] floatValue];
MKCoordinateRegion region1;
region1.center=coord;
region1.span.longitudeDelta=20 ;
region1.span.latitudeDelta=20;
[mapvw setRegion:region1 animated:YES];
NSString *titleStr =[locationName objectAtIndex:i] ;
NSLog(@"title is:%@",titleStr);
NearestResultDiagnose * annotObj =[[NearestResultDiagnose alloc]initWithCoordinate:coord titleee:titleStr];
[mapvw addAnnotation:annotObj];
}
}
然后在我的 .h 文件中放入以下代码
{
CLLocationCoordinate2D coordinate;
NSString *titleee;
NSString *subTitle;
NSString *time;
}
@property (nonatomic)CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) NSString *titleee;
@property (nonatomic, retain) NSString *subTitle;
@property (nonatomic,retain) NSString *time;
-(id)initWithCoordinate:(CLLocationCoordinate2D) c titleee:(NSString *) t subTitle:(NSString *)timed time:(NSString *)tim;
-(id)initWithCoordinate:(CLLocationCoordinate2D) c titleee:(NSString *)t;
并综合属性