22

UIMapViewを使用してiPhoneの場所を表示しています。現在地から目的地までの道順を知りたいのですが、MapKitではできないと思いますので(教えてください)、グーグルマップかサファリのどちらかを開いて表示します。

(現在の場所)から座標(関心のある場所)までの座標を指定してこれを行うことはできますか?これらの経度と緯度があります。または、番地を使用する必要がありますか?

番地を使用する必要がある場合、緯度と経度から取得できますか。

4

7 に答える 7

75

ええ、MapKitを使用することはできません。現在地と目的地の両方を含むGoogleマップのURLリクエストを作成して、Googleマップアプリで道順を確認することができます。

URLの例を次に示します。

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

Swift3のグーグルマップとアップルマップの両方に以下のコードを使用します-

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を使用して電話をタップした場所の座標を取得し、2つの座標を使用してGoogle WebサービスからKMLファイルをリクエストし、KMLファイル(開発者サイトのサンプルアプリKMLビューア)を解析しルートを表示します。 ..
ありがとう

于 2011-06-29T06:23:33.603 に答える
2

まず、Googleマップがデバイスにインストールされているかどうかを確認します

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を使用してView Controllerを作成し、Googleの地図/方向サービスを実行するURLを渡すことです。このようにして、ユーザーをアプリケーションにとどめます。Appleキットはズームをサポートしていないため、Webページをプルアップする場合はこのアプローチでは不十分です。ただし、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 に答える