如何在iOS中的Google地图上显示多个标记? 我使用了以下方法,但它没有用。
for (int i = 0; i < [array count]; i++)
{
pointsToUse[i] = CLLocationCoordinate2DMake([[[[array objectAtIndex:0] componentsSeparatedByString:@","] objectAtIndex:0] floatValue],[[[[array objectAtIndex:0] componentsSeparatedByString:@","] objectAtIndex:1] floatValue]);
[_map animateToLocation:pointsToUse[i]];
GMSMarkerOptions *options = [[GMSMarkerOptions alloc] init];
options.position = pointsToUse[i];
[_map animateToLocation:pointsToUse[i]];
[_map addMarkerWithOptions:options];
}
答案 0 :(得分:1)
当我认为您应该使用[array objectAtIndex:0]
时,您正在使用[array objectAtIndex:i]
(在两个地方)?
另外,您可能不需要拨打animateToLocation
?
答案 1 :(得分:1)
我尝试了你的代码。这似乎工作正常。只需在将值传递给pointsToUse后删除索引0处的对象。
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"12.981902,80.266333",@"12.982902,80.266363", nil];
CLLocationCoordinate2D pointsToUse[5];
for (int i = 0; i < [array Size]; i++)
{
pointsToUse[i] = CLLocationCoordinate2DMake([[[[array objectAtIndex:0] componentsSeparatedByString:@","] objectAtIndex:0] floatValue],[[[[array objectAtIndex:0] componentsSeparatedByString:@","] objectAtIndex:1] floatValue]);
[array removeObjectAtIndex:0];
GMSMarkerOptions *options = [[GMSMarkerOptions alloc] init];
options.position = pointsToUse[i];
[mapView_ animateToLocation:pointsToUse[i]];
[mapView_ addMarkerWithOptions:options];
}
答案 2 :(得分:1)
是的,你们两个都是正确的。根据你的建议,我改变了代码,它的工作原理。 但问题是,我设置了缩放并且缩放是固定的。如果这两个位置很远,我在一个屏幕上看不到两个位置(我需要捏两个看)。我怎样才能同时看到这两个位置? 我的代码如下所示。
-(void) displayMapwithPositionfortheArray:(NSMutableArray*) array
{
CLLocationCoordinate2D firstPoint = CLLocationCoordinate2DMake([[[[array objectAtIndex:0] componentsSeparatedByString:@","] objectAtIndex:0] floatValue],[[[[array objectAtIndex:0] componentsSeparatedByString:@","] objectAtIndex:1] floatValue]);
GMSCameraPosition *currloc = [GMSCameraPosition cameraWithLatitude:firstPoint.latitude
longitude:firstPoint.longitude
zoom:8
bearing:0
viewingAngle:45];
_map = [GMSMapView mapWithFrame:CGRectZero camera:currloc];
_map.myLocationEnabled = YES;
_map.frame = CGRectMake(0, heightOffset, self.view.frame.size.width, self.view.frame.size.height - heightOffset);
[self.view addSubview:_map];
CLLocationCoordinate2D pointsToUse[[array count]];
for (int i = 0; i < [array count]; i++)
{
pointsToUse[i] = CLLocationCoordinate2DMake([[[[array objectAtIndex:i] componentsSeparatedByString:@","] objectAtIndex:0] floatValue],[[[[array objectAtIndex:i] componentsSeparatedByString:@","] objectAtIndex:1] floatValue]);
GMSMarkerOptions *options = [[GMSMarkerOptions alloc] init];
options.position = pointsToUse[i];
[_map addMarkerWithOptions:options];
}
}
答案 3 :(得分:1)
试试这个:
-(void)plotMutliplePinsonMap
{
mapView_ = [[GMSMapView alloc]initWithFrame:CGRectMake(0, 96, 320, 450)];
for(int i=0;i<[arrTobeShown count];i++)
{
double_lat = [[[arrTobeShown objectAtIndex:i]valueForKey:@"latitude"] doubleValue];
double_long = [[[arrTobeShown objectAtIndex:i]valueForKey:@"longitude"] doubleValue];
GMSMarker *mkr = [[GMSMarker alloc] init];
if (double_lat !=0 && double_long!=0)
{
[mkr setPosition:CLLocationCoordinate2DMake(double_lat, double_long)];
[mkr setTitle:[[arrTobeShown objectAtIndex:i] valueForKey:@"name"]];
[mkr setSnippet:[[arrTobeShown objectAtIndex:i] valueForKey:@"address"]];
[mkr setMap:mapView_];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:double_lat longitude:double_long zoom:5];
mapView_.camera=camera;
}
}
[self.view addSubview:mapView_];
[mapView_ setHidden:YES];
[self.view layoutIfNeeded];
}