0

现在我正在用完美的输出更新代码。这可以帮助我的朋友放置正确的标记。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

放置vcViewController.h

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #import #import "fetchurl.h" #import

@interface placevcViewController : UIViewController
{
    CLLocationDegrees lat;
    CLLocationDegrees lng;
    CLLocationCoordinate2D local;
}
-(id)init;
-(void)location:(CLLocationManager *)address;
@property (nonatomic, strong) NSDictionary *location;
@property (strong, nonatomic) UITextField *addressfeild;
@property (strong, nonatomic) fetchurl *fu;
@property (strong, nonatomic) GMSMapView *map;
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) GMSCameraPosition *camera;
@end

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

放置vcViewController.m

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#import "placevcViewController.h"

@interface placevcViewController ()

@end

@implementation placevcViewController
@synthesize addressfeild, map,fu,locationManager,camera;
-(id)init
{
    self = [super init];
     location = [[NSDictionary alloc] initWithObjectsAndKeys:@"0.0",@"lat",@"0.0",@"lng",@"Null Island",@"adress",@"NULL Home",@"name", nil];
    return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    locationManager = [[CLLocationManager alloc] init];
    locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
    [locationManager startUpdatingLocation];

    lat = locationManager.location.coordinate.latitude;
    lng = locationManager.location.coordinate.longitude;
    local = CLLocationCoordinate2DMake(lat, lng);

    fu = [[fetchurl alloc]init];

    camera = [GMSCameraPosition cameraWithLatitude:lat longitude: lng zoom:12];
    map = [GMSMapView mapWithFrame:CGRectMake(0, 60, 320, 480) camera:camera];
    [self.view addSubview:map];
        map.settings.myLocationButton = YES;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(200, 65, 100, 40);
    [button setTitle:@"SEARCH" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(search:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

    addressfeild = [[UITextField alloc] initWithFrame:CGRectMake(10, 68, 200, 30)];
    addressfeild.placeholder = @"SEARCH";
    [addressfeild setBorderStyle:UITextBorderStyleRoundedRect];
    [self.view addSubview:addressfeild];

}
 -(IBAction)search:(id)sender
{
    [self location:str1];
}
 -(void)location:(NSString *)address
{
    NSString *baseUrl =@"https://maps.googleapis.com/maps/api/place/nearbysearch/json?";
    NSString *url = [NSString stringWithFormat:@"%@location=%@&radius=10000&name=dominos&sensor=false&key=AIzaSyCczXEpxw19qAXQMEUUA98OsMaOESNSOjM",baseUrl,address];
    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL *queryUrl = [NSURL URLWithString:url];
    NSLog(@"query url%@",queryUrl);
    dispatch_async(dispatch_get_main_queue(), ^{
        NSData *data = [NSData dataWithContentsOfURL:queryUrl];
        [self fetchData:data];
    });
}

-(void)fetchData:(NSData *)data
{
    NSString *data1 = [NSString stringWithUTF8String:[data bytes]];
    // NSLog(@"Response data: %@", data1);
    NSError *error;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    NSArray* results =[json objectForKey:@"results"];
    //NSLog(@"Data is:%@" ,results);
    for (int i = 0;i <[results count]; i++) {
        NSDictionary *result = [results objectAtIndex:i];
        // NSLog(@"Data is %@", result);
        NSString *address = [result objectForKey:@"vicinity"];
        // NSLog(@"Address is %@", address);
        NSString *name = [result objectForKey:@"name"];
        //NSLog(@"name is %@", name);
        NSDictionary *geometry = [result objectForKey: @"geometry"];
        NSDictionary *locations = [geometry objectForKey:@"location"];
        NSString *lat =[locations objectForKey:@"lat"];
        NSString *lng =[locations objectForKey:@"lng"];
        //NSLog(@"longitude is %@", lng);
        NSDictionary *gc = [[NSDictionary alloc]initWithObjectsAndKeys:lat,@"lat",lng,@"lng",address,@"address",name,@"name", nil];
        location = gc;
        double lat1 = [[location objectForKey:@"lat"] doubleValue];
        NSLog(@"Marker position%f",lat1);
        double lng1 = [[location objectForKey:@"lng"] doubleValue];
        NSLog(@"Marker position%f",lng1);
        GMSMarker *marker = [[GMSMarker alloc]init];
        CLLocationCoordinate2D local = CLLocationCoordinate2DMake(lat1, lng1);

        marker.position = local;
        marker.title = [ location objectForKey:@"name"];
        NSLog(@"Address is %@",marker.title);
        marker.snippet = [location objectForKey:@"address"];
        marker.map = map;
        GMSCameraUpdate *cams = [GMSCameraUpdate setTarget:local zoom:12];
        [map animateWithCameraUpdate:cams];
    }

}

    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

    @end

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

我在我加粗的功能上遇到了麻烦。实际上我想通过一个函数将我当前位置的坐标从 placevcViewController.m 传递fetchurl.m -(void)location:(CLLocationManager *)address withCallback:(SEL)sel withDelegate: (id)delegate; ,但不知何故它不起作用。函数不正确,或者我没有使用正确的数据类型来获取坐标。

更新: 我已经更新了 fetchurl.m 文件中的上述代码(你可以 c ** 那里更新了星星中的代码),并且更新帮助我获取了多个位置的数组。但现在我不知道如何在每个位置添加标记

4

1 回答 1

0

要成功使用委托协议模式,您需要一些额外的设置:

PlacevcViewController.h 需要同时定义一个委托属性,并定义协议:

@property (nonatomic, assign) id delegate;

//at the bottom of your file, below the @interface ... @end
@protocol nameOfYourProtocol <NSObject>

-(void)methodForDoingSomething:(SomeClass*)argument;

@end

然后在 PlacevcViewController.m 中,您可以像这样调用您的委托方法:

[self.delegate methodForDoingSomething:anArgument];

然后您分配您的委托并在目标类中实现该方法(在您的情况下为 fetchUrl.h)

//declare conformity to the protocol like this in the header file
@interface fetchurl : NSObject <nameOfYourProtocol>

最后在 fetchUrl.m 文件中:

//assign the delegate to self when creating an instance of fetchUrl
fu = [[fetchUrl alloc] init];
[fu setDelegate:self];

//implement the delegate method
- (void)methodForDoingSomething:(SomeClass*)argument {
    //your code goes here
}

虽然使用委托协议模式没有对错之分,但从我可以告诉您的示例来看,一种更简单的方法可能是在其中定义一个方法fetchUrl,该方法返回您需要的项目并让该 fetchUrl 实例的所有者处理它。IE:

- (NSDictionary*)fetchData:(NSData*)data {
    //do the work and create a dictionary

    //return theDictionary
   }

然后在你的 placevcViewController 中使用结果:

NSDictionary* results = [fu fetchData:someData];
//now work with the results
于 2013-10-29T14:15:21.360 回答