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;
}
Related
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 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_;
I want to display my spots NSArray *chSpot = [parser parseArray:responseObject];
which receives 3 objects , I want to show each location regarding to its lat and long
and find the nearest spot according to my location...I am using this code:
NSArray *chSpot = [parser parseArray:responseObject];
// GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:[locLat doubleValue]
// longitude:[locLong doubleValue]
// zoom:4];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:nil];
mapView_.myLocationEnabled = YES;
mapView_.settings.myLocationButton = YES;
for (int i=0; i<chSpot.count; i++)
{
ChargingSpots *spot = [chSpot objectAtIndex:i];
NSString *locLat = spot.LocationLat;
NSString *locLong = spot.LocationLong;
// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake([locLat doubleValue], [locLong doubleValue]);
marker.title = #"Amman";
marker.snippet = #"Jordan";
mapView_.delegate=self;
marker.appearAnimation = kGMSMarkerAnimationPop;
marker.icon = [UIImage imageNamed:#"car"];
marker.map = mapView_;
}
//[mapView_ setSelectedMarker:marker];
self.view = mapView_;
//
//
// CLLocation *locationA = [[CLLocation alloc] initWithLatitude:[locLat doubleValue] longitude:[locLong doubleValue]];
//
// CLLocation *locationB = [[CLLocation alloc] initWithLatitude:[locLat doubleValue] longitude:[locLong doubleValue]];
//
// CLLocationDistance distance = [ locationA distanceFromLocation:locationB];
//
// NSLog (#"%f",distance);
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[self.locationManager startUpdatingLocation];
In your locationManager's delegate's method locationManager:didUpdateLocations: you can get your current location and it provides the method to calculate distanceFromLocation:(const CLLocation *)location
CLLocationDistance distA = [currentLocation distanceFromLocation: locationA];
CLLocationDistance distB = [currentLocation distanceFromLocation: locationB]; //etc
I'm working on an app for iOS and i'm running into some trouble with the google maps sdk.
My app gets a list of coordinates and draws a polyline between them as well as start and finish markers. At the moment, this all occurs successfully. What i'm trying to do now is update the camera to contain the entire path. Here is my code currently:
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:[[sharedModel startNode] nodeLocation].latitude
longitude:[[sharedModel startNode] nodeLocation].longitude
zoom:18];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
GMSMutablePath *path = [GMSMutablePath path];
for (int i = 0; i < [results count]; i++)
{
[path addCoordinate:[[results objectAtIndex:i] nodeLocation]];
}
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
polyline.strokeWidth = 10;
polyline.map = mapView_;
GMSMarker *marker1 = [[GMSMarker alloc] init];
marker1 = [GMSMarker markerWithPosition:[[results objectAtIndex:0] nodeLocation]];//[[sharedModel startNode] nodeLocation]];
marker1.title = #"Start";
marker1.map = mapView_;
GMSMarker *marker2 = [[GMSMarker alloc] init];
marker2 = [GMSMarker markerWithPosition:[[results lastObject] nodeLocation]];
marker2.title = #"Finish";
marker2.map = mapView_;
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithPath:path];
GMSCameraUpdate *update = [GMSCameraUpdate fitBounds:bounds];
[mapView_ moveCamera:update];
self.view = mapView_;
When I run this, my markers and polyline all show up in the right locations but the map is zoomed out to the max distance it can go (although it is centered over the path). Any advice?
Is this code in loadView? Try maybe creating the map view in loadView, but updating the camera later, maybe in viewDidLoad or viewWillAppear.
Based on the answer to this question I'm thinking that maybe the camera update methods don't work properly from loadView:
Fit bounds not working as expected
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_
}