Current Location doesn't work with Apple Maps IOS 6

独自空忆成欢 提交于 2019-11-29 06:03:05

I am having the same problem. I haven't found a solution yet but if you leave off the saddr

http://maps.apple.com/maps?daddr=" + address

it will just ask them where to start and the first option is "Current Location" so when they click "Current Location" it will show the map correctly.

If anyone finds a better solution please post it as I am still looking for a better solution.

You can use my method:

    <script type="text/javascript">

        var link = "maps:saddr=YPlat,YPlong&daddr=42.118599,-72.625122";
        navigator.geolocation.getCurrentPosition(showPosition);

        function showPosition(position)
        {

            link = link.replace("YPlat",position.coords.latitude);
            link = link.replace("YPlong",position.coords.longitude);

            window.location = link;
        } 
    </script>

confirmed with iOS 5.1 and iOS 6

Just pass "Current Location" as the source address:

http://maps.apple.com/maps?saddr=Current%20Location&daddr=Your_Address

You can get the coordinates of the current location using CLLocationManager, or its wrapper DKLocationManager (on github), created by Keith Pitt.

Once you have the coordinates, you can use the following code sample.

+ (void) openDirectionFrom:CLLocation* currentLocation To:(NSString*) daddr {
    NSString* urlStr;
    NSString* saddr = @"Current+Location";

    if ([[UIDevice currentDevice] systemVersion] floatValue] >=6) {
        //iOS 6+, Should use map.apple.com. Current Location doesn't work in iOS 6 . Must provide the coordinate.
        if ((currentLocation.coordinate.latitude != kCLLocationCoordinate2DInvalid.latitude) && (currentLocation.coordinate.longitude != kCLLocationCoordinate2DInvalid.longitude)) {
            //Valid location.
            saddr = [NSString stringWithFormat:@"%f,%f", currentLocation.coordinate.latitude,currentLocation.coordinate.longitude];
            urlStr = [NSString stringWithFormat:@"http://maps.apple.com/maps?saddr=%@&daddr=%@", saddr, daddr];
        } else {
            //Invalid location. Location Service disabled.  
            urlStr = [NSString stringWithFormat:@"http://maps.apple.com/maps?daddr=%@", daddr];
        }
    } else {
        // < iOS 6. Use maps.google.com
        urlStr = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=%@&daddr=%@", saddr, daddr];
    }

    [(UIApplicationWithEvents*)[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]];

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!