adding multiple pins on google map in ios - ios

I want to add multiple pins on a google map, while it is being loaded.
I have a list of Latitude and Longitude values of nearby locations.
How can I show all these locations on the map with a pin. I am using Google SDK for iOS.
I am using the following code, but it didn't work for me.
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];
}
I have tried enough to search for it but there isn't enough documentation to answer my question.
Thanks in advance for the help.

This works for me:
for(GMSMarker*marker in array)
{
GMSMarker *mkr= [[GMSMarker alloc]init];
[mkr setPosition:CLLocationCoordinate2DMake(<coord>)];
[mkr setAnimated:YES];
[mkr setTitle:<Title>];
[mkr setSnippet:<Snippet>];
[mkr setMap:mapView_];
}
Using latest Google Maps SDK.
Hope it helps

self.view = mapView_;
for(int i=0;i<[array count];i++)
{
GMSMarker *marker = [[GMSMarker alloc] init];
marker.animated=YES;
marker.position = CLLocationCoordinate2DMake(latitude,longitude);
marker.title = #"name";
marker.snippet = #"snippet";
marker.map = mapView_;
}
This worked for me!

For swift we can use
for i in 0..<array.count() {
var marker = GMSMarker()
marker.animated = true
marker.position = CLLocationCoordinate2DMake(latitude, longitude)
marker.title = "name"
marker.snippet = "snippet"
marker.map = mapView_
}

Related

I am trying to display multiple markers and a line between them on google map

for (int i=0; i<self.busRoutesArr.count-1; i++)
{
NSString *lat = [self.latArr objectAtIndex:i];
NSString *lon = [self.longArr objectAtIndex:i] ;
double lt=[lat doubleValue];
double ln=[lon doubleValue];
NSLog(#"%f, %f",lt,ln);
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:lt
longitude:ln
zoom:60];
GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectMake(10, 100, 250, 250) camera:camera];
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = camera.target;
marker.snippet = #"Hello World";
marker.map = mapView;
mapView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:mapView];
}
Only 1 marker being displayed. I want to display all the markers and a line connecting them
The following code is for displaying all markers in the map.
for(int i=0;i<[array count];i++) {
GMSMarker *marker = [[GMSMarker alloc] init];
marker.animated=YES;
marker.position = CLLocationCoordinate2DMake(latitude,longitude);
marker.title = #"name";
marker.snippet = #"snippet";
marker.map = mapView;
}

Annotations is not shown on googlemap

