Google maps in iOS doesn't show multiple pins - ios

I am using Google Maps SDK in iOS app.I am stuck at plotting multiple pins on map.This is how I am trying to plot the pins.And I used the exact same approach to show multiple pins using MapKit which worked fine.But no success with google Maps.
-(void)plotMembersOnMap
{
for (NSMutableDictionary *obj in self.jsonDictionary)
{
membersDict = [obj objectForKey:#"members"];
NSLog(#"Count member %d",[membersDict count]); // shows count
for (NSDictionary *obj in membersDict)
{
CLLocationCoordinate2D center;
NSString *latitudeString = [obj objectForKey:#"lat"];
NSString *longitudeString = [obj objectForKey:#"lng"];
double latitude = [latitudeString doubleValue];
double longitude = [longitudeString doubleValue];
center.latitude =latitude;
center.longitude = longitude;
NSString *userName = [obj objectForKey:#"pseudo"];
GMSMarker *marker = [[GMSMarker alloc] init];
marker.map = mapView_;
marker.position = CLLocationCoordinate2DMake(center.latitude, center.longitude);
customGoogleCallout.callOutTitleLabel.text = #"Member";
customGoogleCallout.callOutUserName.text = userName;
marker.icon = [UIImage imageNamed:#"marker_membre.png"];
}
}
}

just try to add lat and lon to an array then supply the marker.position.
have some loop for i and position is object at index[i].

this might help...
CLLocationCoordinate2D center = { [[obj objectForKey:#"lat"] floatValue] , [[obj objectForKey:#"lon"] floatValue] };
GMSMarker *marker = [GMSMarker markerWithPosition:center];

Try this ,
NSArray *latitudeString = [obj objectForKey:#"lat"];
NSArray *longitudeString = [obj objectForKey:#"lng"];
Instead of
NSString *latitudeString = [obj objectForKey:#"lat"];
NSString *longitudeString = [obj objectForKey:#"lng"];

Maybe because it's plotting the latest coordinate you assign to center. You should make new instance so the previous value will not replaced.

Related

How to Remove All markers from googlemap not mapview.clear (ios objective-c)

I am trying to remove existing all markers from google maps, we can do by map.clear but I don't want to remove everything(Polyline, polygons) on map, I just want to remove only markers
I am creating markers based on array count
-(void)annotationCreationForCoordinatesOfArray:(NSMutableArray *)array
{
for (int i=0; i<array.count; i++)
{
CLLocationCoordinate2D position = CLLocationCoordinate2DMake([[[array objectAtIndex:i] objectForKey:#"latitude"] doubleValue], [[[array objectAtIndex:i] objectForKey:#"longitude"] doubleValue]);
mark = [GMSMarker markerWithPosition:position];
NSString *annoNumber = [NSString stringWithFormat:#"%i",i];
mark.title = annoNumber;
mark.map = _mapView;
mark.tracksViewChanges = YES;
mark.draggable = YES;
mark.icon = [UIImage imageNamed:#"Mappin.png"];
}
}
Try this code its work for me.
Create global array
NSMutableArray *removalMarkerArray;
Now add all marker in global array
removalMarkerArray=[[NSMutableArray alloc]init];
-(void)annotationCreationForCoordinatesOfArray:(NSMutableArray *)array{
for (int i=0; i<array.count; i++){
CLLocationCoordinate2D position = CLLocationCoordinate2DMake([[[array objectAtIndex:i] objectForKey:#"latitude"] doubleValue], [[[array objectAtIndex:i] objectForKey:#"longitude"] doubleValue]);
mark = [GMSMarker markerWithPosition:position];
NSString *annoNumber = [NSString stringWithFormat:#"%i",i];
mark.title = annoNumber;
mark.map = _mapView;
mark.tracksViewChanges = YES;
mark.draggable = YES;
mark.icon = [UIImage imageNamed:#"Mappin.png"];
[removalMarkerArray addObject:mark];
}
}
Then where you want to clear all marker
for (GMSMarker *marker in removalMarkerArray ){
marker.map = nil;
}

Do not add annotations repeated

I am inserting some annotations that are coming from a json server, but I wanted to check if the annotation is already on the map, if so, does not add it again. For they are being added on each other , have someone help me solve this problem?
my code:
// adiciona produtos ao mapa
- (void)adicionaAnnotationsNoMapa:(id)objetos{
NSMutableArray *annotationsPins = [[NSMutableArray alloc] init];
for (NSDictionary *annotationDeProdutos in objetos) {
CLLocationCoordinate2D location;
AnnotationMap *myAnn;
myAnn = [[AnnotationMap alloc] init];
location.latitude = [[annotationDeProdutos objectForKey:#"latitude"] floatValue];
location.longitude = [[annotationDeProdutos objectForKey:#"longitude"] floatValue];
myAnn.coordinate = location;
myAnn.title = [annotationDeProdutos objectForKey:#"name"];
myAnn.subtitle = [NSString stringWithFormat:#"R$ %#",[annotationDeProdutos objectForKey:#"price"]];
myAnn.categoria = [NSString stringWithFormat:#"%#", [annotationDeProdutos objectForKey:#"id_categoria"]];
myAnn.idProduto = [NSString stringWithFormat:#"%#", [annotationDeProdutos objectForKey:#"id"]];
[annotationsPins addObject:myAnn];
}
[self.mapView addAnnotations:annotationsPins];
}
You can iterate through annotations already on MkMapView and see if they are already there:
NSArray * annotations = [self.mapView.annotations copy];
for (NSDictionary *annotationDeProdutos in objetos)
{
// Check if annotation in annotations are duplicate of annotationDeProdutos.
// You can match using name.
}
Or the other way if you only want to show annotations from single server call:
[self.mapView removeAnnotations: self.mapView.annotations];
// Now add from JSON response.
You can do this for each of your annotations:
if(![self.mapView.annotations containsObject: myAnn]) {
[self.mapView addAnnotations: myAnn];
}
I solved the problem with this code :
// adiciona produtos ao mapa
- (void)adicionaAnnotationsNoMapa:(id)objetos{
NSMutableArray *annotationsPins = [[NSMutableArray alloc] init];
for (NSDictionary *annotationDeProdutos in objetos) {
CLLocationCoordinate2D location;
AnnotationMap *myAnn;
myAnn = [[AnnotationMap alloc] init];
location.latitude = [[annotationDeProdutos objectForKey:#"latitude"] floatValue];
location.longitude = [[annotationDeProdutos objectForKey:#"longitude"] floatValue];
myAnn.coordinate = location;
myAnn.title = [annotationDeProdutos objectForKey:#"name"];
myAnn.subtitle = [NSString stringWithFormat:#"R$ %#",[annotationDeProdutos objectForKey:#"price"]];
myAnn.categoria = [NSString stringWithFormat:#"%#", [annotationDeProdutos objectForKey:#"id_categoria"]];
myAnn.idProduto = [NSString stringWithFormat:#"%#", [annotationDeProdutos objectForKey:#"id"]];
if (self.mapView.annotations.count <1) {
[annotationsPins addObject:myAnn];
} else {
__block NSInteger foundIndex = NSNotFound;
[annotationsPins enumerateObjectsUsingBlock:^(AnnotationMap *annotation, NSUInteger idx, BOOL *stop) {
CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:location.latitude longitude:location.longitude];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:annotation.coordinate.longitude];
if ([loc1 distanceFromLocation:loc2] <= 1.0f) {
foundIndex = idx;
*stop = YES;
}
}];
if (foundIndex != NSNotFound) {
[annotationsPins addObject:myAnn];
}
}
}
[self.mapView addAnnotations:annotationsPins];
}

How to plot the markers in google maps from a dictionay in ios?

In my app, I have done JSON parsing and I got the coordinates in the form of a dictionary, I want to use the coordinates angd plot it in the map,
I have using this
SBJsonParser *jsonParser = [SBJsonParser new];
NSArray *jsonData = (NSArray *) [jsonParser objectWithString:outputData error:nil];
for(int i=0;i<[jsonData count];i++)
{
NSDictionary *dict=(NSDictionary *)[jsonData objectAtIndex:i];
Nslog(#"%#",dict);
double la=[[dict objectForKey:#"latitude"] doubleValue];
double lo=[[dict objectForKey:#"longitude"] doubleValue];
CLLocation * loca=[[CLLocation alloc]initWithLatitude:la longitude:lo];
CLLocationCoordinate2D coordi=loca.coordinate;
marker=[GMSMarker markerWithPosition:coordi];
marker.snippet = #"Hello World";
marker.animated = YES;
marker.map = mapView;
}
it is printed as
[{"driver_id":"Tn1234sunil#gmail.com","username":"sunil","latitude":"0.000000000000000",
"longitude":"0.000000000000000"},
{"driver_id":"ma12marii#yahoo.com","username":"mari","latitude":"13.040720500000000",
"longitude":"80.243139600000000"}, {"driver_id":"45sabala#gmail.com","username":"balaji","latitude":"0.000000000000000",
"longitude":"0.000000000000000"}
Then, In my log, it is getting printed as
2014-01-04 10:55:48.121 MyTaxi[608:12e03] latitude : 0.000000
2014-01-04 10:55:48.121 MyTaxi[608:12e03] longitude : 0.000000
2014-01-04 10:55:48.122 MyTaxi[608:12e03] latitude : 13.040721
2014-01-04 10:55:48.122 MyTaxi[608:12e03] longitude : 80.243140
2014-01-04 10:55:48.122 MyTaxi[608:12e03] latitude : 0.000000
2014-01-04 10:55:48.123 MyTaxi[608:12e03] longitude : 0.000000
But, this doesnt works properly
Does any body have an idea how to plot these points to the google maps
try this one this might be helpful just create a for loop to your count ,increment it ...
NSDictionary *dict=(NSDictionary *)[jsonData objectAtIndex:i];
double la=[[dict valueForKey:#"latitude"] doubleValue];
double lo=[[dict valueForKey:#"longitude"] doubleValue];
NSMutableArray * latArray=[[NSMutableArray alloc]init];
NSMutableArray * longArray=[[NSMutableArray alloc]init];
[latArray addObject:[NSNumber numberWithDouble:la]];
[longArray addObject:[NSNumber numberWithDouble:lo]];
CLLocation * loca=[[CLLocation alloc]initWithLatitude:[[latArray objectAtIndex:i]doubleValue] longitude:[[longArray objectAtIndex:i]doubleValue]];
CLLocationCoordinate2D coordi=loca.coordinate;
GMSMarker *marker= [[GMSMarker alloc] init];
marker=[GMSMarker markerWithPosition:coordi];
marker.position = CLLocationCoordinate2DMake([[latArray objectAtIndex:i]doubleValue], [[longArray objectAtIndex:i]doubleValue]);
marker.snippet = #"Hello World";
marker.animated = YES;
marker.map = mapView;
I think , You have to alloc Marker in for loop,
right now you are creating only one marker,
for(int i=0;i<[jsonData count];i++)
{
NSDictionary *dict=(NSDictionary *)[jsonData objectAtIndex:i];
double la=[dict valueForKey:#"latitude" doubleValue];
double lo=[dict valueForKey:#"longitude" doubleValue];
CLLocation * loca=[[CLLocation alloc]initWithLatitude:la longitude:lo];
CLLocationCoordinate2D coordi=loca.coordinate;
GMSMarker *marker= [[GMSMarker alloc] init];
marker=[GMSMarker markerWithPosition:coordi];
marker.snippet = #"Hello World";
marker.animated = YES;
marker.map = mapView;
.....
}

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];
}
}

MKPointAnnotation Expression is not assignable

So I have the following code block, which is supposed to iterate over an array of JSON objects and place MKPointAnnotations on a map:
for(id jsonObject in dataArray)
{
NSLog(#"%d",[dataArray count]);
NSDictionary* jsonDictionary = jsonObject;
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
NSString *lat = [jsonDictionary objectForKey:#"latitude"];
NSString *lon = [jsonDictionary objectForKey:#"longitude"];
point.coordinate.latitude = [lat doubleValue];
point.coordinate.longitude = [lon doubleValue];
[map addAnnotation:point];
}
However, the two lines:
point.coordinate.latitude = [lat doubleValue];
point.coordinate.longitude = [lon doubleValue];
are giving me an "Expression is not Assignable" error. I can't for the life of me figure it out. I've tried to make a CLLocationCoordinate2D object and assigning that, but it doesn't work either.
This should work:
CLLocationCoordinate2d coordinate = ...
MKPointAnnotation* annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = coordinate;
[mapView addAnnotation:annotation];
It works in an existing app, just checked the code and the app.
Check this answer as well: https://stackoverflow.com/a/15162092/1032151

Resources