i'm trying to create a building navigation application similar to http://navigationapps.com/wp-content/uploads/2010/10/point-inside-2.png . I'm planning on displaying the building floor plan image as a UIImageView and using CoreLocation to get the longitude and latitude coordinates for the pin pointer. The next step - which im also stuck on, how do i plot the point on the image? - i am already able to retrieve the users lat and lot coordinates ..
Well, you firstly a UIImageView to display some sort of icon for the pin pointer, and then a function to transform lat/long coordinates into the position on your image.
Assuming your building floor plan image is aligned with north at the top,
- (CGPoint)plotPointOfLocation:(CLLocationCoordinate2D)location {
CLLocationCoordinate2D topLeftCoordinate = CLLocationCoordinate2DMake(/* the lat/long of the top-left of the plan image */);
CLLocationCoordinate2D bottomRightCoordinate = CLLocationCoordinate2DMake(/* the lat/long of the bottom-right of the plan image */);
CGSize planImageSize = CGSizeMake(/* the size of the plan image, as drawn on the screen, in pixels */);
CLLocationDegrees latSpan = bottomRightCoordinate.latitude - topLeftCoordinate.latitude;
CLLocationDegrees longSpan = bottomRightCoordinate.longitude - topLeftCoordinate.longitude;
CLLocationCoordinate2D offsetLocation = CLLocationCoordinate2DMake(location.latitude - topLeftCoordinate.latitude,
location.longitude - topLeftCoordinate.longitude);
CGPoint ret = CGPointMake((offsetLocation.latitude / latSpan) * planImageSize.width,
(offsetLocation.longitude / longSpan) * planImageSize.height);
return ret;
}
Then just set your pin UIImageView 'center' property to the value returned from this method.
Related
I am Trying to Resize MkCircle Through finger touch. How can I start this? can any one suggest me?
Thanks for advance.
I draw the custom circle above the map view with same radius look like circle.
Finding edge point of MKcirlce
convert center point CLLocationCoordinate2D to MKMapPoint
MKMapPoint centerMapPoint = MKMapPointForCoordinate(centerCoordinate)
Calculate MKMapSize of the circle and get width
double circleMapSize = mkCircle.boundingMapRect.size.width
3.create a point on the perimeter of the circle.
MKMapPoint leftMostPoint = MKMapPointMake(centerMapPoint.x - circleMapSize/2.0, centerMapPoint.y)
Now get CLLocationCoordinate2D point from leftMostPoint
CLLocationCoordinate2D leftMostCoordinateOnCircle = MKCoordinateForMapPoint(leftMostPoint)
So now i have center point and edge point then convert it to (x,y) of ios coordinates.
CGPoint myPoint = [self.MapView convertCoordinate:originalCoordinate toPointToView:self.MapView];
CGPoint edgePoint = [self.MapView convertCoordinate:originalCoordinate toPointToView:self.MapView];
then i calculate distance b/w those two points using pythagoras theorem
int distnce1=sqrt(powf(edgePoint.x- myPoint.x, 2) + powf(edgePoint.y-myPoint.y, 2));
int finalWidth=2*distnce1;
Then i set distance to custom circle width and height
after pich Gesture end again i try to get edge lattitude and longitude of custom circle by using circle maths
float x = circleView.frame.size.width/2 * sin(0)+circleView.center.x;
float y = circleView.frame.size.width/2 * cos(0)+circleView.center.y;
convert x,y into coordinates
CLLocationCoordinate2D select_coordinate = [self.MapView convertPoint:point toCoordinateFromView:self.view];
then i find distance b/w new coordinates and center point of MKCircle then change the radius of MKCirlce equal to Distance.
One approach which might be simplest is to actually draw the circle yourself above the map view, on a separate transparent view which responds to touch events. Then, when drawing is complete, hide that view but add an actual MKCircle to the map view with the same graphical settings.
Typically it's easier to handling drawing/gestures outside of the map view for things like this.
Is there any good code which implements auto scrolling of MKMapView when dragging pin?
The effect I'm trying to achieve is a map scrolling when I drag the pin and reach edges of the map. When I move pin out of the edges I expect scrolling to stop and when I drop it, map shell move until pin reaches center of the screen.
I know how to center map on chosen location, but I'm not really have an idea how to scroll it while dragging pin.
It would really help if someone could just direct me to a logic of the way how to implement it.
MKMapRect mapRect = [self.mapView visibleMapRect];
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(42.777126,-76.113281);
MKMapPoint point = MKMapPointForCoordinate(coordinate);
mapRect.origin.x = point.x - mapRect.size.width * 0.3;
mapRect.origin.y = point.y - mapRect.size.height * 0.70;
[self.mapView setVisibleMapRect:mapRect animated:YES];
I am using MKMapView and showing particular point on that map.
There is a button in my screen for finding My Location.now i want that when i click on MyLocation button both the point(My location and another point for particular location) should come in single screen .
****it should be like like : ****
------------------------------
so i have to calculate distance between myLocation (blue) and particular point (Red)
Any help please ?
Something Similar ...
//create new region and set map
CLLocationCoordinate2D coord = {latitude: yourLocation.latitude, longitude: yourLocation.longitude};
MKCoordinateSpan span = MKCoordinateSpanMake(fabs(otherLocation.lattitude), fabs(otherLocation.longitude));
MKCoordinateRegion region = {coord, span};
[self.mapView setRegion:region];
If you want to show Direction in google maps you need current latitude & longitude and particular place lat & long,
Within Button touch action
{
NSURL *strlist = [NSURL URLWithString:[NSString stringWithFormat:#"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f",c_lat,c_long,lat,long]];
here //c_lat = current latitude
//c_long = current longitude
//lat = Particular place latitude
//long = Particular place longitude
[[UIApplication sharedApplication]openURL:strlist];
}
If you want to show direction map within app,
Refer this tut
Link for Direction Tut
MKMapView returns an invalid region under iOS 5 -- latitude + latitudeDelta/2 is over 100 and shouldn't be over 90.
Has anyone seen this problem?
Steps to Reproduce:
Create an MKMapView
Log the mapView.region from within the regionDidChangeAnimated delegate method
Zoom out the map as far as possible and drag it to the right, so the view is scrolled to the top/left
Expected Results:
In iOS 4, the mapView.region is reasonable:
lat=2.202047 lon=-67.500000 latDelta=165.698164 lonDelta=225.000000
In iOS 5, however, the mapView.region is out of bounds:
lat=17.978733 lon=-67.500000 latDelta=165.698164 lonDelta=225.000000
Latitudes should be within the -90 to 90 range. However, in iOS 5, lat + latDelta/2 is 100.827815. That is not possible. While I can clamp the values at +/- 90, the offset difference is causing problems with our overlays.
Regression:
Does not happen in iOS 4.3. Happens regularly in iOS 5. Screen dumps of the map views look identical even though the center latitude is 15 degrees off.
Notes:
Project file and screen dumps can be downloaded here.
This seems to be an adequate workaround. Rather than reading the mapView.region property, call this method instead:
#implementation MKMapView(fixedRegion)
-(MKCoordinateRegion) fixedRegion_
{
// this call is broken on iOS 5, as is the region property, so don't use them
// return( [self convertRect:self.bounds toRegionFromView:self] );
CLLocationCoordinate2D topLeft = [self convertPoint:CGPointZero toCoordinateFromView:self];
CLLocationCoordinate2D bottomRight = [self convertPoint:CGPointMake(self.bounds.size.width, self.bounds.size.height) toCoordinateFromView:self];
MKCoordinateRegion region;
region.center.latitude = (topLeft.latitude + bottomRight.latitude)/2;
region.center.longitude = (topLeft.longitude + bottomRight.longitude)/2;
region.span.latitudeDelta = fabs( topLeft.latitude - bottomRight.latitude );
region.span.longitudeDelta = fabs( topLeft.longitude - bottomRight.longitude );
return region;
}
#end
Now one could argue (correctly!) that this code isn't 100% correct either because the center value of a Mercator projection in lon/lat isn't really halfway between the top and bottom, but since this matches iOS 4 functionality and keeps the values within the legal range for the map, it works for me.
By using the MKMapView+ZoomLevel category, you won’t have to bother setting the region at all.
here is very good tutorial on the same
http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/
or
http://mayurbirari.wordpress.com/2011/02/07/how-to-access-mkmapkit-in-iphone/
After you perform the zoom/pinch operation try to load the region in
-(void) mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
instead in
regionDidChangeAnimated.
hope this will help..:)
I have an OpenGL view overlaid on top of an MKMapView. My goal is to have the coordinates of the glView to have the same values of the MKMapPoints on the map. To do this I found the values for the MKMapPoint on the top left and the bottom right of the map.
CLLocationCoordinate2D coordinateTopLeft = [mapView convertPoint:CGPointMake(0, 0) toCoordinateFromView:mapView];
MKMapPoint pointTopLeft = MKMapPointForCoordinate(coordinateTopLeft);
CLLocationCoordinate2D coordinateBottomRight = [mapView convertPoint:CGPointMake(mapView.frame.size.width, mapView.frame.size.width) toCoordinateFromView:mapView];
MKMapPoint pointBottomRight = MKMapPointForCoordinate(coordinateBottomRight);
Then I set the right, left, bottom, and top coordinates of the glView to match these points accordingly.
glOrthof(pointTopLeft.x, pointBottomRight.x, pointBottomRight.y, pointTopLeft.y, -1, 1);
Unfortunately, this did not work. The triangles that I tried to draw did not work. I tested and the points that I was trying to draw were located in the correct domain.
Am I just misunderstanding how the glOrthof method works?
Have you tried flipping pointBottomRight.y and pointTopLeft.y? The OpenGL y-coordinate system is inverted from most commonly used conventions.