Non-JS markers / UIImageView overlays on map?

6 views
Skip to first unread message

Mike

unread,
Nov 7, 2008, 2:01:45 PM11/7/08
to iphone-map-view
Ok, I got my project working well enough for my demo last week using
the Javascript markers, but I'd really like to switch to using an
overlay approach, so I can more directly control the marker images and
have clicks on them invoke other things inside my application...

It seems like others have already done this sort of thing, so I was
wondering if somebody would be willing to share some code, or at least
talk me through the general process so I can implement that sort of
setup.

Any help is appreciated. I'm sure others would love to hear how to do
this as well...

Brian

unread,
Nov 11, 2008, 1:49:58 AM11/11/08
to iphone-map-view
I think there is enough to go on in some of the other threads- you may
want to do a search. I recall a very long thread that had a couple
messages that laid everything out for you. Basically you want to add
some new UI object as a subview to serve as a marker, and as you trap
events such as the map moving, you have to move the marker. And of
course you'll track off-screen markers to see if/when it is time to
make them visible again.

None of the code will be complex I don't think- you just have to be
careful and attentive to details to have everything look seamless.

Good luck

Hassan

unread,
Dec 30, 2008, 10:42:15 PM12/30/08
to iphone-map-view

After scanning this thread i figured out how to do this--very easy...

I'm treating my markers as UIButtons with the image of the google pin.
This way I can just use the native button event handing to make my
buttons clickable. (i've also tweaked my MapWebView so that the
delegate is passed in the init--this way i can create a new
MapWebViewDelegate method to set the starting location in the URL used
to initialize the map view).

- (void) applicationDidFinishLaunching:(UIApplication *)application {
mWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]
bounds]];
mapView = [[[MapView alloc] initWithFrame: [[UIScreen mainScreen]
applicationFrame] delegate:self] autorelease];
[mWindow addSubview:mapView];

self.markerList = [[NSMutableArray alloc] init];

UIButton * marker1 = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *markerImage = [UIImage imageNamed:@"marker.png"];
[marker1 setImage:markerImage forState:UIControlStateNormal];
[marker1 setImage:markerImage forState:UIControlStateHighlighted];
marker1.frame = CGRectMake(0, 0, 35, 36);
[self.markerList addObject:marker1];
[mWindow addSubview:marker1];

UIButton * marker2 = [UIButton buttonWithType:UIButtonTypeCustom];
[marker2 setImage:markerImage forState:UIControlStateNormal];
[marker2 setImage:markerImage forState:UIControlStateHighlighted];
marker2.frame = CGRectMake(0, 0, 35, 36);
[self.markerList addObject:marker2];
[mWindow addSubview:marker2];

UIButton * marker3 = [UIButton buttonWithType:UIButtonTypeCustom];
[marker3 setImage:markerImage forState:UIControlStateNormal];
[marker3 setImage:markerImage forState:UIControlStateHighlighted];
marker3.frame = CGRectMake(0, 0, 35, 36);
[self.markerList addObject:marker3];
[mWindow addSubview:marker3];



[mWindow makeKeyAndVisible];
}

then in the mapCenterUpdatedToPixel delegate method, i figure out how
much the screen has moved and update all the "marker" UIButtons with
the same offset:

#define CENTER_X 160
#define CENTER_Y 230
- (void) mapCenterUpdatedToPixel:(GPoint)pixel
{
long dx = CENTER_X - pixel.x;
long dy = CENTER_Y - pixel.y;
for (int i = 0; i < [self.markerList count]; i++)
{
UIButton * marker = [self.markerList objectAtIndex:i];
CGRect oldFrame = marker.frame;
CGRect newFrame = CGRectMake(oldFrame.origin.x + dx,
oldFrame.origin.y + dy,
oldFrame.size.width,
oldFrame.size.height);
marker.frame = newFrame;
}

}

john

unread,
Feb 8, 2009, 5:44:55 AM2/8/09
to iphone-map-view
Mike,

Were you able to attach an infowindow type bubble, simlar to the
google/apple maps app to your pins? What happens when you click the
pin?
Reply all
Reply to author
Forward
0 new messages