I'm trying to map a specific point in the mapview to my view. I am using the following code, but it returns some really high and wrong numbers.
CGPoint annPoint = [self.mapView convertCoordinate:coord toPointToView:self.view];
Does anyone can help me?
I found the error. The mapview should be loaded (completly?) before i can call the convertion method.
Related
I am working with mapkit in Xcode 5.1 and am trying to display the map scale in regionDidChangeAnimated. I have no idea now to accomplish this though. I tried to look around and was unsuccessful. Any ideas?
EDIT:
-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
CGPoint nePoint = CGPointMake(self.mapView.bounds.origin.x + mapView.bounds.size.width, mapView.bounds.origin.y);
CGPoint swPoint = CGPointMake((self.mapView.bounds.origin.x), (mapView.bounds.origin.y + mapView.bounds.size.height));
CLLocationCoordinate2D neCoord;
neCoord = [self.mapView convertPoint:nePoint toCoordinateFromView:self.mapView];
CLLocationCoordinate2D swCoord;
swCoord = [self.mapView convertPoint:swPoint toCoordinateFromView:self.mapView];
CLLocationDistance distance = [neCoord distanceFromLocation:swCoord];
}
Any reason why I am getting an error with the last line, CLLocationDistance?
Use the methods that translate between the coordinates on the map and the points on the map view, such as convertPoint:toCoordinateFromView:. Use the edge points of your view for this.
Now you have the coordinates - you can calculate the distances between the points with CLLocation's distanceFromLocation:. You can make some assumptions about the width of your view based on the physical properties of, day an iPhone or iPad and calculate the scale.
I'm trying to set a custom starting region in an iOS app using MKMapView's setRegion. The app runs fine, and a map appears, but no matter what I try I can't get the region to change. I've tried many tutorials and solutions, but none are working. Here's my code:
-(void)viewDidAppear:(BOOL)animated{
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.001;
span.longitudeDelta = 0.001;
region.span = span;
region.center.latitude = 100;
region.center.longitude = 100;
[mapView setRegion:(region) animated:(TRUE)];
}
I have the MKMapView and Core Location frameworks added to the projects properly, I import MapKit.h, and I declare mapView, so I don't know why it's not working.
Thank you,
Jacob
It sounds like the map view's IBOutlet is not connected to the map view control in the xib/storyboard.
If it's not connected, the mapView variable will be nil and calling methods on it will do nothing.
In the xib/storyboard, right-click on View Controller and connect the mapView outlet to the map view control.
Additionally, although not necessary for just setting the region, also connect the map view's delegate outlet to the View Controller. This will be required if you later implement any map view delegate methods. If this is not done, the delegate methods won't get called.
Another separate point:
In your first code example, you are setting the region's center to 100, 100. Please note that this is an invalid coordinate. If the map view was actually connected, setting the center to this would have caused a crash with "Invalid Region". Latitude must be from -90 to +90 (longitude must be from -180 to +180).
By the way, the new coordinate you're trying in the code posted in the comment (26, 80) is in India. Since you're setting the span to a relatively small value, you'll need to zoom out a lot to see this.
However it was set as answered, I will let here an example of what worked for me, when I had no clue why setRegion was not working at all. In my case the problem was the I've initiated the MKMapView without a frame, e.g.:
override func viewDidLoad() {
...
self.mapView = MKMapView() // Wrong!
...
self.mapView.setRegion(...) // Does not work!
}
It looks like that the whole region thing is calculated with regards the the initial (CGRect). It worked for me by doing:
override func viewDidLoad() {
...
self.mapView = MKMapView(frame: CGRect(0, 108, self.mapView.bounds.width, self.mapView.bounds.height))
self.mapView.setRegion(...) // Oh, it works!
}
This problem was described here too.
i've got a little Problem on setting up a MKMapView.
I really just want that the complete map fits into the MkMapView-Region specified inside
the Storyboard.
But it seems that it is not possible to zoom or scale the map, with the result that the whole world is visible.
Here is what it looks like:
So if i want to show the connected annotations at the same time, it wont work if they are to far away from each other. Cause it is not possible to make the whole map fit.
Maybe i am missing a step on setting the MapView up but i dont get it.
Any Ideas what i am doing wrong ?
You can use the following method of MKMapView
- (void)setRegion:(MKCoordinateRegion)region animated:(BOOL)animated
to set center point and zoom level.
Hope this will help you.
If there is only 1 annotation, then lets have a good zoom say 10x. Else zoom till we just see all the annotations ( + some extra padding :)
if ([currentMapView.annotations count] == 1) {
region.span.latitudeDelta = 0.02;
region.span.longitudeDelta = 0.02;
} else {
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1; // Add a little extra space on the sides
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1; // Add a little extra space on the sides
}
I am trying to Geo-fence using Google Map for iPhone app.
A lot of tutorials can be found for MKMapView. But can't find for the GMSMapView.
The basic thing is how to convert the screen coordinate (x,y) to the MapCoordinate lat/lng.
Is there any API available for Google Map in iOS for that conversion?
Thanks
You can use something like this:
GMSMapView* mapView = ...;
CGPoint point = ...;
...
CLLocationCoordinate2D coordinate =
[mapView.projection coordinateForPoint: point];
UPDATE:
The comments on the projection property in GMSMapView.h are:
/**
* The GMSProjection currently used by this GMSMapView. This is a snapshot of
* the current projection, and will not automatically update when the camera
* moves. The projection may be nil while the render is not running (if the map
* is not yet part of your UI, or is part of a hidden UIViewController, or you
* have called stopRendering).
*/
#property (nonatomic, readonly) GMSProjection *projection;
Therefore you can only access the .projection property after the map has rendered. It will be nil if you try to access it during loadView or viewDidLoad.
I don't know if there is a better way to tell if the map has been rendered, but I noticed that the mapView:didChangeCameraPosition: method is called once after the map view is first displayed, and that the map's projection property is valid there.
So, in your view controller's header, add GMSMapViewDelegate:
#interface ViewController : UIViewController <GMSMapViewDelegate>
When you allocate the map view, assign the delegate:
_map = [GMSMapView mapWithFrame: CGRectMake(0, 0, width, height) camera: camera];
_map.delegate = self;
[self.view addSubview: _map];
Then add the delegate method:
- (void)mapView: (GMSMapView*)mapView
didChangeCameraPosition: (GMSCameraPosition*)position
{
CGPoint point = CGPointMake(x, y);
CLLocationCoordinate2D coordinate =
[_map.projection coordinateForPoint: point];
}
Note that mapView:didChangeCameraPosition: is called every time the user changes the camera, so you'd probably need to use a flag, so that you only do your calculations the first time mapView:didChangeCameraPosition: is called.
No need to convert x,y to lat,long.
GMSCircle *fence = [GMSCircle circleWithPosition:locationCord radius:fenceRadius];
[fence setFillColor:[UIColor colorWithRed:102.0/255 green:178.0/255 blue:255.0/255 alpha:0.3]];
[fence setZIndex:100];
[fence setMap: _map];
Add this code when you are making GMSMapView and geo fence will be shown with your location marker.
i`m using MapKit framework and i want to ask you about something :
+ (NSUInteger)zoomLevelForMapRect:(MKMapRect)mRect withMapViewSizeInPixels:(CGSize)viewSizeInPixels
{
NSUInteger zoomLevel = MAXIMUM_ZOOM; // MAXIMUM_ZOOM is 20 with MapKit
MKZoomScale zoomScale = mRect.size.width / viewSizeInPixels.width; //MKZoomScale is just a CGFloat typedef
double zoomExponent = log2(zoomScale);
zoomLevel = (NSUInteger)(MAXIMUM_ZOOM - ceil(zoomExponent));
return zoomLevel;
}
this method..how can i know the value of mRect and viewSizeInPixels parameters to be able to call it?? thx in advance :)
The map view's current MKMapRect is the visibleMapRect property and the view size would be in frame.size (since MKMapView is a subclass of UIView) so the method would be called using something like:
NSUInteger zoomLevel = [UtilityClass
zoomLevelForMapRect:mapView.visibleMapRect
withMapViewSizeInPixels:mapView.frame.size];
UtilityClass is whatever class that method is in and replace mapView with whatever you map view is actually named.
By the way, the MapKit Framework Reference and the Location Awareness Programming Guide are worth looking at.