2014-09-02 60 views
0

我正在开发一个应用程序,在该应用程序中需要显示带有用户当前位置和检索地址的Google地图。我已经实现了该部分并且它正在工作,现在我需要找到最近的餐厅使用谷歌地方API的地方。我整合了它们,它向我展示了所有最近的餐厅。不过,我需要在该餐厅放置针脚。我使用GMSMarker来显示单个引脚,但是我需要在获取信息后显示多个引脚。以下是我的源代码。请帮助删除多个引脚。谷歌地图在iOS中放置多个引脚

- (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
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; 
     } 

这里是我用来做同样的代码。

+0

好吧,我添加了这段代码后,就显示出一个黑屏。请让我知道这件事。 – Kashif 2014-09-02 05:42:29

+0

取出一个单独的数组,包含lat,long和其他相关信息,然后遍历该数组,并在您想要显示的适当位置调用它。 – 2014-09-02 05:52:52

+0

谢谢你已经解决了。我已经接受了答案。 – Kashif 2014-09-02 05:56:30