Ios 将坐标从一个视图传递到另一个视图

Ios 将坐标从一个视图传递到另一个视图,ios,google-maps-api-3,Ios,Google Maps Api 3,现在我正在用完美的输出更新代码。这可能有助于我的朋友放置正确的标记 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #import "placevcViewController.h" @interface placevcViewController () @end @implementation placevcViewController @synthesize addre

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

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

#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.h

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #进口 #导入“fetchurl.h” #进口

@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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#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

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

#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.h”
@接口placevcViewController()
@结束
@视图控制器的实现
@综合地址字段、地图、fu、位置管理器、摄像头;
-(id)init
{
self=[super init];
location=[[NSDictionary alloc]initWithObjectsSandKeys:@“0.0”@“lat”@“0.0”@“lng”@“空岛”@“地址”@“空主页”@“名称”,无];
回归自我;
}
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
//自定义初始化
}
回归自我;
}
-(无效)viewDidLoad
{
[超级视图下载];
locationManager=[[CLLocationManager alloc]init];
locationManager.distanceFilter=KCLDistanceFilterOne;//无论何时移动
locationManager.desiredAccuracy=KCallocationAccuracyHundredMeters;//100米
[locationManager startUpdatingLocation];
lat=locationManager.location.coordinate.latitude;
lng=locationManager.location.coordinate.longitude;
本地=CLLocationCoordination2Dmake(lat,lng);
fu=[[fetchurl alloc]init];
摄像机=[GMSCameraPosition摄像机,纬度:纬度:经度:lng变焦:12];
map=[GMSMapView mapWithFrame:CGRectMake(0,60320480)摄像机:摄像机];
[self.view addSubview:map];
map.settings.myLocationButton=是;
UIButton*button=[UIButton button类型:UIButtonyPeroundRect];
button.frame=CGRectMake(200,65,100,40);
[按钮设置标题:@“搜索”状态:uicontrol状态正常];
[按钮添加目标:自我操作:@selector(搜索:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:按钮];
addressfeild=[[UITextField alloc]initWithFrame:CGRectMake(10,68,200,30)];
addressfeild.placeholder=@“搜索”;
[addressfeild setBorderStyle:UITextBorderStyleRoundedRect];
[self.view addSubview:addressfeild];
}
-(iAction)搜索:(id)发件人
{
[自我定位:str1];
}
-(无效)位置:(NSString*)地址
{
NSString*baseUrl=@”https://maps.googleapis.com/maps/api/place/nearbysearch/json?";
NSString*url=[NSString stringWithFormat:@“%@location=%@&radius=10000&name=dominos&sensor=false&key=AIZASYCCZEXEPXW19QAXQMEUUA98OSMAOESNOSOJM”,基本url,地址];
url=[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL*queryUrl=[NSURL URLWithString:url];
NSLog(@“查询url%@”,queryUrl);
dispatch\u async(dispatch\u get\u main\u queue()^{
NSData*data=[NSData dataWithContentsOfURL:queryUrl];
[自取数据:数据];
});
}
-(void)fetchData:(NSData*)数据
{
NSString*data1=[NSString stringWithUTF8String:[数据字节]];
//NSLog(@“响应数据:%@”,数据1);
n错误*错误;
NSDictionary*json=[NSJSONSerialization JSONObjectWithData:数据选项:针织错误:&错误];
NSArray*results=[json objectForKey:@“results”];
//NSLog(@“数据为%@”,结果);

对于(int i=0;i要成功使用委托协议模式,您需要一些额外的设置:

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
然后分配委托并在目标类中实现该方法(本例中为fetchUrl.h)

虽然使用委托协议模式没有任何对错,但从您的示例中我可以看出,一种更简单的方法可能是在
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

虽然编译代码不是绝对必需的,但您应该始终以大写字母开始命名类,并让实例为小写。PlacevcViewController和FetchUrl是类名,那么实例可以以小写字母开始。我已经在FetchUrl.m文件中更新了上述代码(你可以在那里c**stars中的代码已升级),更新帮助我获得多个位置的数组。但现在我不知道如何在每个位置添加标记。我已在fetchurl.m文件中更新了上述代码(你可以在那里c**stars中的代码已升级),更新帮助我获得多个位置的数组。但现在我不知道如何在每个位置添加标记。
NSDictionary* results = [fu fetchData:someData];
//now work with the results