IOS: map with route

In my application, I have to create a viewcontroller where the map should be inside; On this map I should see a route connecting two contacts. (because I have two contacts) Therefore, I want to know what is the best solution to this result; again I have to see two types of route: a route with a car and a walking route ... is there any framework that can help me? (because mapkit does not solve my problem) thanks

+4
source share
4 answers

If you use UIWebView, you can use javascript and get functionality for free, because, as you said, MapKit does not provide this functionality. It's pretty simple, check out this google maps guide for the basics . Of course, this way you get the pros and cons of html and javascript.

You just write your javascript code in html (here map.html ), which you put in the project folder, and initiate this html file like this.

 NSString *html = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"map" ofType:@"html"] encoding:NSUTF8StringEncoding error:&error]; [self.mapView loadHTMLString:html baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]]; 

You call the javascript function from objective-c as follows. here in map.html there is a javascript function called setMarkerAtPosition(latitude, longitude) . pretty straight forward.

 [self.mapView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"setMarkerAtPosition(%f,%f)",latlong.latitude, latlong.longitude]]; 

Change by request:

You add a UIWebView and connect it to the controller (if you don’t know how to do this, you should definitely step back from this and do some basic tutorials) (remember to synthesize mapView again. If you don’t know what it is, basic reading)

 #import <UIKit/UIKit.h> @interface MapViewController : UIViewController @property (weak, nonatomic) IBOutlet UIWebView *mapView; @end 

Then put the following code in your viewDidLoad method. It creates an NSString and initializes it with the contents of your local html file (as already published). Then you call the loadHTMLString method of your UIWebView. The html line is based on your local html map.html file (just name it and just drag it into your project if you haven't added it yet).

 - (void)viewDidLoad { [super viewDidLoad]; NSError *error = nil; // create NSString from local HTML file and add it to the webView NSString *html = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"map" ofType:@"html"] encoding:NSUTF8StringEncoding error:&error]; [self.mapView loadHTMLString:html baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]]; [self.view sendSubviewToBack:self.mapView]; } 

Then add a button to your view (interface designer, you need to know how) and connect it to the method. Put this code inside this method (by the way, this will give you a route in Germany)

 [self.mapView stringByEvaluatingJavaScriptFromString:@"calculateRoute(50.777682, 6.077163, 50.779347, 6.059429)"]; 

Finally, use this as the contents of your map.html file (note: it also has some useless things, but you can figure it out later. I deleted all the code that you don't need to display the route, but a lot of things are useless in the header) :

 <html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> <script type="text/javascript" src="http://maps.google.de/maps/api/js?sensor=false&region=DE"></script> <script src="http://www.google.com/jsapi"></script> <script src="http://code.jquery.com/jquery-1.7.2.js"></script> <script src="json.js"></script> <script type="text/javascript">google.load("jquery", "1");</script> <script type="text/javascript"> google.load("maps", "3", {other_params:"sensor=false"}); </script> <script type="text/javascript"> // global variables var currentPosition; directionsDisplay = new google.maps.DirectionsRenderer(); var directionsService = new google.maps.DirectionsService(); var map; var infowindow = new google.maps.InfoWindow(); // calculates the current userlocation function getCurrentLocation(){ if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(success, error); } else { alert('geolocation not supported'); } } // called when the userLocation has been located function success(position) { currentPosition = position; drawMap(position.coords.latitude, position.coords.longitude); } // called when the userLocation could not be located function error(msg) { alert('error: ' + msg); } // calculate and route from-to and show on map function calculateRoute(fromLat, fromLong, toLat, toLong){ var start = new google.maps.LatLng(fromLat, fromLong); var end = new google.maps.LatLng(toLat, toLong); var request = { origin:start, destination:end, travelMode: google.maps.DirectionsTravelMode.WALKING }; directionsService.route(request, function(response, status) { if (status == google.maps.DirectionsStatus.OK) { directionsDisplay.setDirections(response); } }); directionsDisplay.setMap(map); } // draws the inital map and shows userlocation function drawMap(latitude, longitude) { var latlng = new google.maps.LatLng(latitude, longitude); var myOptions = { disableDefaultUI: true, zoom: 15, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); google.maps.event.addListener(map, 'click', function() {if(infowindow){infowindow.close();}}); setMarkerAtPosition(latitude, longitude); } </script> </head> <body onload="getCurrentLocation()"> <div id="map_canvas" style="width:100%; height:100%"> </body> </html> 
+4
source

See this tutorial for drawing a polyline or route at mkmapview

1> Draw a route using mapkit

2> From versions above ios4.0 you can use MKOverlayPathView See Apple Docs

+2
source

You can request Google Maps for this route, analyze the json response and display a line on the PolyLineView on your map. This is the way it works in the iOS Maps app. Check out the google maps route API at https://developers.google.com/maps/documentation/directions/

+1
source

You can use web view and open URL

https://maps.google.com/maps?saddr=START_LOCATION&daddr=DESTINATION_LOCATION

this will open the webpage as you want.

You can enter any pair (lat, long) or the name of a direct city.

Hope this helps you

+1
source

Source: https://habr.com/ru/post/1412274/


All Articles