My xcode mapview wont work when combined with webview
- by user1715702
I have a small problem with my mapview. When combining the mapview code with the code for webview, the app does not zoom in on my position correctly (just gives me a world overview where i´m supposed to be somewhere in California - wish I was). And it doesn´t show the pin that I have placed on a specific location.
These things works perfectly fine, as long as the code does not contain anything concerning webview.
Below you´ll find the code. If someone can help me to solve this, I would be som thankful!
ViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#define METERS_PER_MILE 1609.344
@interface ViewController : UIViewController <MKMapViewDelegate>{
BOOL _doneInitialZoom;
IBOutlet UIWebView *webView;
}
@property (weak, nonatomic) IBOutlet MKMapView *_mapView;
@property (nonatomic, retain) UIWebView *webView;
@end
ViewController.m
#import "ViewController.h"
#import "NewClass.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize _mapView;
@synthesize webView;
- (void)viewDidLoad
{
[super viewDidLoad];
[_mapView setMapType:MKMapTypeStandard];
[_mapView setZoomEnabled:YES];
[_mapView setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0,0.0} , {0.0,0.0} };
region.center.latitude = 61.097557;
region.center.longitude = 12.126545;
region.span.latitudeDelta = 0.01f;
region.span.longitudeDelta = 0.01f;
[_mapView setRegion:region animated:YES];
newClass *ann = [[newClass alloc] init];
ann.title = @"Hjem";
ann.subtitle = @"Her bor jeg";
ann.coordinate = region.center;
[_mapView addAnnotation:ann];
NSString *urlAddress = @"http://google.no";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
}
- (void)viewDidUnload
{
[self setWebView:nil];
[self set_mapView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)viewWillAppear:(BOOL)animated {
// 1
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = 61.097557;
zoomLocation.longitude = 12.126545;
// 2
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
// 3
MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];
// 4
[_mapView setRegion:adjustedRegion animated:YES];
}
@end
NewClass.h
#import <UIKit/UIKit.h>
#import <MapKit/MKAnnotation.h>
@interface newClass : NSObject{
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@end
NewClass.m
#import "NewClass.h"
@implementation newClass
@synthesize coordinate, title, subtitle;
@end