1
我想繪製mapview中的一個點。 我創建了兩個文件 1.一個擴展了nsobject的文件,以及一個擴展視圖的文件。 下面是這兩個文件的源代碼:mapview不顯示註釋點
#import "gpMapViewAnnotations.h"
@implementation gpMapViewAnnotations
@synthesize title, coordinate;
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d {
title = ttl;
coordinate = c2d;
return self;
}
這是接口文件的代碼。
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface gpMapViewAnnotations : NSObject <MKAnnotation>{
NSString *title;
CLLocationCoordinate2D coordinate;
}
@property (nonatomic, copy) NSString *title;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d;
@end
@end
現在,這是視圖文件的源代碼:
#import <UIKit/UIKit.h>
#import <MapKit/Mapkit.h>
#import "ASIHTTPRequestDelegate.h"
#import "AppDelegate.h"
#import "gpMapViewAnnotations.h"
@interface gpViewController : UIViewController
< ASIHTTPRequestDelegate, MKMapViewDelegate>
{
MKMapView *gpMapView;
gpMapViewAnnotations *gpAnnotations;
}
@property (strong, nonatomic) IBOutlet MKMapView *gpMapView;
@end
,這裏是實現文件:
#import "gpViewController.h"
#import "ASIHTTPRequest.h"
#import "Parser.h"
#import "AppDelegate.h"
#import "TFHpple.h"
#import "TouchXML.h"
#import "gpMapViewAnnotations.h"
@interface gpViewController()
@end
@implementation gpViewController
@synthesize gpMapView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// Set some coordinates for our position (Buckingham Palace!)
CLLocationCoordinate2D location;
location.latitude = (double) 51.501468;
location.longitude = (double) -0.141596;
// Add the annotation to our map view
gpMapViewAnnotations *newAnnotation = [[gpMapViewAnnotations alloc] initWithTitle:@"Buckingham Palace" andCoordinate:location];
[gpMapView addAnnotation:newAnnotation];
NSURL *url = [NSURL URLWithString:@"http://v1.syndication.nhschoices.nhs.uk/organisations/gppractices/location.xml?apikey=YAHUGHET&range=50&northing=174900&easting=517900&name=richmond"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
[gpMapView setDelegate:self];
}
- (void)viewDidUnload
{
[self setGpMapView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSLog(@"view did load");
NSLog(@"request finished");
// Use when fetching text data
NSString *responseString = [request responseString];
// NSLog(@"%@",responseString);
// Use when fetching binary data
NSData *responseData = [request responseData];
// we will put parsed data in an a array
NSMutableArray *res = [[NSMutableArray alloc] init];
// using local resource file
// NSString *XMLPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"test.xml"];
// NSData *XMLData = [NSData dataWithContentsOfFile:responseString];
CXMLDocument *doc = [[CXMLDocument alloc] initWithData:responseData options:0 error:nil];
NSArray *nodes = NULL;
// searching for piglet nodes
nodes = [doc nodesForXPath:@"//*" error:nil];
NSLog(@"coming here 1");
for (CXMLElement *node in nodes) {
//NSLog(@"coming here 2");
// NSMutableArray *item = [[NSMutableArray alloc]init];
//create list object to store the title and latitude and longitude.
List *gpList = [[List alloc]init];
if([[node localName] isEqualToString:@"entry"]){
NSArray *entryTagNodes = [node children];
//iterate on entry nodes
for(CXMLElement *entryTagNode in entryTagNodes){
//check if the tagname is content
if([[entryTagNode localName] isEqualToString:@"content"]){
//iterat on content tag childs
NSArray *contentTagNodes = [entryTagNode children];
// NSLog(@"tag: %d",[contentTagNodes count]);
for(CXMLElement *innerContentNode in contentTagNodes){
NSArray *organisationTagNodes = [innerContentNode children];
for(CXMLElement *orgTagNode in organisationTagNodes){
// NSLog(@"%@",[orgTagNode localName]);
//check for tag name is equal to name
if([[orgTagNode localName] isEqualToString:@"name"]){
gpList.title = [orgTagNode stringValue];
}else if([[orgTagNode localName] isEqualToString:@"geographicCoordinates"]){
NSArray *geoCordTags = [orgTagNode children];
for(CXMLElement *geoCordTag in geoCordTags){
if([[geoCordTag localName] isEqualToString:@"latitude"]){
gpList.latitude = [geoCordTag stringValue];
}else if([[geoCordTag localName] isEqualToString:@"longitude"]){
gpList.longitude = [geoCordTag stringValue];
}
}
NSLog(@"latitude nad longitude %@ %@",gpList.latitude,gpList.longitude);
}
}
}
}
}
[res addObject:gpList];
}
// and here it is - attributeForName! Simple as that.
// [item setObject:[[node valueForKey:@"id"] stringValue] forKey:@"id"]; // <------ this magical arrow is pointing to the area of interest
}
// and we print our results
// NSLog(@"%@", res);
NSEnumerator *enumerator = [res objectEnumerator];
id obj;
while(obj = [enumerator nextObject]){
NSLog(@"title %@",[obj title]);
}
/*
TFHpple *doc = [TFHpple hppleWithData:responseData isXML:YES];
NSArray *elements = [doc searchWithXPathQuery:@"/"];
NSLog(@"%d",[elements objectAtIndex:0]);
*/
/*
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:responseData];
Parser *parser = [[Parser alloc] initParser];
[xmlParser setDelegate:parser];
BOOL worked = [xmlParser parse];
if(worked){
NSLog(@"parsing is happening ");
}else{
NSLog(@"parsing is not happening %@",[app.listArray count]);
}*/
}
-(void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views{
MKAnnotationView *annotationView = [views objectAtIndex:0];
id <MKAnnotation> mp = [annotationView annotation];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate], 1500, 1500);
[mv setRegion:region animated:YES];
[mv selectAnnotation:mp animated:YES];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(@"%@",error);
}
@end
我沒有一個線索我在繪製一個簡單的點時做錯了。我仍然需要使用我創建的讀取XML文件的數組放置多個點。但即使是一個簡單的觀點現在也沒有被繪製出來。 請問我在這裏做錯了什麼?
你能解釋一下代碼和部分,我需要改變爲accroding我的代碼? – Maverick
您必須將此方法添加到您的mapview委託。 –
我很抱歉,我對此有點新鮮感。請您解釋一下哪個代表?我已經添加了協議mkmapviewDelegate到接口。 – Maverick