Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

ios google maps plotting multiple markers issues(info window and marker repeating)

First i create a Map with user location

-(void)initGogleMapView{

GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:currentLocation.coordinate.latitude
                                                        longitude:currentLocation.coordinate.longitude
                                                             zoom:1];

mapView = [GMSMapView mapWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64) camera:camera];
[self.view addSubview:mapView];
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = camera.target;

marker.appearAnimation = kGMSMarkerAnimationPop;
marker.map = mapView;
marker.title=@"Current Location";


}

Then i have an array of latitude and longitude and plot in for loop

   for(int i=0;i<[latLongArr count];i++)
         {
             GMSMarker *tempmarker = [[GMSMarker alloc] init];
             tempmarker.position = CLLocationCoordinate2DMake([[[latLongArr objectAtIndex:i] valueForKey:@"Latitude"] floatValue],[[[latLongArr objectAtIndex:i] valueForKey:@"Longitude"] floatValue]);
             tempmarker.title=[[latLongArr objectAtIndex:i] valueForKey:@"Name"];
             tempmarker.appearAnimation = kGMSMarkerAnimationPop;

             tempmarker.map = mapView;



         }

Markers are getting repeated with same title.Can anyone help me where i am doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First of all check your Latitude and Longitude in array, it might be duplicates. Otherwise, follow steps :

At very first, add GoogleMaps.bundle and GoogleMaps.framework to your project. And then when you want to implement google map, #import <GoogleMaps/GoogleMaps.h>, then set delegate @interface YourViewController : UIViewController <GMSMapViewDelegate>.

Declare property in your .h

@property (nonatomic, retain) GMSMapView *gMapView;

In viewDidLoad()

GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:appDelegate.currentLoc.coordinate.latitude longitude:appDelegate.currentLoc.coordinate.longitude zoom:6];
self.gMapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
self.gMapView.delegate = self;
self.gMapView.myLocationEnabled = YES;
self.gMapView.mapType = kGMSTypeSatellite;
self.view = self.gMapView;

GMSMarker *curLocation = [[GMSMarker alloc] init];
curLocation.title = @"Current Location";
curLocation.appearAnimation = kGMSMarkerAnimationPop;
curLocation.position = CLLocationCoordinate2DMake(appDelegate.currentLoc.coordinate.latitude, appDelegate.currentLoc.coordinate.longitude);
curLocation.map = self.gMapView;

Where you have added multiple markers to map.

for(int i=0;i<[latLongArr count];i++)
{
   GMSMarker *marker = [[GMSMarker alloc] init];
   marker.position = CLLocationCoordinate2DMake([[(NSDictionary *)[latLongArr objectAtIndex:i] valueForKey:@"Latitude"] doubleValue], [[(NSDictionary *)[latLongArr objectAtIndex:i] valueForKey:@"Longitude"] doubleValue]);
   marker.appearAnimation = kGMSMarkerAnimationPop;
   marker.title = @"Title";
   marker.snippet = @"Sub title";
   marker.map = self.gMapView;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...