现在我用完美的输出更新代码。这可以帮助我的朋友放置正确的标记。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
placevcViewController.h
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #进口 #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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
placevcViewController.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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我遇到了我加粗的功能问题。实际上我想通过函数 - (void)位置将我当前位置的坐标从 placevcViewController.m 传递给 fetchurl.m :( CLLocationManager * )地址withCallback:(SEL)sel withDelegate:(id)delegate; ,但不知何故它不起作用。函数不正确或我没有使用正确的数据类型来获取坐标。
更新: 我已经在fetchurl.m文件中更新了上面的代码(你可以知道明星中的代码是否已经升级),并且更新有助于我获得多个位置的数组。但现在我没有得到如何在每个位置添加标记。
答案 0 :(得分: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实例的所有者处理它。即:
- (NSDictionary*)fetchData:(NSData*)data {
//do the work and create a dictionary
//return theDictionary
}
然后只使用placevcViewController中的结果:
NSDictionary* results = [fu fetchData:someData];
//now work with the results