如何使用iphone上的坐标打开Goog​​le地图以获取路线

时间:2009-10-10 13:58:17

标签: iphone google-maps mapkit coordinates

我正在使用UIMapView在iPhone上显示位置。我想从当前位置到感兴趣的位置做一个方向,我认为不可能使用MapKit(但如果是请通知)所以我将打开Goog​​le Maps应用程序或safari来显示它。

我可以通过指定从(当前位置)到坐标(感兴趣的位置)的坐标来做到这一点我有这些经度和纬度。或者我必须使用街道地址吗?

如果我必须使用街道地址,我可以从纬度和经度获得它们。

7 个答案:

答案 0 :(得分:74)

是的,使用MapKit是不可能的。您可以尝试形成一个Google地图网址请求,其中包含您将在Google地图应用中按指示打开的当前位置和目标位置。

这是一个示例网址:

http://maps.google.com/?saddr=34.052222,-118.243611&daddr=37.322778,-122.031944

以下是您在代码中实现此功能的方法:

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]];

答案 1 :(得分:7)

在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)!)
        }

答案 2 :(得分:2)

有可能。使用 MKMapView 获取您点按手机的位置坐标,并使用两个坐标请求来自Google网络服务的 KML 文件,解析 KML 文件(开发者网站中的示例应用KML查看器)并显示路线.... 谢谢

答案 3 :(得分:1)

一个可靠的解决方案是使用包含UIWebView的NIB创建一个视图控制器,然后传递执行Google地图/方向服务的URL。这样,您就可以将用户保留在应用程序中。在提取网页时这种方法是不够的,因为Apple套件不支持缩放。但是对于OS4,至少用户可以双击主页按钮并切换回应用程序。

答案 4 :(得分:1)

可以在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];

并显示:

- (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; 
}

试一试。

答案 5 :(得分:1)

首先检查谷歌地图是否已安装在设备中

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://");
    }

在.plist中添加查询架构

<key>LSApplicationQueriesSchemes</key>
<array>
 <string>comgooglemaps</string>
</array>

答案 6 :(得分:-1)

您可以将丢弃的图钉通过电子邮件发送给自己,当您在电子邮件中打开链接时,它会显示坐标。