代码之家  ›  专栏  ›  技术社区  ›  Aran Mulholland JohnnyAce

如何在iPhone上使用坐标打开谷歌地图

  •  20
  • Aran Mulholland JohnnyAce  · 技术社区  · 15 年前

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

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

    如果我必须使用街道地址,我能从纬度和经度得到它们吗?

    7 回复  |  直到 8 年前
        1
  •  2
  •   Hiren anka    13 年前

    有可能,使用 Mkmap视图 获取您在电话上点击的位置坐标,并使用两个坐标请求 KML 来自Google Web服务的文件,分析 KML 文件(开发人员站点中的示例应用程序kml查看器)并显示路由….
    谢谢你

        2
  •  74
  •   Adolfo    11 年前

    是的,使用MapKit是不可能的。你可以尝试建立一个谷歌地图URL请求,它包含你当前的位置和目的地,这些位置和目的地将在谷歌地图应用程序中打开。

    以下是一个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]];
    
        3
  •  6
  •   Vishwas Singh    8 年前

    在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)!)
            }
    
        4
  •  1
  •   alex    13 年前

    一个可靠的解决方案是创建一个包含uiwebview的NIB视图控制器,然后传递执行谷歌地图/方向服务的URL。这样,就可以将用户保留在应用程序中。这种方法在打开网页时是不够的,因为AppleKit不支持缩放。但有了OS4,至少用户可以双击Home按钮,切换回应用程序。

        5
  •  1
  •   xor22h    12 年前

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

    试试看。

        6
  •  1
  •   Hardik Thakkar    8 年前

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

    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>
    
        7
  •  -1
  •   Hamza    14 年前

    你可以通过电子邮件将掉下来的密码发送给自己,当你在电子邮件中打开链接时,它会显示坐标。

    推荐文章