在我的应用程序中,我应该创建一个视图控制器,里面应该是一个地图;在这个地图中,我应该看到连接两个引脚的路线。(因为我有两个引脚)所以我想知道获得这个结果的最佳解决方案是什么;同样,我应该看到两种类型的路线:一条有汽车的路线和一条步行路线……有没有可以帮助我的框架?(因为mapkit不能解决我的问题)谢谢
发布于 2012-05-14 03:57:09
如果你使用的是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并将其连接到控制器(如果您不知道如何做到这一点,您肯定应该后退一步,做一些基本的教程)(不要忘记合成mapView。再说一次,如果你不知道那是什么,那就进行基本阅读)
#import <UIKit/UIKit.h>
@interface MapViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIWebView *mapView;
@end然后,将以下代码放入您的viewDidLoad中,method.It将创建一个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];
}接下来,将一个按钮添加到视图(界面构建器,您应该知道如何使用),并将其连接到一个方法。在这个方法中,放入以下代码(顺便说一下,这将为您提供一条在德国的路线)
[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®ion=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>发布于 2012-05-14 04:09:42
请参阅本教程在mkmapview中绘制折线或路径
1>Draw route using mapkit
ios4.0以上的2>From版本您可以使用MKOverlayPathView See Apple Docs
发布于 2012-05-14 03:41:22
您可以向Google Maps询问该路线,解析json答案并在地图上显示一行PolyLineView。这就是它在iOS地图应用程序上的工作方式。在https://developers.google.com/maps/documentation/directions/上查看Google Maps Route API
https://stackoverflow.com/questions/10573621
复制相似问题