iPhone:Road Direction Map view - iphone

I am showing a map view using MapKit framework in my iPhone application. I am also showing the particular friends list Pin point in map view based on the location they are. I would like to enhance now to showing the road map direction from my location to a particular selected friend location. I know the lat and long for both source and destination, but i have draw route line in road direction, it should be road direction. Could someone help me on this?
Thank you!

MapKit does not expose a means of performing driving directions. So, it's not as simple as asking the map to display a course from location A to location B. You have two options:
1) Integrate with Google's API to get the driving directions, and overlay your own lines onto the MapKit map.
or
2) Simply direct your users out of app and delegate this functionality to the built in Map app.
I have no experience with the former, but the later is very easy. Simply:
CLLocationCoordinate2D location = [[map userLocation] location].coordinate;
double currentLat = location.latitude;
double currentLong = location.longitude;
NSString *googleUrl = [[NSString alloc] initWithFormat:#"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f", currentLat, currentLong, item.latitude, item.longitude];
NSLog(#"%#", googleUrl);
[[UIApplication sharedApplication] openURL:[[NSURL alloc] initWithString:googleUrl]];

Actually there is no api supported by iPhone sdk to draw route on map. There a repo on github which is using google maps api to draw route on map by using map overlay. It has some limitation but you can take help from this repo - https://github.com/kishikawakatsumi/MapKit-Route-Directions

Related

Getting the Co-ordinatades of middle point of Map - iPhone sdk

I am making iPhone application in which i am using UIMapKit. I have a static pointer (Arrow) in the middle of screen of iPhone and behind that Pointer i am using iOS maps in which user interaction is enable , means user can move the map can zoom in or zoom out the map.
My task is to get the longitude and latitude of that location which comes under my Screen Pointer(Arrow)
Can anyone help me how can i do achieve this. I have very good command on UIMap i just need a idea
You can get middle point of screen using :
CGPoint screenCenterPoint = self.view.center;
and then convert it to coordinates using :
CLLocationCoordinate2D mapCenterPoint = [self.view convertPoint:screenCenterPoint toCoordinateFromView:self.view];
If you use Google maps then you can use this ,too :
CLLocationCoordinate2D mapCenterPoint = [self.mapView.projection coordinateForPoint: screenCenterPoint];
To learn more look at GMSProjection and GMSMapView.
The necessary functionality is built in to MapView.
CLLocationCoordinate2D pinCoordinate = [self.pickupLocationView convertPoint:CGPointMake(self.pickupLocationView.frame.size.width / 2, self.pickupLocationView.frame.size.height) toCoordinateFromView:self.view];
This is UIKit as the poster requested. I believe Akash's solution would work if the question was about Google's map.

How can i use Maps directions app?

I have an app that display locations on the MapView, and i want to get the direction of a specific location from my current location using the Directions APP on the iPhone. how can i do that?
This is a pretty old post from Jeff: http://iphonedevelopment.blogspot.com/2009/02/mapping-directions-from-your-app.html .
From your question, I assume you want to get directions using the Maps app.
To do this you'll need to code something like:
NSString *mapsUrl =
[NSString stringWithFormat:#"http://maps.google.com/maps?saddr=%f,%f&daddr=%#",
self.mapView.userLocation.location.coordinate.latitude,
self.mapView.userLocation.location.coordinate.longitude,
<destination address>];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: [mapsUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]];
where saddr is the start address (or lat, lon pair in this case) and daddr is the destination address.
I have done that before. It is basically more trouble than it's worth because you have to implement the directions yourself and every time the Map app gets new features added then the direction system in your app will become outdated. What you really want to do is create a link for the directions to open in the Map app. Owners always make the argument that they don't want the user to have to leave the app, but ultimately you want to provide the best utilities for the user and unfortunately linking to the map app is the way to go here.
You can link to the map app like this:
[[UIApplication sharedApplication] openURL:[NSURL
URLWithString:#"http://maps.google.com/whateveryoururlis"];

Adding pins to offline map using route-me RMMapView

I am using the route-me files found at https://github.com/route-me/route-me to build a offline map application for iPhones.
I have the map working offline and also successfully implemented "current location" button that will show the users location on the map with a marker.
Now, I want to be able to let the users be able to drop "pins" on the map so that users can get the coordinates of the "pins" that user just dropped on the map. (separate from the current location)
I have tried to look for tutorials and other helpful documents on Google but all the information that I've found are using the MapKit lib and framework
If any of you are expert on route-me and using RMMapView to make offline maps, it would be awesome if you guys can help me out.
Thanks
For droping pins on RMMapview , this is required snippet of code used in - (void)viewDidLoad after creating RMMapview
RMMarkerManager *markerManager = [[[RMMarkerManager alloc] initWithContents: mapView]autorelease];
RMMarker *marker = [[RMMarker alloc]initWithUIImage:[UIImage imageNamed:#"marker-blue.png"] anchorPoint:CGPointMake(0.5, 1.0)];
[marker setTextForegroundColor:[UIColor blueColor]];
[markerManager addMarker:marker AtLatLong:(CLLocationCoordinate2D){40.988,23.098}];
[marker release];

Map view refresh in iPhone app

I am using map functionality in my iphone app. I m showing stores for users current location on map.
Whenever user scrolls the map he needs to be shown stores of new location. eg. suppose user at
New York at first app will show New York stores but when he scrolls map to Texas then app should fire web service request for Texas location. My problem is
1) if web service request goes at each map scroll, app may crash or wait each time for response for new set of stores. (for this i m going to put some hardcoded radius to send request) So how to handle it proper way.
2) I want to know distance between two location so that i can send request to server only if the distance between 2 locations is greater than some specific value.
I am using map view delegates for above functionality. Please suggest me some proper way to handle it.
Thanks
Well to find the distance between 2 points i use
CLLocation *location1 = [[CLLocation alloc]initWithLatitude:[[dict valueForKey:#"lat"] doubleValue] longitude:[[dict valueForKey:#"lon"]doubleValue]];
float distance =[mUserCurrentLocation distanceFromLocation:location1]/1000;
float distanceinMeters=[mUserCurrentLocation distanceFromLocation:location1]; NSString *distancestr= [NSString stringWithFormat:#"%.2f KM",distance];
See If this can help you.

how to use Google Map's API in Iphone?

I want to figure out how Google Map's API to give us directions.
Create a sample tool where I can use my location as a starting point and choose another location and I can view directions (both driving and walking) in text and on map within my existing application.
I haven't used google map api so I am completely naive about this
can anyone help me with this?
try this,
NSString *urlstring=[NSString stringWithFormat:#"http://maps.google.com/?saddr=%f,%f&daddr=%f,%f",sourcelocation.latitude,sourcelocation.longitude,destinationlocation.latitude,destinationlocation.longitude];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlstring]];
What the code do is, it will show the route between your source and destination location on google map.
If you dont have the longitude then you can do it like:-
NSString *source=#"NorthCarolina";
NSString *destination=#"SouthCarolina";
NSString *urlstring=[NSString stringWithFormat:#"http://maps.google.com/?saddr=%#&daddr=%#",source,destination];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlstring]];
I hope this will help you.Happy coding:)