I have a strange issue that started happening recently, and i cannot figure out why.
When I initialise my map with this piece of code, everything works perfectly:
_mapView = [[SKMapView alloc] initWithFrame:CGRectMake( 0.0f, 0.0f, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame) )];
[self.view insertSubview:_mapView atIndex:0];
_mapView.settings.rotationEnabled = NO;
_mapView.settings.displayMode = SKMapDisplayMode2D;
[SKRoutingService sharedInstance].mapView = _mapView;
But, when I want to zoom in to a specific region on the map, things go funky and the screen is just blue.
Code:
[self.locationManager startUpdatingLocation];
_lattitude = self.locationManager.location.coordinate.latitude;
_longitude = self.locationManager.location.coordinate.longitude;
SKCoordinateRegion region;
region.center = CLLocationCoordinate2DMake(_latitude, _longitude);
region.zoomLevel = 14;
_mapView.visibleRegion = region;
[self.locationManager stopUpdatingLocation];
Screenshot:
Now, I'd like to mention that the blue screen only appears on the first launch. If i close the app and start it again, the map will be displayed properly (Both in Emulator and on the real device).
I tracked which piece of code is causing this, and it's this one:
_mapView.visibleRegion = region;
How can I fix this?
Probably the location manager has not yet received its position and gives you 0,0 which is somewhere in front of Africas coast. Ocean = blue.
You will need to setup a delegate for the location manager and wait for a positive fix on your position and then display the region in your map. In your code above you immediately request the location after starting the location manager.
Related
I am trying to load a Google Map into my application within a subview. When I initialize my GMSMapView in iOS, the current location is in the top left corner of the map instead of the center screen. This is true if I were to press the "My location" button as well.
This seems to be an issue when running on my phones but not on simulators. How do I get it so that it is set up correctly in the center?
I've tried rearranging code and loading the map differently. I think it might be due to Autolayout Constraints, as when I set mapView.translatesAutoresizingMaskIntoConstraints = true; then the location is in the center, but my view is messed up.
I currently have GMSMapView set up as the custom class of my view with auto layout constraints set up to resize it.
I load up my GPS class and set the frame with this.
gpsVC = [[GPSViewController alloc] init];
gpsVC.view.frame=CGRectMake(0, CGRectGetMaxY(self.segmentTopView.frame), CGRectGetWidth(self.mainView.frame), CGRectGetHeight(self.mainView.frame)-CGRectGetHeight(self.topView.frame)-CGRectGetMaxY(self.segmentTopView.frame));
[self.segmentView addSubview:gpsVC.view];
And in my GPSViewController I set up the map camera as follows:
self.mapView.myLocationEnabled = YES;
GMSCameraPosition *camera=[GMSCameraPosition cameraWithLatitude:myLocation.coordinate.latitude longitude:myLocation.coordinate.longitude zoom:[mapDefaultZoomLevel floatValue]];
self.mapView.camera=camera;
Is anyone else experiencing this, and did you find any solution to this problem?
Edit: Here are some images (Links because I can't put in images yet)
https://imgur.com/FEMfwri
https://imgur.com/ySQio5b
https://imgur.com/9kijxbT
for anyone wondering I eventually found a workaround solution to the problem I was having.
I believe that there was a view frame issue with auto-layout that was happening when my GPSViewController initialized. I avoided this issue by setting up my Google Map after my view has been setup. I removed the GMSMapView from the custom class of my UIView and instead programmatically created the map manually and added it to the view.
In my MasterViewController.m:
gpsVC = [[GPSViewController alloc] init];
gpsVC.view.frame=CGRectMake(0, CGRectGetMaxY(self.segmentTopView.frame), CGRectGetWidth(self.segmentView.frame), CGRectGetHeight(self.segmentView.frame)-CGRectGetHeight(self.segmentTopView.frame));
[gpsVC setupMap];
In my GPSViewController.m (abridged):
-(void)setupMap{
GMSCameraPosition *camera=[GMSCameraPosition cameraWithLatitude:coordinate.latitude longitude:coordinate.longitude zoom:mapDefaultZoomLevel];
self.mapView = [GMSMapView mapWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) camera:camera];
self.mapView.myLocationEnabled = YES;
self.mapView.settings.myLocationButton = YES;
self.mapView.settings.compassButton = YES;
self.mapView.delegate=self;
[self.myView addSubview:self.mapView];
}
Looks like you are doing it correctly, the only thing I can't say for sure is whether you are centering your map correctly in the sub view. But here are some other map tricks you can try, if the problem is with the map:
[self.mapView clear];
// Set map view and display
double zoom = 14;
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:[myLocation.latitude doubleValue]
longitude:[myLocation.longitude doubleValue]
zoom:zoom];
// Initialize Map View
self.mapView.camera = camera;
self.mapView.myLocationEnabled = YES;
__block GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] init];
//You can try setting a couple of points east and west of your current location and including them in the bounds
CLLocation westLocation = myLocation;
westLocation.longitude += .005;
CLLocation eastLocation = myLocation;
eastLocation.longitude -= .005;
bounds = [bounds includingCoordinate:myLocation.position];
bounds = [bounds includingCoordinate:eastLocation.position];
bounds = [bounds includingCoordinate:westLocation.position];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
[self.mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:40.0f]];
} else {
[self.mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:100.0f]];
}
Same issue with you. I was delay init google mapview to test. And It works normally again.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self initGoogleMap];
});
I'm trying to set approx a 10 mile range around the current location of the user when my mapView loads.
Here is my Objective C code in viewDidLoad:
self.mapView = [[MKMapView alloc] initWithFrame:self.view.frame];
self.mapView.delegate = self;
self.mapView.showsUserLocation = YES;
[self.view addSubview:self.mapView];
//Set initial scope of map
CLLocationDistance mapWidith = 16000;
CLLocationDistance mapHeight = 16000;
CLLocationCoordinate2D userLocation = self.mapView.userLocation.coordinate;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation, mapHeight , mapWidith);
[self.mapView setRegion: region
animated:false];
When I run it's starting with the correct zoom (I think) but randomly centered in the atlantic ocean somewhere. Not sure if I'm accessing the userLocation coordinate correctly but it seems right. Im pretty new to MKMapView so Im struggling a bit.
The userLocation property on mapView isn't populated yet. You should implement the mapView:didUpdateUserLocation: method and after that point you can reliably use that property (or use the location sent to you).
I use a MKMapView to show my current location , I found that when i am doing nothing ,it will take me about 15+ seconds to see the blue circle and point in the map view ,but if i move the map after the map view is start to locating ,the blue circle and point will show immediately (will dont need 5 seconds now) ,what 's the diffrence between them ? can I short the time to show blue circle in coding ? thanks alot
i create the map
self.runMapView = [[[MKMapView alloc] initWithFrame:self.bounds] autorelease];
self.runMapView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
self.runMapView.showsUserLocation = YES;
runMapView.delegate = self;
and then :
- (void)mapView:(MKMapView *)mapView_ didUpdateUserLocation:(MKUserLocation *)userLocation
{
if (loc2 == nil)//loc 2 is the ivar i used to track my first location
{
if (CLLocationCoordinate2DIsValid(userLocation.location.coordinate))
{
self.loc2 = userLocation.location;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.location.coordinate, 2000, 2000);
[runMapView setRegion:region];
}
}
}
and i add some polylineview to the map,and when i add the mapview to the view ctrl 's view at start ,the mapview 's alpha is set to 0(because i have a button to decide show or hide mapview),i dont know if the map update location in back can cause this problem?
Create one variable for location manager,
You need to update location just below the mapview initialize like this:
self.runMapView = [[[MKMapView alloc] initWithFrame:self.bounds] autorelease];
self.runMapView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
self.runMapView.showsUserLocation = YES;
self.runMapView.delegate = self;
[self.locationManager startUpdatingLocation];
In iOS 5 this worked fine, and I was under the impression that everything from MKMapKit would continue working in much the same way. However, instead of zooming to my location and adding annotations that I have set up, it just shows North America. I am leaving the annotations out of the code snippet I have, and just putting in the initial code setup.
[mapView setMapType:MKMapTypeSatellite];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
mapView.delegate = self;
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = 32.385325 ;
region.center.longitude = -86.217442;
region.span.longitudeDelta = 0.005f;
region.span.latitudeDelta = 0.005f;
[mapView setRegion:region animated:YES];
Any thoughts on why it is not zooming to where I have it set?
I personally don't suspect an iOS 6 v iOS 5 issue. This code works fine for me in iOS 6. So the problem rests elsewhere.
In short, a problem with the mapView variable seems far more likely. Have you checked to see that mapView is not nil? There are all sorts of simple candidate issues:
Perhaps you manually declared class instance variable for a property (which you should not do ... simply define your property and let the compiler synthesize your instance variable for you, precisely to avoid this sort of possible confusion); or
Perhaps there was a failure to link up the IBOutlet, etc.
I'm trying to center my mapView on the user's location, but an exception is thrown that's caught by the AppDelegate before the mapView or view controller are even loaded.
mapView is an MQMapView
userLocation is assigned earlier from mapView.userLocation.location.coordinate
MQCoordinateSpan userSpan = MQCoordinateSpanMake(1000, 1000);
MQCoordinateRegion userRegion = MQCoordinateRegionMake(userLocation, userSpan);
[mapView setRegion:userRegion animated:true];
As far as I can make out from the MapQuest developer guide I'm calling setRegion correctly. Any idea what might be causing the exception?
I was having crashes with my iPhone4S and console revealed nan values for region. After trying about 7 different solutions from SO and various suggestions from Apple DTS, I solved it by eliminating the regionThatFits call. I simply used:
CLLocationDistance visibleDistance = 100000; // 100 kilometers
MKCoordinateRegion adjustedRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, visibleDistance, visibleDistance);
[_mapView setRegion:adjustedRegion animated:YES];
Apparently there is a problem with that regionThatFits method.