diff --git a/MapView/Map/MapBox.h b/MapView/Map/MapBox.h index ae8292747..db32b8540 100644 --- a/MapView/Map/MapBox.h +++ b/MapView/Map/MapBox.h @@ -68,6 +68,7 @@ #import "RMProjection.h" #import "RMQuadTree.h" #import "RMShape.h" +#import "RMStaticMapView.h" #import "RMTile.h" #import "RMTileCache.h" #import "RMTileImage.h" diff --git a/MapView/Map/RMStaticMapView.h b/MapView/Map/RMStaticMapView.h new file mode 100644 index 000000000..fbe724eaf --- /dev/null +++ b/MapView/Map/RMStaticMapView.h @@ -0,0 +1,39 @@ +// +// RMStaticMapView.h +// +// Copyright (c) 2008-2012, Route-Me Contributors +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +#import + +@interface RMStaticMapView : UIImageView + +- (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)initialZoomLevel; + +// TODO: delegate +// TODO: markers +// TODO: attribution +// TODO: logo bug + +@end diff --git a/MapView/Map/RMStaticMapView.m b/MapView/Map/RMStaticMapView.m new file mode 100644 index 000000000..0a3f8d40a --- /dev/null +++ b/MapView/Map/RMStaticMapView.m @@ -0,0 +1,87 @@ +// +// RMStaticMapView.m +// +// Copyright (c) 2008-2012, Route-Me Contributors +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +#import "RMStaticMapView.h" + +#define RMStaticMapViewMaxWidth 640.0f +#define RMStaticMapViewMaxHeight 640.0f +#define RMStaticMapViewMinZoom 0.0f +#define RMStaticMapViewMaxZoom 17.0f + +@implementation RMStaticMapView + +- (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)initialZoomLevel +{ + if (!(self = [super initWithFrame:frame])) + return nil; + + CGRect requestFrame = CGRectMake(frame.origin.x, frame.origin.y, fminf(frame.size.width, RMStaticMapViewMaxWidth), fminf(frame.size.height, RMStaticMapViewMaxHeight)); + + if ( ! CLLocationCoordinate2DIsValid(centerCoordinate)) + centerCoordinate = CLLocationCoordinate2DMake(0, 0); + + initialZoomLevel = fmaxf(initialZoomLevel, RMStaticMapViewMinZoom); + initialZoomLevel = fminf(initialZoomLevel, RMStaticMapViewMaxZoom); + + self.backgroundColor = [UIColor colorWithPatternImage:[RMMapView resourceImageNamed:@"LoadingTile.png"]]; + + self.contentMode = UIViewContentModeCenter; + + UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; + + [spinner startAnimating]; + + spinner.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin; + + spinner.center = self.center; + + [self addSubview:spinner]; + + NSURL *imageURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://api.tiles.mapbox.com/v3/%@/%f,%f,%i/%ix%i.png", mapID, centerCoordinate.longitude, centerCoordinate.latitude, (int)roundf(initialZoomLevel), (int)roundf(requestFrame.size.width), (int)roundf(requestFrame.size.height)]]; + + [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:imageURL] + queue:[NSOperationQueue new] + completionHandler:^(NSURLResponse *response, NSData *responseData, NSError *error) + { + [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; + + if (responseData) + { + dispatch_async(dispatch_get_main_queue(), ^(void) + { + self.image = [UIImage imageWithData:responseData]; + }); + } + + else + return; // TODO: notify delegate of error & display something + }]; + + return self; +} + +@end diff --git a/MapView/MapView.xcodeproj/project.pbxproj b/MapView/MapView.xcodeproj/project.pbxproj index 39c8badad..08290d818 100755 --- a/MapView/MapView.xcodeproj/project.pbxproj +++ b/MapView/MapView.xcodeproj/project.pbxproj @@ -124,6 +124,8 @@ DD6A838C1644A26E0097F31F /* TrackingHeading@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = DDA6B8C3155CAB9A003DB5D8 /* TrackingHeading@2x.png */; }; DD6A838D1644A26E0097F31F /* TrackingLocation.png in Resources */ = {isa = PBXBuildFile; fileRef = DDA6B8C0155CAB9A003DB5D8 /* TrackingLocation.png */; }; DD6A838E1644A26E0097F31F /* TrackingLocation@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = DDA6B8C1155CAB9A003DB5D8 /* TrackingLocation@2x.png */; }; + DD7C7E38164C894F0021CCA5 /* RMStaticMapView.h in Headers */ = {isa = PBXBuildFile; fileRef = DD7C7E36164C894F0021CCA5 /* RMStaticMapView.h */; }; + DD7C7E39164C894F0021CCA5 /* RMStaticMapView.m in Sources */ = {isa = PBXBuildFile; fileRef = DD7C7E37164C894F0021CCA5 /* RMStaticMapView.m */; }; DD8CDB4A14E0507100B73EB9 /* RMMapQuestOSMSource.h in Headers */ = {isa = PBXBuildFile; fileRef = DD8CDB4814E0507100B73EB9 /* RMMapQuestOSMSource.h */; }; DD8CDB4B14E0507100B73EB9 /* RMMapQuestOSMSource.m in Sources */ = {isa = PBXBuildFile; fileRef = DD8CDB4914E0507100B73EB9 /* RMMapQuestOSMSource.m */; }; DD8FD7541559E4A40044D96F /* RMUserLocation.h in Headers */ = {isa = PBXBuildFile; fileRef = DD8FD7521559E4A40044D96F /* RMUserLocation.h */; }; @@ -255,6 +257,8 @@ DD5FA1E915E2B020004EB6C5 /* RMLoadingTileView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RMLoadingTileView.h; sourceTree = ""; }; DD5FA1EA15E2B020004EB6C5 /* RMLoadingTileView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RMLoadingTileView.m; sourceTree = ""; }; DD6A83751644A20C0097F31F /* MapBox.bundle */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = MapBox.bundle; sourceTree = BUILT_PRODUCTS_DIR; }; + DD7C7E36164C894F0021CCA5 /* RMStaticMapView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RMStaticMapView.h; sourceTree = ""; }; + DD7C7E37164C894F0021CCA5 /* RMStaticMapView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RMStaticMapView.m; sourceTree = ""; }; DD8CDB4814E0507100B73EB9 /* RMMapQuestOSMSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RMMapQuestOSMSource.h; sourceTree = ""; }; DD8CDB4914E0507100B73EB9 /* RMMapQuestOSMSource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RMMapQuestOSMSource.m; sourceTree = ""; }; DD8FD7521559E4A40044D96F /* RMUserLocation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RMUserLocation.h; sourceTree = ""; }; @@ -484,6 +488,8 @@ B8C974690E8A1A50007D16AD /* RMMapView.h */, B8C9746A0E8A1A50007D16AD /* RMMapView.m */, 12F2031E0EBB65E9003D7B6B /* RMMapViewDelegate.h */, + DD7C7E36164C894F0021CCA5 /* RMStaticMapView.h */, + DD7C7E37164C894F0021CCA5 /* RMStaticMapView.m */, B8C974B30E8A23C5007D16AD /* Coordinate Systems */, B83E64E20E80E73F001663B6 /* Projections */, B83E64CF0E80E73F001663B6 /* Tile Cache */, @@ -645,6 +651,7 @@ DD5FA1EB15E2B020004EB6C5 /* RMLoadingTileView.h in Headers */, DD4195C9162356900049E6BA /* RMBingSource.h in Headers */, DD1E3C6E161F954F004FC649 /* SMCalloutView.h in Headers */, + DD7C7E38164C894F0021CCA5 /* RMStaticMapView.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -792,6 +799,7 @@ DD5FA1EC15E2B020004EB6C5 /* RMLoadingTileView.m in Sources */, DD4195CA162356900049E6BA /* RMBingSource.m in Sources */, DD1E3C6F161F954F004FC649 /* SMCalloutView.m in Sources */, + DD7C7E39164C894F0021CCA5 /* RMStaticMapView.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; };