Skip to content

Commit 6fd8f3f

Browse files
author
Jesse
committed
Added MapKit plugin for rendering a map control with Pins in your app.
1 parent 9603880 commit 6fd8f3f

File tree

10 files changed

+792
-0
lines changed

10 files changed

+792
-0
lines changed

iPhone/MapKitPlug/example/index.html

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2+
<html>
3+
<head>
4+
<!-- Change this if you want to allow scaling -->
5+
<meta name="viewport" content="width=default-width; user-scalable=no" />
6+
7+
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
8+
9+
<title>MapTest</title>
10+
11+
<!-- iPad/iPhone specific css below, add after your main css >
12+
<link rel="stylesheet" media="only screen and (max-device-width: 1024px)" href="ipad.css" type="text/css" />
13+
<link rel="stylesheet" media="only screen and (max-device-width: 480px)" href="iphone.css" type="text/css" />
14+
-->
15+
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
16+
<script type="text/javascript" charset="utf-8" src="MapKitPlug.js"></script>
17+
<script type="text/javascript" charset="utf-8">
18+
19+
20+
// If you want to prevent dragging, uncomment this section
21+
/*
22+
function preventBehavior(e)
23+
{
24+
e.preventDefault();
25+
};
26+
document.addEventListener("touchmove", preventBehavior, false);
27+
*/
28+
29+
function onBodyLoad()
30+
{
31+
document.addEventListener("deviceready",onDeviceReady,false);
32+
}
33+
34+
function cbMapCallback()
35+
{
36+
alert(arguments[0]);
37+
}
38+
39+
/* When this function is called, PhoneGap has been initialized and is ready to roll */
40+
function onDeviceReady()
41+
{
42+
43+
44+
}
45+
46+
function showMap()
47+
{
48+
var pin = { lat:49.281468,
49+
lon:-123.104446,
50+
title:"Nitobi HQ",
51+
pinColor:"purple",
52+
index:0,
53+
selected:true};
54+
55+
var pin2 = { lat:49.281468,
56+
lon:-123.104446,
57+
title:"Nitobi HQ",
58+
pinColor:"purple",
59+
index:0,
60+
selected:true};
61+
62+
63+
64+
// do your thing!
65+
var _options = {
66+
buttonCallback: "cbMapCallback",
67+
height:360,
68+
diameter:1000,
69+
atBottom:true,
70+
lat:pin.lat,
71+
lon:pin.lon
72+
};
73+
74+
window.plugins.mapKit.showMap();
75+
window.plugins.mapKit.setMapData([pin],_options);
76+
77+
}
78+
79+
function hideMap()
80+
{
81+
window.plugins.mapKit.hideMap();
82+
}
83+
84+
function resizeMap()
85+
{
86+
var pin = { lat:49.281468,
87+
lon:-123.104446,
88+
title:"Nitobi HQ",
89+
pinColor:"purple",
90+
index:0,
91+
selected:true};
92+
93+
var pin2 = { lat:49.281468,
94+
lon:-123.104446,
95+
title:"Nitobi HQ",
96+
pinColor:"purple",
97+
index:0,
98+
selected:true};
99+
100+
101+
102+
// do your thing!
103+
var _options = {
104+
buttonCallback: "cbMapCallback",
105+
height:260,
106+
diameter:1000,
107+
atBottom:true,
108+
lat:pin.lat,
109+
lon:pin.lon
110+
};
111+
112+
window.plugins.mapKit.showMap();
113+
window.plugins.mapKit.setMapData([pin],_options);
114+
}
115+
116+
117+
118+
</script>
119+
</head>
120+
<body onload="onBodyLoad()">
121+
<button style="top:400px;position:absolute;" onclick="showMap()">Show Map</button>
122+
<button style="left:100px;top:400px;position:absolute;" onclick="hideMap()">Hide Map</button>
123+
<button style="left:200px;top:400px;position:absolute;" onclick="resizeMap()">Resize Map</button>
124+
125+
</body>
126+
</html>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// AsyncImage.h
3+
// SabreHotels
4+
//
5+
// Created by Brett Rudd on 19/03/2010.
6+
// Copyright 2010 __MyCompanyName__. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
12+
@interface AsyncImageView : UIView {
13+
//could instead be a subclass of UIImageView instead of UIView, depending on what other features you want to
14+
// to build into this class?
15+
16+
NSURLConnection* connection; //keep a reference to the connection so we can cancel download in dealloc
17+
NSMutableData* data; //keep reference to the data so we can collect it as it downloads
18+
//but where is the UIImage reference? We keep it in self.subviews - no need to re-code what we have in the parent class
19+
20+
}
21+
22+
- (void)loadImageFromURL:(NSURL*)url;
23+
- (void)loadDefaultImage;
24+
- (UIImage*) image;
25+
26+
@end
27+
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//
2+
// AsyncImageView.m
3+
// Postcard
4+
//
5+
// Created by markj on 2/18/09.
6+
// Copyright 2009 Mark Johnson. You have permission to copy parts of this code into your own projects for any use.
7+
// www.markj.net
8+
//
9+
10+
#import "AsyncImageView.h"
11+
12+
13+
// This class demonstrates how the URL loading system can be used to make a UIView subclass
14+
// that can download and display an image asynchronously so that the app doesn't block or freeze
15+
// while the image is downloading. It works fine in a UITableView or other cases where there
16+
// are multiple images being downloaded and displayed all at the same time.
17+
18+
@implementation AsyncImageView
19+
20+
- (void)dealloc {
21+
[connection cancel]; //in case the URL is still downloading
22+
[connection release];
23+
[data release];
24+
[super dealloc];
25+
}
26+
27+
28+
- (void)loadImageFromURL:(NSURL*)url {
29+
if (connection!=nil) { [connection release]; } //in case we are downloading a 2nd image
30+
if (data!=nil) { [data release]; }
31+
32+
NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
33+
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; //notice how delegate set to self object
34+
//TODO error handling, what if connection is nil?
35+
}
36+
37+
38+
//the URL connection calls this repeatedly as data arrives
39+
- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData {
40+
if (data==nil) { data = [[NSMutableData alloc] initWithCapacity:2048]; }
41+
[data appendData:incrementalData];
42+
}
43+
44+
//the URL connection calls this once all the data has downloaded
45+
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
46+
//so self data now has the complete image
47+
[connection release];
48+
connection=nil;
49+
if ([[self subviews] count]>0) {
50+
//then this must be another image, the old one is still in subviews
51+
[[[self subviews] objectAtIndex:0] removeFromSuperview]; //so remove it (releases it also)
52+
}
53+
54+
//make an image view for the image
55+
UIImageView* imageView = [[[UIImageView alloc] initWithImage:[UIImage imageWithData:data]] autorelease];
56+
//make sizing choices based on your needs, experiment with these. maybe not all the calls below are needed.
57+
imageView.contentMode = UIViewContentModeScaleAspectFit;
58+
imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth || UIViewAutoresizingFlexibleHeight );
59+
[self addSubview:imageView];
60+
imageView.frame = self.bounds;
61+
[imageView setNeedsLayout];
62+
[self setNeedsLayout];
63+
64+
[data release]; //don't need this any more, its in the UIImageView now
65+
data=nil;
66+
}
67+
68+
//in case we want a local image
69+
- (void)loadDefaultImage {
70+
71+
if ([[self subviews] count]>0) {
72+
//then this must be another image, the old one is still in subviews
73+
[[[self subviews] objectAtIndex:0] removeFromSuperview]; //so remove it (releases it also)
74+
}
75+
76+
//make an image view for the image
77+
UIImageView* imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon.png"]] autorelease];
78+
//make sizing choices based on your needs, experiment with these. maybe not all the calls below are needed.
79+
imageView.contentMode = UIViewContentModeScaleAspectFit;
80+
imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth || UIViewAutoresizingFlexibleHeight );
81+
[self addSubview:imageView];
82+
imageView.frame = self.bounds;
83+
[imageView setNeedsLayout];
84+
[self setNeedsLayout];
85+
86+
}
87+
88+
//just in case you want to get the image directly, here it is in subviews
89+
- (UIImage*) image {
90+
UIImageView* iv = [[self subviews] objectAtIndex:0];
91+
return [iv image];
92+
}
93+
94+
@end

iPhone/MapKitPlug/src/MapKit.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// UIControls.h
3+
// PhoneGap
4+
//
5+
6+
#import <Foundation/Foundation.h>
7+
#import <UIKit/UIKit.h>
8+
#import <MapKit/MapKit.h>
9+
10+
#import "PhoneGapCommand.h"
11+
12+
@interface MapKitView : PhoneGapCommand <MKMapViewDelegate>
13+
{
14+
UIView* childView;
15+
MKMapView* mapView;
16+
NSString* buttonCallback;
17+
UIButton* imageButton;
18+
}
19+
20+
@property (nonatomic, retain) NSString *buttonCallback;
21+
@property (nonatomic, retain) UIView* childView;
22+
@property (nonatomic, retain) MKMapView* mapView;
23+
@property (nonatomic, retain) UIButton* imageButton;
24+
25+
- (void)createView;
26+
27+
- (void)showMap:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
28+
29+
- (void)hideMap:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
30+
31+
- (void)destroyMap:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
32+
33+
- (void)setMapData:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
34+
35+
- (void) closeButton:(id)button;
36+
37+
@end

0 commit comments

Comments
 (0)