XCode Weather Radar Map Overlay on Mapkit - overlay

I cant seem to figure this out with loading a custom map overlay from a url. As you can see its load the weather overlay from a URL that adds it to the mapview but it doesnt seem to be working and I cant see why it isnt working. Any help is appreciated.
radarView.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface radarViewController : UIViewController <MKMapViewDelegate> {
MKMapView *mapview;
}
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
#property (nonatomic, readwrite) MKCoordinateRegion region;
#property (nonatomic, readonly) MKMapRect visibleRect;
#property (nonatomic, retain) IBOutlet MKMapView *mapview;
-(IBAction)setMap:(id)sender;
-(IBAction)goBack;
#end
radarView.m
#import "radarViewController.h"
#import "MapOverlay.h"
#import "MapOverlayView.h"
#implementation radarViewController
#synthesize mapview,coordinate,visibleRect,region;
- (void)viewDidLoad
{
[super viewDidLoad];
[mapview setDelegate:self];
[mapview setZoomEnabled:YES];
[mapview setScrollEnabled:YES];
[mapview setMapType:MKMapTypeSatellite];
NSURL *imageURL = [NSURL URLWithString: #"http://radar.weather.gov/ridge/Conus/RadarImg/latest_radaronly.gif"];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
//UIImage * image = [UIImage imageWithData:imageData];
MapOverlay *overlay = [[MapOverlay alloc] initWithImageData:imageData withLowerLeftCoordinate:CLLocationCoordinate2DMake(21.65253888888889, -129.314525) withUpperRightCoordinate:CLLocationCoordinate2DMake(50.406625, -65.60158888888888)];
[mapview addOverlay:overlay];
[mapview setVisibleMapRect:[overlay boundingMapRect]];
[overlay release];
visibleRect = [mapview mapRectThatFits:overlay.boundingMapRect];
mapview.visibleMapRect = visibleRect;
region = MKCoordinateRegionForMapRect(visibleRect);
region.center.latitude = 26.503292;
region.center.longitude = -82.032353;
region.span.longitudeDelta = 0.4f;
region.span.latitudeDelta = 0.4f;
[mapview regionThatFits:region];
[mapview setRegion:region animated:YES];
[mapview setClipsToBounds:true];
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
MapOverlay *mapOverlay = overlay;
MapOverlayView *mapOverlayView = [[[MapOverlayView alloc] initWithOverlay:mapOverlay] autorelease];
return mapOverlayView;
}
#end
MapOverlay.h and .m
#import <MapKit/MapKit.h>
#interface MapOverlay : NSObject <MKOverlay> {
MKMapRect boundingMapRect;
}
#property (nonatomic, retain) NSData *radarData;
- (id) initWithImageData: (NSData*) imageData withLowerLeftCoordinate:
(CLLocationCoordinate2D) lowerLeftCoordinate withUpperRightCoordinate:
(CLLocationCoordinate2D) upperRightCoordinate;
#end
#import "MapOverlay.h"
#implementation MapOverlay
#synthesize radarData,coordinate,boundingMapRect;
- (id) initWithImageData: (NSData*) imageData withLowerLeftCoordinate: (CLLocationCoordinate2D) lowerLeftCoordinate withUpperRightCoordinate: (CLLocationCoordinate2D) upperRightCoordinate{
self.radarData = imageData;
MKMapPoint lowerLeft = MKMapPointForCoordinate(lowerLeftCoordinate);
MKMapPoint upperRight = MKMapPointForCoordinate(upperRightCoordinate);
boundingMapRect = MKMapRectMake(lowerLeft.x, upperRight.y, upperRight.x - lowerLeft.x, upperRight.y - lowerLeft.y);
return self;
}
- (CLLocationCoordinate2D)coordinate
{
return MKCoordinateForMapPoint(MKMapPointMake(MKMapRectGetMidX(boundingMapRect),
MKMapRectGetMidY(boundingMapRect)));
}
- (MKMapRect)boundingMapRect
{
return boundingMapRect;
}
#end
MapOverlayView.h and .m
#import <MapKit/MapKit.h>
#interface MapOverlayView : MKOverlayView {
CGFloat tileAlpha;
}
#property (nonatomic, assign) CGFloat tileAlpha;
#end
#import "MapOverlayView.h"
#import "MapOverlay.h"
#implementation MapOverlayView
#synthesize tileAlpha;
- (id)initWithOverlay:(id <MKOverlay>)overlay
{
if (self = [super initWithOverlay:overlay]) {
self.tileAlpha = 0.8;
}
return self;
}
- (void)drawMapRect:(MKMapRect)mapRect
zoomScale:(MKZoomScale)zoomScale
inContext:(CGContextRef)context
{
MapOverlay *mapOverlay = (MapOverlay *)self.overlay;
CGContextSetAlpha(context, tileAlpha);
UIImage *image = [[UIImage alloc] initWithData:mapOverlay.radarData];
NSString *strData = [[NSString alloc]initWithData:mapOverlay.radarData encoding:NSUTF8StringEncoding];
NSLog(#"%#",strData);
MKMapRect theMapRect = [self.overlay boundingMapRect];
CGRect theRect = [self rectForMapRect:theMapRect];
UIGraphicsPushContext(context);
[image drawInRect:theRect blendMode:kCGBlendModeNormal alpha:1.0];
UIGraphicsPopContext();
[image release];
}
- (BOOL)canDrawMapRect:(MKMapRect)mapRect
zoomScale:(MKZoomScale)zoomScale
{
return YES;
}
#end

Related

change location to coordinates and also call didUpdateUserLocation method

This is my implementation file :
#import "mapViewController.h"
#interface mapViewController ()
#end
#implementation mapViewController
#synthesize mapView,source,dest,latdest,latsource,longdest,longsource;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
dest=#"delhi";
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
CLGeocoder *geocoder1 = [[CLGeocoder alloc] init];
[geocoder1 geocodeAddressString:source
completionHandler:^(NSArray* placemarks, NSError* error)
{
for (CLPlacemark* aPlacemark in placemarks)
{
CLLocationCoordinate2D coordinate;
coordinate.latitude = aPlacemark.location.coordinate.latitude;
latsource=&coordinate.latitude;
coordinate.longitude = aPlacemark.location.coordinate.longitude;
longsource=&coordinate.longitude;
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotation setCoordinate:(coordinate)];
[annotation setTitle:source];
annotation.subtitle = #"I'm here!!!";
[self.mapView addAnnotation:annotation];
}
}];
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
MKCoordinateRegion region =MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = userLocation.coordinate;
point.title = #"Where am I?";
point.subtitle = #"I'm here!!!";
[self.mapView addAnnotation:point];
[self.view addSubview:self.mapView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
This is my header file :
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface mapViewController : UIViewController <MKMapViewDelegate>
#property (strong, nonatomic) IBOutlet MKMapView *mapView;
#property(strong,nonatomic) NSString *source,*dest;
#property(nonatomic) CLLocationDegrees *latsource,*longsource;
#property(nonatomic) CLLocationDegrees *latdest,*longdest;
#end
First i want to know why didUpdateUserLocation method is never called.I also want to know the code to add a destination whose coordinates are stored in latdest and longdest.Both of them will get their values from static variable "dest" which has the value "delhi" in it .My final aim is to trace a route on the map from source coordinates(latsource,longsource) to destination coordinates(latest,longdest).
I am new to ios development so i might have done some noob mistakes.
didUpdateUserLocation method won't be called if you not set mapView.delegate = self;
If you use a class, which has delegate methods, and you want to use them, everytime you should set its delegate = self
Don't use the same name as in delegates: mapView
in header:
#property (weak, nonatomic) IBOutlet MKMapView *myMapView;
be sure you connected the IBOutlet MKMapView *myMapView in IterfaceBuilder
in implementation file:
#synthesize myMapView;
in your - (void)viewDidLoad
myMapView.delegate = self;
you have to correct all self.mapView to myMapView
You made a really big mistake:
if you use IBOutlet you have to add on InterfaceBuilder , and connect it.
or you can create everything from code:
#property (weak, nonatomic) MKMapView *myMapView;
myMapView = [[MKMapView alloc] initWithFrame:CGRectMake(0,0,320,480)];
then add the screen with
[self.view addSubview:myMapView];

how do i use UISlider to change the MKMapView zoomIn and ZoomOut in iphone?

how do i use the UISlider to change the zoom value of a MKMapView.
i tried this code but in works not perfectly,
- (IBAction)slideAction:(id)sender
{
span.latitudeDelta = 125*(1-slideValue.value)+0.01;
span.longitudeDelta = 0.001;
region.span = span;
region.center=map.centerCoordinate;
[map setRegion:region animated:TRUE];
}
any suggestions? i think their is no default zoom controller for MKMap?
Note that your longitudeDelta needs to change or it probably won't zoom.
You might find this useful: https://github.com/calabash/calabash-ios-server/blob/master/calabash/Classes/MapKit/MKMapView%2BZoomLevel.m
(I copied most of it from http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/)
//
// ViewController.m
// MapKitRegion
#import "ViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import <AddressBook/AddressBook.h>
#interface ViewController () <MKMapViewDelegate>
#property (strong, nonatomic) IBOutlet UITextView *txtAddress;
#property (strong, nonatomic) IBOutlet UIButton *btnGetMap;
#property (strong, nonatomic) IBOutlet UISegmentedControl *segType;
#property (strong, nonatomic) IBOutlet UISlider *slideZoom;
#property (strong, nonatomic) IBOutlet MKMapView *mapView;
#property CLLocationCoordinate2D coord;
- (IBAction)btnGetMapTouched:(id)sender;
- (IBAction)segTypeChanged:(id)sender;
- (IBAction)slideZoomChanged:(id)sender;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_mapView.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction)btnGetMapTouched:(id)sender {
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:self.txtAddress.text
completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
NSLog(#"Geocode failed with error: %#", error);
return;
}
if(placemarks && placemarks.count > 0) {
CLPlacemark *placemark = placemarks[0];
_coord = placemark.location.coordinate;
_mapView.centerCoordinate = _coord;
[self SetZoom];
}
}];
}
- (IBAction)segTypeChanged:(id)sender {
if (self.segType.selectedSegmentIndex == 0)
_mapView.mapType = MKMapTypeStandard;
else
_mapView.mapType = MKMapTypeSatellite;
}
- (IBAction)slideZoomChanged:(id)sender {
[self SetZoom];
}
- (void) SetZoom {
int meters = self.slideZoom.value * 30000;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance (_coord, meters, meters);
[_mapView setRegion:region animated:NO];
}
#end

