IOS:带路线的地图

Cra*_*Dev 5 routes mapkit ios

在我的应用程序中,我应该创建一个viewcontroller,其中inside应该是一个map; 在这张地图中,我应该看到一条连接两个引脚的路线.(因为我有两个针脚)所以我想知道什么是获得这个结果的最佳解决方案; 再次,我应该看到两种类型的路线:一条有车的路线和一条步行路线......有没有一个可以帮助我的框架?(因为mapkit不解决我的问题)谢谢

MJB*_*MJB 4

如果您使用 UIWebView,您可以使用 javascript 并基本上免费获得该功能,因为正如您所说,MapKit 不提供该功能。这非常简单,请查看此谷歌地图教程以了解基础知识。当然,通过这种方式,您可以获得 html 和 javascript 的所有优点和缺点。

您只需将 javascript 代码写入 html(此处为map.html),然后将其放入项目文件夹中并像这样启动该 html 文件。

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]]];
Run Code Online (Sandbox Code Playgroud)

您可以像这样从 Objective-C 调用 javascript 函数。这里,map.html 中有一个名为 的 javascript 函数setMarkerAtPosition(latitude, longitude)。非常简单。

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

根据要求编辑:

您添加一个 UIWebView 并将其连接到控制器(如果您不知道如何执行此操作,您绝对应该从这里退一步并执行一些基本教程)(不要忘记合成 mapView。再次,如果您不知道那是什么做基础阅读)

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

然后将以下代码放入您的 viewDidLoad 方法中。它创建一个 NSString 并使用本地 html 文件的内容对其进行初始化(就像已经发布的那样)。然后,调用 UIWebView 的 loadHTMLString 方法。html 字符串基于您的本地 html 文件map.html(只需将其命名,然后将其拖放到您的项目中(如果您尚未添加它)。

- (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];
}
Run Code Online (Sandbox Code Playgroud)

接下来向您的视图添加一个按钮(界面构建器,您应该知道如何操作)并将其连接到一个方法。在此方法中,输入此代码(顺便说一下,这将为您提供一条在德国的路线)

[self.mapView stringByEvaluatingJavaScriptFromString:@"calculateRoute(50.777682, 6.077163, 50.779347, 6.059429)"];    
Run Code Online (Sandbox Code Playgroud)

最后,用这个作为你文件的内容map.html(注意;里面还有一些无用的东西,但是你可以稍后再弄清楚。我删除了所有不需要显示路线的代码,但是里面有很多东西标题没用):

<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>
Run Code Online (Sandbox Code Playgroud)