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
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;
}
I am using Google Map iOS sdk. I added polyline on my google map using the following code. But now I want add arrow at the end of this line. How to solve this issue?
GMSMutablePath *path = [GMSMutablePath path];
[path addLatitude:-37.81319 longitude:144.96298];
[path addLatitude:-31.95285 longitude:115.85734];
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
polyline.strokeWidth = 3.f;
polyline.map = mapaView;
You can use GMSMarker to indicate endpoint of your polyline:
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(22.200000, 114.1333900);
marker.title = #"Endpoint";
marker.map = mapView;
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;
}
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
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_
}