IOS:带路线的 map

标签 ios mapkit routes

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

最佳答案

如果您使用 UIWebView,您可以使用 javascript 并基本上免费获得该功能,因为如您所说,MapKit 不提供该功能。这很简单,检查this google maps tutorial for the basics .当然,通过这种方式,您可以获得 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]]];

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

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

根据要求编辑:

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

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

然后将以下代码放入您的 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];
}

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

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

最后,把这个作为你的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>

关于IOS:带路线的 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10573621/

相关文章:

routes - Umbraco 路由

asp.net-mvc - asp.net mvc web api 同一个 Controller 不同的函数参数

objective-c - 连续更改 Imageview 图像在 iphone sdk 中给出内存警告

Swift:使用闭包减少函数

ios - 在 map 上查找城市或地区的多边形

objective-c - 从另一个应用程序转到 iPhone map 应用程序

java - 有没有办法使用 playframework 接受路由中的自定义对象?

ios - 当我重新打开应用程序时 Swift admob 隐藏

ios - 设备离线激活后,自定义属性事件是否会重新发送到 Braze

ios - Cocos2d v3 : How do you draw an arc?