如何在iphone上使用坐标打开谷歌地图查看方向
我正在使用UIMapView显示iPhone上的位置。我想做一个从当前位置到感兴趣位置的指示,我认为使用MapKit是不可能的(但如果是请告知),所以我将打开Google地图应用程序或safari来显示它 我可以通过指定从(当前位置)到坐标(感兴趣的位置)的坐标来实现这一点吗?我有这些经度和纬度。或者我必须使用街道地址如何在iphone上使用坐标打开谷歌地图查看方向,iphone,google-maps,mapkit,coordinates,Iphone,Google Maps,Mapkit,Coordinates,我正在使用UIMapView显示iPhone上的位置。我想做一个从当前位置到感兴趣位置的指示,我认为使用MapKit是不可能的(但如果是请告知),所以我将打开Google地图应用程序或safari来显示它 我可以通过指定从(当前位置)到坐标(感兴趣的位置)的坐标来实现这一点吗?我有这些经度和纬度。或者我必须使用街道地址 如果我必须使用街道地址,我可以从纬度和经度获取它们。是的,使用MapKit是不可能的。您可以尝试形成一个包含当前位置和目的地的Google maps url请求,该请求将在Goo
如果我必须使用街道地址,我可以从纬度和经度获取它们。是的,使用MapKit是不可能的。您可以尝试形成一个包含当前位置和目的地的Google maps url请求,该请求将在Google maps应用程序中打开,并提供方向 下面是一个示例url: 以下是如何在代码中实现这一点:
CLLocationCoordinate2D start = { 34.052222, -118.243611 };
CLLocationCoordinate2D destination = { 37.322778, -122.031944 };
NSString *googleMapsURLString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f",
start.latitude, start.longitude, destination.latitude, destination.longitude];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURLString]];
一个可靠的解决方案是创建一个带有NIB(包含UIWebView)的视图控制器,然后传递执行Google地图/方向服务的URL。这样,您就可以将用户保留在应用程序中。在打开网页时,这种方法是不够的,因为Apple工具包不支持缩放。但是使用OS4,至少用户可以双击home按钮并切换回应用程序。这是可能的。使用MKMapView获取您在手机上点击的位置坐标,并使用这两个坐标从google web服务请求KML文件,解析KML文件(开发者站点中的示例应用程序KML查看器)并显示路线……
谢谢您您可以通过电子邮件将丢失的pin发送给自己,当您在电子邮件中打开链接时,它将显示坐标。可以在MapKit中显示路线:只需使用MKPolyline 我从googleMapsApi获取多段线字符串。我在服务器上用php解析它,并将最终的polilyne字符串返回到我的应用程序
NSMutableArray *points = [myApp decodePolyline:[route objectForKey:@"polyline"]];
if([points count] == 0)
{
return;
}
// while we create the route points, we will also be calculating the bounding box of our route
// so we can easily zoom in on it.
MKMapPoint northEastPoint;
MKMapPoint southWestPoint;
// create a c array of points.
MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * [points count]);
for(int idx = 0; idx < points.count; idx++)
{
// break the string down even further to latitude and longitude fields.
NSString* currentPointString = [points objectAtIndex:idx];
NSArray* latLonArr = [currentPointString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];
CLLocationDegrees latitude = [[latLonArr objectAtIndex:0] doubleValue];
CLLocationDegrees longitude = [[latLonArr objectAtIndex:1] doubleValue];
// create our coordinate and add it to the correct spot in the array
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);
MKMapPoint point = MKMapPointForCoordinate(coordinate);
if (idx == 0) {
northEastPoint = point;
southWestPoint = point;
}
else
{
if (point.x > northEastPoint.x)
northEastPoint.x = point.x;
if(point.y > northEastPoint.y)
northEastPoint.y = point.y;
if (point.x < southWestPoint.x)
southWestPoint.x = point.x;
if (point.y < southWestPoint.y)
southWestPoint.y = point.y;
}
pointArr[idx] = point;
_currentLenght++;
}
// create the polyline based on the array of points.
self.routeLine = [MKPolyline polylineWithPoints:pointArr count:points.count];
_routeRect = MKMapRectMake(southWestPoint.x, southWestPoint.y,
northEastPoint.x - southWestPoint.x,
northEastPoint.y - southWestPoint.y);
// clear the memory allocated earlier for the points
free(pointArr);
if (nil != self.routeLine) {
[self.mapView addOverlay:self.routeLine];
}
[self.mapView setVisibleMapRect:_routeRect];
NSMutableArray*points=[myApp decodePolyline:[route objectForKey:@“polyline]”;
如果([点数]==0)
{
返回;
}
//在创建管线点的同时,我们还将计算管线的边界框
//所以我们可以很容易地放大它。
MKMapPoint东北点;
MKMapPoint西南角;
//创建一个c点数组。
MKMapPoint*pointArr=malloc(sizeof(CLLocationCoordinate2D)*[点数];
对于(int idx=0;idx东北点x)
东北点x=点x;
如果(点y>东北点y)
东北点y=点y;
如果(点x<西南点x)
西南点x=点x;
如果(点y<西南点y)
西南点y=点y;
}
pointArr[idx]=点;
_currentLenght++;
}
//基于点阵列创建多段线。
self.routeLine=[MKPolyline polyline polylineWithPoints:pointArr count:points.count];
_Rout直立=MKMapRectMake(西南点.x,西南点.y,
东北点.x-西南点.x,
东北点y-西南点y);
//清除先前为点分配的内存
免费(pointArr);
if(nil!=自布线){
[self.mapView addOverlay:self.routeLine];
}
[self.mapView setVisibleMapRect:_];
并显示:
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
MKOverlayView* overlayView = nil;
if(overlay == self.routeLine)
{
self.routeLineView = [[[MKPolylineView alloc] initWithPolyline:self.routeLine] autorelease];
self.routeLineView.fillColor = [UIColor blueColor];
self.routeLineView.strokeColor = TICNavigatorColor;
self.routeLineView.lineWidth = 7;
self.routeLineView.lineJoin = kCGLineJoinRound;
self.routeLineView.lineCap = kCGLineCapRound;
overlayView = self.routeLineView;
}
return overlayView;
}
-(MKOverlayView*)地图视图:(MKMapView*)地图视图覆盖:(id)覆盖
{
MKOverlayView*overlayView=nil;
if(叠加==自布线)
{
self.routeLineView=[[MKPolylineView alloc]initWithPolyline:self.routeLine]autorelease];
self.routeLineView.fillColor=[UIColor blueColor];
self.routeLineView.strokeColor=TICNavigatorColor;
self.routeLineView.lineWidth=7;
self.routeLineView.lineJoin=kCGLineJoinRound;
self.routeLineView.lineCap=kCGLineCapRound;
overlayView=self.routeleview;
}
返回覆盖视图;
}
试一试。在Swift 3中为谷歌和苹果地图使用下面的代码-
if UIApplication.shared.canOpenURL(URL(string: "comgooglemaps://")!)
{
let urlString = "http://maps.google.com/?daddr=\(destinationLocation.latitude),\(destinationLocation.longitude)&directionsmode=driving"
// use bellow line for specific source location
//let urlString = "http://maps.google.com/?saddr=\(sourceLocation.latitude),\(sourceLocation.longitude)&daddr=\(destinationLocation.latitude),\(destinationLocation.longitude)&directionsmode=driving"
UIApplication.shared.openURL(URL(string: urlString)!)
}
else
{
//let urlString = "http://maps.apple.com/maps?saddr=\(sourceLocation.latitude),\(sourceLocation.longitude)&daddr=\(destinationLocation.latitude),\(destinationLocation.longitude)&dirflg=d"
let urlString = "http://maps.apple.com/maps?daddr=\(destinationLocation.latitude),\(destinationLocation.longitude)&dirflg=d"
UIApplication.shared.openURL(URL(string: urlString)!)
}
首先检查谷歌地图是否安装在设备中
if ([[UIApplication sharedApplication] canOpenURL:
[NSURL URLWithString:@"comgooglemaps://"]]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"comgooglemaps://?saddr=23.0321,72.5252&daddr=22.9783,72.6002&zoom=14&views=traffic"]];
} else {
NSLog(@"Can't use comgooglemaps://");
}
<key>LSApplicationQueriesSchemes</key>
<array>
<string>comgooglemaps</string>
</array>
在.plist中添加查询架构
if ([[UIApplication sharedApplication] canOpenURL:
[NSURL URLWithString:@"comgooglemaps://"]]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"comgooglemaps://?saddr=23.0321,72.5252&daddr=22.9783,72.6002&zoom=14&views=traffic"]];
} else {
NSLog(@"Can't use comgooglemaps://");
}
<key>LSApplicationQueriesSchemes</key>
<array>
<string>comgooglemaps</string>
</array>
LSApplicationQueriesSchemes
谷歌地图
有机会得到一个示例项目吗?KML Viewer项目将帮助您绘制两个地方之间的路线。iOS6地图是一个笑话,顺便说一句,第一个链接断开了!发布了6:)有人使用Apple Maps应用程序完成了IOS6版本吗?网络视图中是否有可打开的url?我的意思是,如果我们没有google map,你认为这是个好主意吗?如何在google Maps或Apple Maps上启用语音只显示从源到目标的线路。。我想把线条画成之字形。就像它显示了一条你需要沿着这个方向走的正确路线。。。