2014-09-02 57 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