22

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

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

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

4

7 回答 7

75

是的,使用 MapKit 是不可能的。您可以尝试形成一个包含您当前位置和目的地的 Google 地图 url 请求,该请求将在 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]];
于 2009-10-12T05:37:23.267 回答
11

在 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)!)
        }
于 2017-01-04T19:42:02.593 回答
2

有可能。使用MKMapView获取您在手机上点击的位置坐标,并使用这两个坐标从谷歌网络服务请求KML文件,解析KML文件(开发人员站点中的示例应用程序 KML 查看器)并显示路线.. ..
谢谢

于 2011-06-29T06:23:33.603 回答
2

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

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>
于 2017-06-15T11:06:13.893 回答
1

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

于 2010-05-06T23:55:53.793 回答
1

可以在 MapKit 中显示路线:只需使用 MKPolyline

我从 googleMapsApi 获得折线字符串。我用 php 在服务器上解析它,并将最终的多线字符串返回到我的应用程序。

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

试试看。

于 2012-09-28T06:53:50.800 回答
-1

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

于 2011-08-18T05:54:22.307 回答