开发者

how to open OpenStreetmap in mkmapview? -iphone

开发者 https://www.devze.com 2023-01-26 16:54 出处:网络
I have gone through certain related answers but dont seem to get correct answer or the answer I need.

I have gone through certain related answers but dont seem to get correct answer or the answer I need.

As we open googlemap in mkmap view I want to open open开发者_开发问答street map in mkmapview.

If there is any link or sample code showing it please pass it on.

Thanks in advance


MKMapView conforms to Google Map terms and conditions so it uses only google map. You cant integrate OpenStreetMap just like that into MKMapView. Google code has a API RouteME which renders OpenStreetMap in iphone.

RouteMe also provide a good documentation how to include into our project. So feel free to use that.


Import these Frameworks:

#import <MapKit/MapKit.h>

Include this delegate

@interface ViewController () <MKMapViewDelegate>

Add this code to your preexisting viewDidLoad method

(void)viewDidLoad
{
    // Tile system URL template goes here
    NSString *template = @"http://tile.openstreetmap.org/{z}/{x}/{y}.png"; 

    // Associating overlay with URL template
    MKTileOverlay *overlay = [[MKTileOverlay alloc] initWithURLTemplate:template]; 

    // Allowing overlays on MKMapView & disabling Apple map data
    overlay.canReplaceMapContent = YES; 

    // Adding overlay to MKMapView above Apple lables
    [self.mapView addOverlay:overlay level:MKOverlayLevelAboveLabels];

    // Linking the delegate to allow the next method to be called
    self.mapView.delegate = self; 
}

And this somewhere in your class that’s mapView’s delegate (most likely a view controller).

(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay 
{
    if ([overlay isKindOfClass:[MKTileOverlay class]]) {
        // Overlay the tile with the new template
        return [[MKTileOverlayRenderer alloc] initWithTileOverlay:overlay]; 
    }
    return nil;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号