私は問題があります。Google apis で場所を検索し、地図上に注釈ピンを作成させます。データは dict に格納されます。データを含むコールアウトで別のViewControllerに転送するために、これらの位置を試します。
ここに私のコードの一部があります: .h ファイル:
#import <MapKit/MapKit.h>
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import "MapPoint.h"
#define kGOOGLE_API_KEY @"MyGoogleAPIsKey"
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
@interface FirstViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
CLLocationCoordinate2D currentCentre;
BOOL firstLaunch;
int currenDist;
}
- (IBAction)myButton:(UIBarButtonItem *)sender;
- (IBAction)myPosition:(UIBarButtonItem *)sender;
@property (strong, nonatomic) IBOutlet MKMapView *mapView;
@end
.m ファイル:
@implementation FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//Make this controller the delegate for the map view.
self.mapView.delegate = self;
// Ensure that you can view your own location in the map view.
[self.mapView setShowsUserLocation:YES];
//Instantiate a location object.
locationManager = [[CLLocationManager alloc] init];
//Make this controller the delegate for the location manager.
[locationManager setDelegate:self];
//Set some parameters for the location object.
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
firstLaunch=YES;
[_mapView setCenterCoordinate:_mapView.userLocation.coordinate];
}
-(void)plotPositions:(NSArray *)data {
// 1 - Remove any existing custom annotations but not the user location blue dot.
for (id<MKAnnotation> annotation in _mapView.annotations)
{
if ([annotation isKindOfClass:[MapPoint class]])
{
[_mapView removeAnnotation:annotation];
}
}
// 2 - Loop through the array of places returned from the Google API.
for (int i=0; i<[data count]; i++)
{
//Retrieve the NSDictionary object in each index of the array.
NSDictionary* place = [data objectAtIndex:i];
// 3 - There is a specific NSDictionary object that gives us the location info.
NSDictionary *geo = [place objectForKey:@"geometry"];
// Get the lat and long for the location.
NSDictionary *loc = [geo objectForKey:@"location"];
// 4 - Get your name and address info for adding to a pin.
NSString *name = [place objectForKey:@"name"];
NSString *vicinity = [place objectForKey:@"vicinity"];
// 4.5 - Get id for detailed view.
NSString *myId = [place objectForKey:@"id"];
// Create a special variable to hold this coordinate info.
CLLocationCoordinate2D placeCoord;
// Set the lat and long.
placeCoord.latitude=[[loc objectForKey:@"lat"] doubleValue];
placeCoord.longitude=[[loc objectForKey:@"lng"] doubleValue];
// 5 - Create a new annotation.
MapPoint *placeObject = [[MapPoint alloc] initWithName:name address:vicinity coordinate:placeCoord];
[_mapView addAnnotation:placeObject];
NSLog(@"%@", myId);
}
}
-(void)button:(id)sender
{
//here is my main problem ...
}
私が間違いや何か他のことをした場合は、それを教えてください. どんな答えにも感謝します!
私は Obj-C にまったく慣れていないので、すべてのヒントが役に立ちます。
よろしくカーティス