Change UIView's background color

I am doing some exercises with delegate, but now I have a problem with a UIView. This is my storyboard
I want to change the color of the UIView with 3 UISliders. The range of UISliders is from 0 to 255.
And this is my code:
ColorField is the UIView custom class
ColorField.h
#import <UIKit/UIKit.h>
#protocol ColorFieldDelegate <NSObject>
-(NSArray *)giveMeColors;
#end
#interface ColorField : UIView
#property (nonatomic , weak) IBOutlet id<ColorFieldDelegate> delegate;
#end
ColorField.m
#import "ColorField.h"
#implementation ColorField
#synthesize delegate = _delegate;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
NSArray *arrayOfColors = [self.delegate giveMeColors];
int red = [[arrayOfColors objectAtIndex:0] intValue];
int green = [[arrayOfColors objectAtIndex:1] intValue];
int blue = [[arrayOfColors objectAtIndex:2] intValue];
NSLog(#"Red --> %d" ,red);
NSLog(#"Green --> %d" ,green);
NSLog(#"Blue --> %d \n\n" ,blue);
self.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}
#end
ColorViewController.h
#import <UIKit/UIKit.h>
#import "ColorField.h"
#interface ColorViewController : UIViewController <ColorFieldDelegate>
#property (nonatomic) IBOutlet ColorField *colorField;
#property (weak, nonatomic) IBOutlet UISlider *redSlider;
#property (weak, nonatomic) IBOutlet UISlider *greenSlider;
#property (weak, nonatomic) IBOutlet UISlider *blueSlider;
#end
ColorViewController.m
#import "ColorViewController.h"
#interface ColorViewController ()
#property (nonatomic) double redQuantity;
#property (nonatomic) double greenQuantity;
#property (nonatomic) double blueQuantity;
#end
#implementation ColorViewController
#synthesize colorField = _colorField;
#synthesize redSlider;
#synthesize greenSlider;
#synthesize blueSlider;
#synthesize redQuantity;
#synthesize blueQuantity;
#synthesize greenQuantity;
- (void)viewDidLoad
{
[super viewDidLoad];
[self.colorField setDelegate:self];
[self.colorField setNeedsDisplay];
self.redQuantity = 125.0;
self.blueQuantity = 125.0;
self.greenQuantity = 125.0;
[self.colorField setNeedsDisplay];
// Do any additional setup after loading the view, typically from a nib.
}
-(ColorField *)colorField
{
if (_colorField == nil) {
_colorField = [[ColorField alloc] init];
}
return _colorField;
}
- (void)viewDidUnload
{
[self setColorField:nil];
[self setRedSlider:nil];
[self setGreenSlider:nil];
[self setBlueSlider:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
-(IBAction)changeRedQuantity:(UISlider *)sender
{
//methods of UISliders
self.redQuantity = [sender value];
[self.colorField setNeedsDisplay];
}
-(IBAction)changeBlueQuantity:(UISlider *)sender
{
self.blueQuantity = [sender value];
[self.colorField setNeedsDisplay];
}
-(IBAction)changeGreenQuantity:(UISlider *)sender
{
self.greenQuantity = [sender value];
[self.colorField setNeedsDisplay];
}
-(NSArray *)giveMeColors
{
NSNumber *redNumber = [NSNumber numberWithDouble:self.redQuantity];
NSNumber *greenNumber = [NSNumber numberWithDouble:self.greenQuantity];
NSNumber *blueNumber = [NSNumber numberWithDouble:self.blueQuantity];
NSArray *array = [[NSArray alloc] initWithObjects:redNumber, greenNumber,blueNumber, nil];
return array;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
BUT...this is the result: It show me only RED, GREEN and BLUE colors without gradation, for example: RGB (255,0,75) is THIS
and not THIS:
I don't know with it can't show me gradation...
Thanks!!
Marco Manzoni
You have to divide your slidervalues by 255.0.
[UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1.0];
colorWithRed only accepts values 0.0-1.0

rightCalloutAccessoryView on MKMapView not show

I have little problem with my iphone application. I have such classes and interfaces:
CinemaMapAnnotation.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface CinemaMapAnnotation : NSObject <MKAnnotation>{
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
#property (nonatomic, assign) CLLocationCoordinate2D coordinate;
#property (nonatomic, copy) NSString* title;
#property (nonatomic, copy) NSString* subtitle;
-(id)initWithCoordinate:(CLLocationCoordinate2D) c;
#end
CinemaMapAnnotation.m
#import "CinemaMapAnnotation.h"
#implementation CinemaMapAnnotation
#synthesize title, subtitle, coordinate;
-(id)initWithCoordinate:(CLLocationCoordinate2D) c{
self = [super init];
if (self) {
coordinate = c;
}
return self;
}
-(void) dealloc {
self.title = nil;
self.subtitle = nil;
[super dealloc];
}
#end
CinemasMapController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "Cinema.h"
#import "CinemaMapAnnotation.h"
#import "PopcornuaAppDelegate.h"
#interface CinemasMapController : UIViewController <MKMapViewDelegate, MKReverseGeocoderDelegate> {
MKMapView *mapView;
MKReverseGeocoder *reverseGeokoder;
}
#property (nonatomic, retain) IBOutlet MKMapView *mapView;
#property (nonatomic, retain) IBOutlet MKReverseGeocoder *reverseGeokoder;
#end
CinemasMapController.m
#import "CinemasMapController.h"
#implementation CinemasMapController
#synthesize mapView, reverseGeokoder;
...
#pragma mark - Map Anotation
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id)annotation
{
static NSString* MyIdentifier = #"CinemaMapAnotation";
MKPinAnnotationView* pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:MyIdentifier];
if (!pinView)
{
MKPinAnnotationView* pinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:MyIdentifier] autorelease];
pinView.draggable = NO;
pinView.animatesDrop = NO;
pinView.enabled = YES;
} else {
pinView.annotation = annotation;
}
if(annotation != mapView.userLocation){
pinView.pinColor = MKPinAnnotationColorRed;
pinView.canShowCallout = YES;
// Add a detail disclosure button to the callout.
pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinView.userInteractionEnabled = YES;
} else {
pinView.pinColor = MKPinAnnotationColorGreen;
}
return pinView;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
NSLog(#"Not show button, so not work");
}
...
#end
My problem is, what all show and work, except not show rightCalloutAccessoryView button. mapView connected and have delegete to CinemasMapController on iphone view (also I try [mapView setDelegate:self]). So what I do wrong?
P.S. Code with line
pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
execute - checked by NSLog.
Here how its look like - no button:
In viewForAnnotation, in the if (!pinView) block, a new local pinView variable is being declared instead of getting assigned to the outer variable.
As a result, the outer pinView variable never gets set (stays nil) and so the map view creates the default annotation view which is what you see.

Problems Adding MKPolygon as an Overlay to an MKMapView

Ey there, so as the title says, I am having a tough time adding an MKPolygon as an overlay to an MKMapView. Here is the relevant code:
ParkingMapViewContoller.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface ParkingMapViewController : UIViewController <MKMapViewDelegate> {
MKMapView *mapView;
}
#property (nonatomic, retain) IBOutlet MKMapView *mapView;
-(void)loadAnnotations;
-(void)showCurrentLocationButtonTapped:(id)sender;
#end
ParkingMapViewController.m
//...
#import "ParkingRegionOverlay.h"
//...
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"%#",self.title);
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:100 target:self action:#selector(showCurrentLocationButtonTapped:)];
/*MKMapPoint points[3] = {{38.53607,-121.765793}, {38.537606,-121.768379}, {38.53487,-121.770578}};
MKPolygon *polygon = [MKPolygon polygonWithPoints:points count:3];*/
ParkingRegionOverlay *polygon = [[ParkingRegionOverlay alloc] initialize];
[mapView addOverlay:polygon];
[self loadAnnotations];
CLLocationCoordinate2D centerCoord = { UCD_LATITUDE, UCD_LONGITUDE };
[mapView setCenterCoordinate:centerCoord zoomLevel:13 animated:NO]; //from "MKMapView+ZoomLevel.h"
}
//...
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
NSLog(#"in viewForOverlay!");
if ([overlay isKindOfClass:[MKPolygon class]])
{
MKPolygonView* aView = [[[MKPolygonView alloc] initWithPolygon:(MKPolygon*)overlay] autorelease];
aView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
aView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
aView.lineWidth = 3;
return aView;
}
return nil;
}
//...
ParkingRegionOverlay.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface ParkingRegionOverlay : NSObject <MKOverlay> {
//CLLocationCoordinate2D origin;
MKPolygon *polygon;
//MKMapRect rect;
}
//#property (nonatomic) CLLocationCoordinate2D origin;
#property (nonatomic, retain) MKPolygon *polygon;
//#property (nonatomic) MKMapRect rect;
-(ParkingRegionOverlay*)initialize;
-(MKMapRect)boundingMapRect;
-(CLLocationCoordinate2D)coordinate;
#end
ParkingRegionOverlay.m
#import "ParkingRegionOverlay.h"
#implementation ParkingRegionOverlay
//#synthesize origin;
#synthesize polygon;
//#synthesize rect;
-(ParkingRegionOverlay*) initialize {
MKMapPoint points[3] = {{38.53607,-121.765793}, {38.537606,-121.768379}, {38.53487,-121.770578}};
polygon = [MKPolygon polygonWithPoints:points count:3];
polygon.title = #"Some Polygon";
return self;
}
- (MKMapRect)boundingMapRect{
MKMapRect bounds = MKMapRectMake(-121.770578,38.537606,-121.770578-(-121.765793),38.537606-38.53487);
return bounds;
}
- (CLLocationCoordinate2D)coordinate{
return CLLocationCoordinate2DMake((38.537606-38.53487)/2, (-121.770578-(-121.765793))/2);
}
#end
You see that NSLog I stuck in the viewForOverlay: method? Well that never shows up in the console, so that function is never called. Any idea of what's wrong? Many thanks!
The main issue is that the code is giving the map view latitude/longitude coordinates where it expects MKMapPoints. For an explanation of the difference, see "Understanding Map Geometry" in the Location Awareness Programming Guide. Use the MKMapPointForCoordinate function to convert from lat/long coordinates to an MKMapPoint.
The second issue is that in viewForOverlay, it is checking if overlay is of type MKPolygon. Your overlay class ParkingRegionOverlay contains an MKPolygon object inside it but is not itself of type MKPolygon.
To fix the main issue, you need to change the initialize and boundingMapRect methods:
-(id)init {
if (self = [super init]) {
MKMapPoint points[3];
CLLocationCoordinate2D c1 = {38.53607,-121.765793};
points[0] = MKMapPointForCoordinate(c1);
CLLocationCoordinate2D c2 = {38.537606,-121.768379};
points[1] = MKMapPointForCoordinate(c2);
CLLocationCoordinate2D c3 = {38.53487,-121.770578};
points[2] = MKMapPointForCoordinate(c3);
polygon = [MKPolygon polygonWithPoints:points count:3];
polygon.title = #"Some Polygon";
}
return self;
}
- (MKMapRect)boundingMapRect{
CLLocationCoordinate2D corner1 =
CLLocationCoordinate2DMake(38.537606, -121.770578);
MKMapPoint mp1 = MKMapPointForCoordinate(corner1);
CLLocationCoordinate2D corner2 =
CLLocationCoordinate2DMake(38.53487, -121.765793);
MKMapPoint mp2 = MKMapPointForCoordinate(corner2);
MKMapRect bounds =
MKMapRectMake(mp1.x, mp1.y, (mp2.x-mp1.x), (mp2.y-mp1.y));
return bounds;
}
Please notice by the way that I changed the method "initialize" to "init". Though it wasn't preventing the polygon from showing, the way you are overriding the initialization of ParkingRegionOverlay using a method called "initialize" and not calling [super init] does not follow convention. (Also remove "initialize" from the .h file.)
To fix the second issue, the viewForOverlay method should look like this:
- (MKOverlayView *)mapView:(MKMapView *)mapView
viewForOverlay:(id <MKOverlay>)overlay
{
NSLog(#"in viewForOverlay!");
if ([overlay isKindOfClass:[ParkingRegionOverlay class]])
//^^^^^^^^^^^^^^^^^^^^
{
//get the MKPolygon inside the ParkingRegionOverlay...
MKPolygon *proPolygon = ((ParkingRegionOverlay*)overlay).polygon;
MKPolygonView *aView = [[[MKPolygonView alloc]
initWithPolygon:proPolygon] autorelease];
//^^^^^^^^^^
aView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
aView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
aView.lineWidth = 3;
return aView;
}
return nil;
}
Finally, change the code in viewDidLoad:
ParkingRegionOverlay *polygon = [[ParkingRegionOverlay alloc] init];
[mapView addOverlay:polygon];
[polygon release]; //don't forget this
it seems to me that you forget to set your delegate
somthing like that
_mapView.delegate = self;
Have you set this?:
yourMap.delegate=self;
And what's your target? 3.x or 4.x?? I think MKPolygon is available from 4.0.
You can try to add the import clause to your prefix file like:
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#endif