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
Related
I have set map view successfully in iPhone with current location with maximize zoom.
like,
myMapView.showsUserLocation = YES;
.....
span.latitudeDelta=0.0001;
span.longitudeDelta=0.0001;
...
Now i want to get complete area of visible portion of Map from its center point in any of measure like km,m,number etc.
any help..
Its really easy to do that:
MKMapRect visibleRect = [myMapView visibleMapRect];
Then there are some functions to measure the rectangle in meters such as:
CLLocationDistance MKMetersBetweenMapPoints(
MKMapPoint a,
MKMapPoint b
);
Take a look at Documentation and Sample Code
And try something like this:
MKCoordinateRegion yourRegion;
// Take a look at Lisbon Downtown
yourRegion.center = CLLocationCoordinate2DMake(38.715, -9.140);
yourRegion.span = MKCoordinateSpanMake(0.02, 0.02);
[self.mapView setRegion:yourRegion animated:YES];
I have a method that works to move the the map so that it follows the current user location. This works fine, but currently the zoom scale doesn't work how I want. I would like the user to be able to zoom out or zoom in on the map how they want and still have it follow the current location at that NEW zoom scale that the user just set on the map.
I tried to do the following but it doesn't work well:
/**
* Centers the map on the current location
*
* #version $Revision: 0.1
*/
- (void)centerMapOnLocation:(CLLocation *)location {
// Make a region using our current zoom level
CLLocationDistance latitude = mapView.region.span.latitudeDelta*100;
CLLocationDistance longitude = mapView.region.span.longitudeDelta*100;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location.coordinate, latitude, longitude);
[mapView setRegion:region animated:YES];
}//end
MKMapView Zoom and Region describes how to get the current view window.
When you're creating the new MKCoordinateRegion to set the new view to, use the constructor that allows you to specify the size:
UIKIT_STATIC_INLINE MKCoordinateRegion MKCoordinateRegionMake(
CLLocationCoordinate2D centerCoordinate,
MKCoordinateSpan span
);
You'll want to pass in the span from the current region and the centerCoordinate from the user location, which I'll take it you already got.
I have this code :
- (void)viewDidLoad
{
[super viewDidLoad];
CLLocationCoordinate2D userLocation = CLLocationCoordinate2DMake(48.9793946200, 2.4726272850);
CLLocationDistance dist1 = 636.9887048804;
CLLocationDistance dist2 = 900.8380655203;
CLLocationDistance dist = dist1;
[self.myMapView setRegion:MKCoordinateRegionMakeWithDistance(userLocation, dist, dist) animated:YES];
// TEST
// ------------------------------------------------------------
MKCoordinateRegion region = self.myMapView.region;
CLLocationDegrees lat = region.center.latitude;
CLLocationDegrees lon = region.center.longitude - region.span.longitudeDelta/2;
CLLocation *west = [[[CLLocation alloc] initWithLatitude:lat longitude:lon] autorelease];
NSLog(#"User location: lat : %.10lf long : %.10lf", userLocation.latitude, userLocation.longitude);
NSLog(#"distance set: %.10lfm", dist);
NSLog(#"center: lat : %.8lf long : %.8lf", region.center.latitude, region.center.longitude);
CLLocation* centerRegion = [[[CLLocation alloc] initWithLatitude:region.center.latitude longitude:region.center.longitude] autorelease];
NSLog(#"distance to western boundary: %.2lfm", [centerRegion distanceFromLocation:west]);
lat = region.center.latitude - region.span.latitudeDelta/2 ;
lon = region.center.longitude;
CLLocation *north = [[[CLLocation alloc] initWithLatitude:lat longitude:lon] autorelease];
NSLog(#"distance to western boundary: %.2lfm", [centerRegion distanceFromLocation:north]);
// ------------------------------------------------------------
}
When setting dist = dist1, that gives :
User location: lat : 48.9793946200 long : 2.4726272850
distance set: 636.9887048804m
center: lat : 48.97937199 long : 2.47269630
distance to western boundary: 500.44m
distance to western boundary: 650.57m
When setting dist = dist2, that gives :
User location: lat : 48.9793946200 long : 2.4726272850
distance set: 900.8380655203m
center: lat : 48.97937199 long : 2.47269630
distance to western boundary: 500.44m
distance to western boundary: 650.57m
What's the problem here ? Why do I have the same display with 2 different distances ?
Final question : How can I be sure to display the wanted meters on the map, at minimum for horizontal and vertical visual (with or without animation of course) ?
If I understand it correctly, you want to tell the mapView "give me a map which is 636m across" or "give me a map which is 900m across". But the map gives you the same distance across for both of these.
When you set a map region, you generally don't get back exactly what you ask for, but instead a best fit. The map view looks at the region you requested, then creates a region which fits your region inside it. The problem is that the map view doesn't zoom exactly to your requested region, it finds the highest zoom level that allows all your region to be visible. It won't use an in-between zoom level. This is why when you use setRegion: the map always looks crisp. You can manually set an in-between zoom level by pinching in or out, but not (as far as I know) programatically.
Note too that the map view might change the actual region variable you pass to it. Here's the docs:
When setting a new region, the map may adjust the value in the region parameter so that it fits the visible area of the map precisely. This is normal and is done to ensure that the value in the region property always reflects the visible portion of the map. However, it does mean that if you get the value of that property right after calling this method, the returned value may not match the value you set. (You can use the regionThatFits: method to determine the region that will actually be set by the map.)
You can see the difference in regions by logging the region you give it, and the one the map view actually sets (although I didn't see the passed myRegion change):
MKCoordinateRegion myRegion = MKCoordinateRegionMakeWithDistance(userLocation, dist, dist);
NSLog(#"Passed: %f %f", myRegion.span.latitudeDelta, myRegion.span.longitudeDelta);
[self.mapView setRegion:myRegion animated:YES];
NSLog(#"Passed 2: %f %f", myRegion.span.latitudeDelta, myRegion.span.longitudeDelta);
NSLog(#"Set: %f %f", mapView.region.span.latitudeDelta, mapView.region.span.longitudeDelta);
> Passed: 0.005728 0.008702
> Passed 2: 0.005728 0.008702
> Set: 0.012957 0.013733
If you bump your distance up to 1200m, you'll be able to see the next zoom level.
BTW, there's a small error in your code:
NSLog(#"distance to western boundary: %.2lfm", [centerRegion distanceFromLocation:north]);
should be
NSLog(#"distance to northern boundary: %.2lfm", [centerRegion distanceFromLocation:north]);
One possible cause of this is that the region you are specifying has square aspect ratio while your MKMapView probably has a rectangular one.
When you set the region MKMapView will not use it exactly as it is, but will modify it so that:
its aspect ratio corresponds to that of the view
the new region contains the specified one
Thus if your view has a width:height aspect ratio of 2:1, then the west/east boundaries would be 200m from the center while north/south boundaries 100m from the center.
Something to try after setting the region as above:
MKCoordinateRegion region = self.mapView.region;
CLLocationDegrees lat = region.center.latitude;
CLLocationDegrees lon = region.center.longitude - region.span.longitudeDelta/2;
CLLocation *west = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
NSLog(#"distance to western boundary: %.2lfm", [userLocation distanceFromLocation:west]);
lat = region.center.latitude + region.span.latitudeDelta/2
lon = region.center.longitude;
CLLocation *north = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
NSLog(#"distance to northern boundary: %.2lfm", [userLocation distanceFromLocation:north]);
One of those should be 100m. If not, I would be interested to see what they are.
P.S. The code above has not been tested in anyway.
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.
Basically I want to show the users location plus a list of selected location on a map. It can even have the standard iphone annotations. But, I have no idea of the general steps I would take to achieve this. Would I use MKMapView, or Core Location, or both? Could someone give me a simple outline of steps to take, or a link to a good tutorial or sample code. Thanks
To expand, I was wondering if there are any examples anywhere on how to deal with arrays of locations. I'm guessing that I would need to identify the users location then set up a radius of how far I want to reference locations away from the user, then populate that radius with an array of location that fit within that radius. Are my thoughts on this correct? And are there any examples out there of how to do at least a part of this. I have seen a ton of examples on how to show a single location, but none dealing with multiple locations.
Here's something I'm using that may help you. It will give you an MKCoordinateRegion that fits an array of CLLocations. You can then use that region to pass it to MKMapView setRegion:animated:
// create a region that fill fit all the locations in it
+ (MKCoordinateRegion) getRegionThatFitsLocations:(NSArray *)locations {
// initialize to minimums, maximums
CLLocationDegrees minLatitude = 90;
CLLocationDegrees maxLatitude = -90;
CLLocationDegrees minLongitude = 180;
CLLocationDegrees maxLongitude = -180;
// establish the min and max latitude and longitude
// of all the locations in the array
for (CLLocation *location in locations) {
if (location.coordinate.latitude < minLatitude) {
minLatitude = location.coordinate.latitude;
}
if (location.coordinate.latitude > maxLatitude) {
maxLatitude = location.coordinate.latitude;
}
if (location.coordinate.longitude < minLongitude) {
minLongitude = location.coordinate.longitude;
}
if (location.coordinate.longitude > maxLongitude) {
maxLongitude = location.coordinate.longitude;
}
}
MKCoordinateSpan span;
CLLocationCoordinate2D center;
if ([locations count] > 1) {
// for more than one location, the span is the diff between
// min and max latitude and longitude
span = MKCoordinateSpanMake(maxLatitude - minLatitude, maxLongitude - minLongitude);
// and the center is the min + the span (width) / 2
center.latitude = minLatitude + span.latitudeDelta / 2;
center.longitude = minLongitude + span.longitudeDelta / 2;
} else {
// for a single location make a fixed size span (pretty close in zoom)
span = MKCoordinateSpanMake(0.01, 0.01);
// and the center equal to the coords of the single point
// which will be the coords of the min (or max) coords
center.latitude = minLatitude;
center.longitude = minLongitude;
}
// create a region from the center and span
return MKCoordinateRegionMake(center, span);
}
As you're probably already established, you'll need to use a MKMapView and Core Location to do what you want. In my app I know what locations I want to display, and then make the MKMapView big enough to fit them all in. The method above will help you do that. If however you want to get a list of locations that fit within a given map region, then you'll have to do more or less the reverse of what I'm doing above.
Here's three:
http://mithin.in/2009/06/22/using-iphone-sdk-mapkit-framework-a-tutorial/
http://blog.objectgraph.com/index.php/2009/04/02/iphone-sdk-30-playing-with-map-kit/
http://www.iphonedevsdk.com/forum/tutorial-discussion/39374-mkmapview-tutorial-using-latitude-longitude.html