我正在开发一个应用程序,我需要在其中显示用户当前位置和检索地址的Google Map。我已经实现了该部分并且它正在运行,现在我需要使用google place API找到该地方最近的餐馆。我已经整合了它,它向我展示了所有最近的餐厅。但是我需要在那家餐厅放下别针。我使用GMSMarker显示单个引脚,但是我需要在获取信息后显示多个引脚。以下是我的源代码。请帮助丢弃多个引脚。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CLLocationCoordinate2D coordinate;
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
currentLocation = [locationManager location];
coordinate = [currentLocation coordinate];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:coordinate.latitude longitude:coordinate.longitude zoom:15];
GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectMake(0, 0, 320, 300) camera:camera];
mapView.delegate = self;
// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude);
marker.title = @"Current Location";
marker.snippet = @"Test";
marker.map = mapView;
[self.view addSubview:mapView];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
currentLocation = [locations objectAtIndex:0];
[locationManager stopUpdatingLocation];
CLGeocoder *geocoder = [[CLGeocoder alloc] init] ;
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error)
{
if (!(error))
{
CLPlacemark *placemark = [placemarks objectAtIndex:0];
NSLog(@"\nCurrent Location Detected\n");
NSLog(@"placemark %@",placemark);
NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
NSString *Address = [[NSString alloc]initWithString:locatedAt];
NSString *Area = [[NSString alloc]initWithString:placemark.locality];
NSString *Country = [[NSString alloc]initWithString:placemark.country];
NSString *CountryArea = [NSString stringWithFormat:@"%@, %@", Area,Country];
NSLog(@"%@",CountryArea);
NSLog(@"%@",Address);
}
else
{
NSLog(@"Geocode failed with error %@", error);
NSLog(@"\nCurrent Location Not Detected\n");
//return;
}
}];
}
-(IBAction)nearbyPlaces:(id)sender {
NSString *str=[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=5000&types=%@&sensor=true&key=%@",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude,@"restaurant",@"AIzaSyB0jFNjBtUlr-zbhiCm-xduYR1YkDMV1Fo"];
NSLog(@"\n\n URL %@",str);
NSURL *strurl = [NSURL URLWithString:[str stringByReplacingOccurrencesOfString:@"|" withString:@"%7C"]];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:strurl];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if ([data length] > 0 && error == nil) {
self.view = mapView_;
id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if ([result isKindOfClass:[NSMutableArray class]])
{
NSLog(@"\n\n.......This is Mutable Array");
}
else
{
if ([[result objectForKey:@"results"] count] > 0) {
NSLog(@">>>>>>>> dict keys :%lu \nFull Address : %@\n LatLong : %@ \n total result : %lu", (unsigned long)[[result objectForKey:@"results"] count],[[result objectForKey:@"results"]valueForKey:@"vicinity"],[[[result objectForKey:@"results"]valueForKey:@"geometry"]valueForKey:@"location"],(unsigned long)[[result objectForKey:@"results"]count]);
for (int i=0; i<[[result objectForKey:@"results"] count]; i++) {
NSLog(@"%@",[[[result objectForKey:@"results"]objectAtIndex:i]valueForKey:@"name"]);
NSLog(@"%@",[[[result objectForKey:@"results"]objectAtIndex:i]valueForKey:@"vicinity"]);
NSLog(@"%@",[[[result objectForKey:@"results"]objectAtIndex:i]valueForKey:@"reference"]);
CLLocationCoordinate2D location;
location.latitude = [[[[[[result objectForKey:@"results"]objectAtIndex:i]valueForKey:@"geometry"]valueForKey:@"location"] valueForKey:@"lat"]doubleValue];
location.longitude = [[[[[[result objectForKey:@"results"]objectAtIndex:i]valueForKey:@"geometry"]valueForKey:@"location"] valueForKey:@"lng"] doubleValue];
}
}
}
}
}];
}
答案 0 :(得分:0)
for (int i=0; i<[youArray count]; i++)
{
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(latitude,longitude);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
marker.title = @"Title";
marker.snippet = @"Subtitle";
marker.flat=YES;
marker.map = _mapView_results;
}
以下是我过去常用的代码。