Annotations are not shown on map.what could be the reason ? pls help.i cannot see any Pin or Annotations on the map,it is showing area near portugal,spain etc.
addressArray = dictionary[#"data"][#"centers"];
NSArray *latitude =[dictionary[#"data"][#"centers"]valueForKey:#"lat"];
NSArray *longitude=[dictionary[#"data"][#"centers"]valueForKey:#"lng"];
NSString *cname=[dictionary[#"data"][#"centers"]valueForKey:#"category_name"];
loaderView.hidden = true;
NSLog(#"%#",latitude);
NSLog(#"%#",longitude);
NSLog(#"%#",cname);
UIImage *pinImage = [UIImage imageNamed:#"map_gym"];
GMSMarker *mark=[[GMSMarker alloc]init];
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake([[latitude objectAtIndex:0] doubleValue] ,[[longitude objectAtIndex:0] doubleValue]);
NSLog(#"%#",[latitude objectAtIndex:0]);
NSLog(#"%#",[longitude objectAtIndex:0]);
mark.position =coord;
mark.title=cname;
mark.icon=pinImage;
mark.infoWindowAnchor = CGPointMake(0.5, 0.25);
mark.groundAnchor = CGPointMake(0.5, 1.0);
mark.map=_mapView;
I think the coordinates of the marker is not properly implemented. Take a look at the code sample in Google's Document.
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(10, 10);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
marker.title = #"Hello World";
marker.map = mapView_;
Try adjusting your code to this.
E.g.
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake([[latitude objectAtIndex:0] doubleValue] ,[[longitude objectAtIndex:0] doubleValue]);
GMSMarker *marker = [GMSMarker markerWithPosition:coord];
marker.title = #"Hello World";
marker.map = mapView_;

Display multiple locations on google map. IOS/Objective-C

I'm using google maps sdks to display locations on IOS app that i'm working on. Displaying single location works fine, but when i try to display multiple locations on a single map it gives a white screen. I have a restaurantsData object that consists of information pertaining to an array of restaurants (along with latitude and longitude for each). Now when try below code it gives a white screen. This is my first time using Google maps for IOS, please help me out.
Restaurant *res = [[Restaurant alloc] init];
for (res in _restaurantData) {
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude: [res.objectLocation.lat doubleValue]
longitude: [res.objectLocation.lng doubleValue] zoom: 5];
GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
GMSMarker *marker = [ [GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake([res.objectLocation.lat doubleValue], [res.objectLocation.lng doubleValue]);
marker.appearAnimation = kGMSMarkerAnimationPop;
marker.icon = [UIImage imageNamed:#"fork_filled"];
marker.map = mapView;
}
Your loop must be like
for (Restaurant *res in _restaurantData) {
GMSMarker *marker = [ [GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake([res.objectLocation.lat doubleValue], [res.objectLocation.lng doubleValue]);
marker.appearAnimation = kGMSMarkerAnimationPop;
marker.icon = [UIImage imageNamed:#"fork_filled"];
marker.map = mapView;
}

fitBounds in GoogleMaps SDK for ios does not fit

I want use fitBounds method in GoogleMaps for ios SDK and view does not fit. My varaibles are all right (path, arrays, etc...) because I can see polyline with markers on map. The only thing that does not work is fit to view. Where I have made mistake? thank you.
// Create a 'normal' polyline.
GMSPolyline *polyline = [[GMSPolyline alloc] init];
GMSMutablePath *path = [GMSMutablePath path];
locationInfoArray = [LocationInfoMemoryManager loadLocationDataWithPath:_locationInfoPathString];
for (int i=0; i<locationInfoArray.count; i++) {
LocationInfo* locationInfo = locationInfoArray[i];
CLLocationCoordinate2D locationPoint = {locationInfo.latitude, locationInfo.longitude};
[path addCoordinate:locationPoint];
}
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:[locationInfoArray[0] latitude]
longitude:[locationInfoArray[0] longitude]
zoom:5 ];
mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
polyline.path = path;
polyline.strokeColor = [UIColor blueColor];
polyline.strokeWidth = 10.f;
polyline.zIndex = 15; // above the larger geodesic line
polyline.map = mapView;
GMSMarker *startMarker = [[GMSMarker alloc] init];
startMarker.title = #"Start";
startMarker.snippet = #"Info will be here";
startMarker.position = CLLocationCoordinate2DMake([[locationInfoArray firstObject] latitude], [[locationInfoArray firstObject] longitude]);
startMarker.map = mapView;
startMarker.flat = NO;
//sydneyMarker.rotation = 30.0;
mapView.selectedMarker = startMarker;
GMSMarker *finishMarker = [[GMSMarker alloc] init];
finishMarker.title = #"Finish";
finishMarker.snippet = #"Info will be here";
finishMarker.position = CLLocationCoordinate2DMake([[locationInfoArray lastObject] latitude], [[locationInfoArray lastObject] longitude]);
finishMarker.map = mapView;
finishMarker.flat = NO;
mapView.selectedMarker = finishMarker;
//Here is probably problem
GMSCoordinateBounds *bounds; = [[GMSCoordinateBounds alloc] initWithPath:path];
GMSCameraUpdate *update = [GMSCameraUpdate fitBounds:bounds withPadding:20];
[mapView moveCamera:update];
self.view = mapView;
Is this code in loadView or viewDidLoad? Based on these earlier questions, I think camera updates only work correctly from viewWillAppear:
GMSCameraUpdate zooming out to the max distance rather than around a path
Fit bounds not working as expected

Google Maps SDK for IOS markers are being overwritten

I am having problems plotting multiple markers with Google Maps SDK for iOS (ver. 1.5.0). I am new to objective c (using Xcode ver 4.6.3) and the Google Maps SDK so I may be missing something obvious. Also I'm using iOS 6.1 simulator. I'm trying to learn by doing.
I have spent several days searching and have found several threads that have dealt with this topic, but none of the solutions work for me. The problem that I'm having is that my markers are overwriting each other. I created an NSArray, locations, that will have 4 columns and unknown rows. The columns are latitude, longitude, name, address.
for(int i=0;i<[locations count];i++){
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:40.0823
longitude:-74.2234
zoom:7];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
self.view = mapView_;
mapView_.myLocationEnabled = YES;
mapView_.mapType = kGMSTypeHybrid;
mapView_.settings.myLocationButton = YES;
mapView_.settings.zoomGestures = YES;
mapView_.settings.tiltGestures = NO;
mapView_.settings.rotateGestures = NO;
NSString *lat = [[locations objectAtIndex:i] objectAtIndex:0];
NSString *lon = [[locations objectAtIndex:i] objectAtIndex:1];
double lt=[lat doubleValue];
double ln=[lon doubleValue];
NSString *name = [[locations objectAtIndex:i] objectAtIndex:2];
NSMutableArray *markersArray = [[NSMutableArray alloc] init];
GMSMarker *marker = [[GMSMarker alloc] init];
marker.appearAnimation=YES;
marker.position = CLLocationCoordinate2DMake(lt,ln);
marker.title = name;
marker.snippet = [[locations objectAtIndex:i] objectAtIndex:3];
marker.map = mapView_;
[markersArray addObject:marker];
}
I see something wrong that's possibly related. You're overwriting markersArray every time you iterate through the locations array in the for loop. Instantiate markersArray outside of the for loop.
Could you try to NSLog the coordinates of each marker you're trying to plot?
If the coordinates are the same, the marker should plot right on top of each other making it appear that markers are being overridden, but they're just on top of each other.
Log the count of the locations and markersArray after you're done to make sure they're equal to each as a quick check.
*Edit: I see your problem. You're overriding your MapView every time you iterate through your for loop.
Try something like this:
// Create a markersArray property
#property (nonatomic, strong) NSMutableArray *markersArray;
// Create a GMSMapView property
#property (nonatomic, strong) GMSMapView *mapView_;
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupMapView];
[self plotMarkers];
}
// Lazy load the getter method
- (NSMutableArray *)markersArray
{
if (!_markersArray) {
_markersArray = [NSMutableArray array];
}
return _markersArray;
}
- (void)setupMapView
{
self.mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
self.view = self.mapView_;
self.mapView_.myLocationEnabled = YES;
self.mapView_.mapType = kGMSTypeHybrid;
self.mapView_.settings.myLocationButton = YES;
self.mapView_.settings.zoomGestures = YES;
self.mapView_.settings.tiltGestures = NO;
self.mapView_.settings.rotateGestures = NO;
// You also instantiate a GMSCameraPosition class, but you don't add it to your mapview
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:40.0823
longitude:-74.2234
zoom:7];
}
- (void)plotMarkers
{
// I don't know how you're creating your locations array, so I'm just pretending
// an array will be returned from this fake method
NSArray *locations = [self loadLocations];
for (int i=0; i<[locations count]; i++){
NSString *lat = [[locations objectAtIndex:i] objectAtIndex:0];
NSString *lon = [[locations objectAtIndex:i] objectAtIndex:1];
double lt=[lat doubleValue];
double ln=[lon doubleValue];
NSString *name = [[locations objectAtIndex:i] objectAtIndex:2];
// Instantiate and set the GMSMarker properties
GMSMarker *marker = [[GMSMarker alloc] init];
marker.appearAnimation=YES;
marker.position = CLLocationCoordinate2DMake(lt,ln);
marker.title = name;
marker.snippet = [[locations objectAtIndex:i] objectAtIndex:3];
marker.map = self.mapView_;
[self.markersArray addObject:marker];
}
}

Resources