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
Related
I want to show my marker on the center of the map but it shows on the top left.
Below is my code implementation:
- (void)viewDidLoad {
[super viewDidLoad];
NSDictionary *userLoc=[kNSUserDefaults objectForKey:#"userLocation"];
mapView_.delegate = self;
CGFloat lat = [[userLoc objectForKey:#"lat"] floatValue];
CGFloat lon = [[userLoc objectForKey:#"long"] floatValue];
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(lat, lon);
mapView_.myLocationEnabled = YES;
mapView_.settings.myLocationButton = YES;
mapView_.mapType = kGMSTypeHybrid;
marker.map = mapView_;
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:lat longitude:lon zoom:17];
[mapView_ animateToCameraPosition:camera];
[mapView_ setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
mapView_.settings.compassButton = YES;
mapView_.mapType = kGMSTypeHybrid;
mapView_.settings.myLocationButton = YES;
mapView_.delegate = self;
mapView_.trafficEnabled = YES;
}
Not able to understand what's wrong in my code.
Thanks in advance!
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;
}
Initialy created poly line and added some path to it . after creating this polyline i need add some other points based on gps location and drawing polyline again .is that redraw again the path.
_path = [GMSMutablePath path];
[_path addLatitude:12.9716 longitude:77.5946]; // bangalore
[_path addLatitude:13.3710 longitude:76.6413]; // Fiji
[_path addLatitude:15.3173 longitude:75.7139]; // Hawaii
[_path addLatitude:15.3647 longitude:75.1240]; // Mountain View
GMSMarker *marker1 = [[GMSMarker alloc] init];
marker1.position = CLLocationCoordinate2DMake(12.9716, 77.5946);
marker1.title = #"Bangalore";
marker1.groundAnchor = CGPointMake(0.2, 0.9);
marker1.appearAnimation = kGMSMarkerAnimationPop;
marker1.icon = [UIImage imageNamed:#"Flag Filled -50.png"];
marker1.snippet = #"India";
marker1.map = _mapView;
GMSMarker *marker2 = [[GMSMarker alloc] init];
marker2.position = CLLocationCoordinate2DMake(13.3710, 76.6413);
marker2.title = #"Tumkur";
marker2.groundAnchor = CGPointMake(0.3, 0.9);
marker2.snippet = #"India";
marker2.icon = [UIImage imageNamed:#"Flag Filled -50.png"];
marker2.map = _mapView;
GMSMarker *marker3 = [[GMSMarker alloc] init];
marker3.position = CLLocationCoordinate2DMake(15.3173, 75.7139);
marker3.title = #"Mysore";
marker3.groundAnchor = CGPointMake(0.3, 0.9);
marker3.snippet = #"India";
marker3.icon = [UIImage imageNamed:#"Flag Filled -50.png"];
marker3.map = _mapView;
GMSMarker *marker4 = [[GMSMarker alloc] init];
marker4.position = CLLocationCoordinate2DMake(15.3647, 75.1240);
marker4.title = #" Hubli";
marker4.groundAnchor = CGPointMake(0.3, 0.9);
marker4.snippet = #"India";
marker4.icon = [UIImage imageNamed:#"Flag Filled -50.png"];
marker4.map = _mapView;
marker4.rotation = 180;
CLLocationCoordinate2D circleCenter1 = CLLocationCoordinate2DMake(15.3647, 75.1240);
GMSCameraUpdate *updatedCamera = [GMSCameraUpdate setTarget:circleCenter1 zoom:10];
[_mapView animateWithCameraUpdate:updatedCamera];
_polyline = [GMSPolyline polylineWithPath:_path];
_polyline.strokeColor = [UIColor blueColor];
_polyline.strokeWidth = 1.f;
_polyline.map = _mapView;
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
static CLLocation *lastKnownLocation = nil;
if(lastKnownLocation == nil) lastKnownLocation = [locations lastObject];
else
{
CLLocation *currentLoc = [locations lastObject];
// Forming GMSPath from User's last location to User's current location.
GMSMutablePath *path = [GMSMutablePath new];
[path addCoordinate:[lastKnownLocation coordinate]];
[path addCoordinate:[currentLoc coordinate]];
GMSPolyline *polyLine = [GMSPolyline polylineWithPath:path];
polyLine.strokeColor = [UIColor redColor];
polyLine.strokeWidth = 2.f;
polyLine.map = googleMap;
// Saving current loc as last known loc,so that we can draw another GMSPolyline from last location to current location when you recieved another callback for this method.
lastKnownLocation = currentLoc;
}
}
Above code will draws path on google map when app got new location update(from second time onward).
Hope that helps,
Get back to me if you need some more deep info.
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 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];
}
}