2012-01-03 15 views
0

マップを読み込んで現在の場所を取得し、郵便番号の半径に基づいて地域の会社のデータベースを検索します。私はforループを各アドレス、順方向ジオコードをループするようにしています。今度はそれぞれの場所に目印と注釈を付けたいと思います。どうすればこれを達成できますか?複数の目印と注釈を追加する方法

- (void)locationUpdate:(CLLocation *)location { 
locationLabel.text = [location description]; 
NSNumber *lat = [[NSString alloc] initWithFormat:@"%g", location.coordinate.latitude]; 
float latValue = [lat floatValue]; 
NSNumber *lng = [[NSString alloc] initWithFormat:@"%g", location.coordinate.longitude]; 
float lngValue = [lng floatValue]; 

mapView=[[MKMapView alloc] initWithFrame:self.view.bounds]; 
mapView.showsUserLocation=TRUE; 
mapView.mapType=MKMapTypeStandard; 
mapView.delegate=self; 

/*Region and Zoom*/ 
MKCoordinateRegion region; 
MKCoordinateSpan span; 
span.latitudeDelta=0.2; 
span.longitudeDelta=0.2; 

CLLocationCoordinate2D location1=mapView.userLocation.coordinate; 

location1.latitude=latValue; 
location1.longitude=lngValue; 
region.span=span; 
region.center=location1; 

/*Geocoder Stuff*/ 

geoCoder=[[MKReverseGeocoder alloc] initWithCoordinate:location1]; 
geoCoder.delegate=self; 
[geoCoder start]; 

[mapView setRegion:region animated:TRUE]; 
[mapView regionThatFits:region]; 
[self.view insertSubview:mapView atIndex:0]; 
} 
- (void)reverseGeocoder:(MKReverseGeocoder *)geoCoder didFindPlacemark:(MKPlacemark *)placemark 
{ 
NSDictionary *zipDic = [placemark addressDictionary]; 
NSString *zipCode = [zipDic objectForKey:@"ZIP"]; 
NSString *post = [NSString stringWithFormat:@"zip=%@", zipCode]; 
NSString *hostString = @"https://www.mysite.com/searchzip.php?"; 

// Append string and add percent escapes 
hostString = [[hostString stringByAppendingString:post] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
NSURL *hostURL = [NSURL URLWithString:hostString]; 
NSString *jsonString = [[NSString alloc] initWithContentsOfURL:hostURL]; 
self.zipArray = [jsonString JSONValue]; 
NSLog(@"%@", zipArray); 
for (NSString *sZip in zipArray) { 
    NSString *lblAddress = [sZip objectForKey:@"address"]; 
    NSString *hostStr = [[@"http://maps.google.com/maps/geo?q=" stringByAppendingString:lblAddress]stringByAppendingString:@"&key=ABQIAAAA1KqXKe5yJPkX6ii6Ud K-0RSIvIZDM4KnjydqrehqKK56hFf_fxQc0uyCKoh-4i77-5B0Qfc8Gs223Q&sensor=false&output=json"]; 
    hostStr = [hostStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

    NSURL *hostURL = [NSURL URLWithString:hostStr]; 
    NSString *jsonString = [[NSString alloc] initWithContentsOfURL:hostURL]; 

    SBJSON *parser = [[SBJSON alloc] init]; 

    // parse the JSON string into an object - assuming json_string is a NSString of JSON data 
    NSArray *object = [parser objectWithString:jsonString error:nil]; 

    NSArray *placemarks = [object objectForKey:@"Placemark"]; 
    NSDictionary *mark = [placemarks objectAtIndex:0]; 
    NSDictionary *point = [mark objectForKey:@"Point"]; 
    NSArray *coordinates = [point objectForKey:@"coordinates"]; 
    NSNumber *lat = (NSNumber*)[coordinates objectAtIndex:0]; 
    float latValue = [lat floatValue]; 
    NSNumber *lon = (NSNumber*)[coordinates objectAtIndex:1]; 
    float lonValue = [lon floatValue]; 

      //Here is where I would put placemarks and annotations 
} 

} 
- (void)locationError:(NSError *)error { 
locationLabel.text = [error description]; 
} 

@end 

私が試した...

CLLocationCoordinate2D location = {latitude: latValue, longitude: lonValue}; 
    return location; 

しかし、明らかにその間違っている:ここに私のコードです。 私はそうのような注釈を処理するために別々のMapAnnotationクラスを作成してみました:

#import <Foundation/Foundation.h> 
#import <MapKit/MapKit.h> 
#import "AgencyViewController.h" 
@interface MapAnnotation : NSObject<MKAnnotation> { 
CLLocationCoordinate2D coordinate; 
NSString *subtitletext; 
NSString *titletext; 
} 
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 
@property (readwrite, retain) NSString *titletext; 
@property (readwrite, retain) NSString *subtitletext; 
-(id)initWithCoordinate:(CLLocationCoordinate2D) coordinate; 
- (NSString *)subtitle; 
- (NSString *)title; 
-(void)setTitle:(NSString*)strTitle; 
-(void)setSubTitle:(NSString*)strSubTitle; 

などのようにそれを実装:

CLLocationCoordinate2D newCoord = {latitude: latValue, longitude: lonValue}; 

    MapAnnotation *addAnnotation = [[MapAnnotation alloc] initWithCoordinate:newCoord]; 
    [addAnnotation setTitle:@"The Pin Title"]; 
    [addAnnotation setSubTitle:@"The pin subtitle goes here"]; 

    [mapView addAnnotation:addAnnotation]; 

そして、それdidntの作業のいずれか...

+0

「うまくいかない」とはどういう意味ですか?何が起こっている?アプリがクラッシュしているのですか、注釈が表示されないだけですか?あなたのコードを見るのは良いことですが、何が起こっているのか/起こっていないのかを教えてください。 –

答えて

0

はそれを考え出しました、追加する必要があります

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { 
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"Prospects"]; 
if(pinView == nil) { 
    pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Prospects"]; 
    pinView.pinColor = MKPinAnnotationColorGreen; 
    pinView.animatesDrop = NO; 
    pinView.canShowCallout = YES; 
} else { 
    pinView.annotation = annotation; 
} 
return pinView; 
} 

変更する方法を理解する必要があります私の位置ピンの色。

+0

'pinView.pinColor = MKPinAnnotationColorGreen;' if'文の外側で試してみてください – Frade

関連する問題