2017-02-27 22 views
0

我是iOS新手,面臨着在Google地圖上創建多個引腳的問題。 我的代碼是這樣的如何在Google地圖上動態創建多個引腳,目標爲C

在View DidLoad()

//Google Maps... 

    _placesClient = [GMSPlacesClient sharedClient]; 


    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:18.516726 
                  longitude:73.856255 
                   zoom:3]; 
    mapView = [GMSMapView mapWithFrame:CGRectMake(10, 140, 350, 350) camera:camera]; 

    GMSMutablePath *path = [GMSMutablePath path]; 
    [path addLatitude:18.516726 longitude:73.856255]; // Sydney 
    [path addLatitude:19.0728300 longitude:72.8826100]; // Fiji 

    mapView.delegate=self; 
    mapView.myLocationEnabled=YES; 
    locationtbl.hidden=YES; 

    if (nil == locationManager) 
     locationManager = [[CLLocationManager alloc] init]; 

    locationManager.delegate = self; 
    //Configure Accuracy depending on your needs, default is kCLLocationAccuracyBest 
    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer; 

    // Set a movement threshold for new events. 
    locationManager.distanceFilter = 500; // meters 

    [locationManager startUpdatingLocation]; 

    mapView.settings.myLocationButton = YES; 
    mapView.settings.scrollGestures = YES; 
    mapView.settings.zoomGestures = YES; 
    mapView.settings.tiltGestures=YES; 
    mapView.settings.rotateGestures=NO; 
    mapView.settings.compassButton = YES; 

    [self.view addSubview:mapView]; 

谷歌委託的方法是這樣的

#pragma mark - Google Maps 

// Add a UIButton in Interface Builder to call this function 
    - (IBAction)pickPlace:(UIButton *)sender { 

     CLLocationCoordinate2D center = CLLocationCoordinate2DMake(18.516726, 73.856255); 
     CLLocationCoordinate2D northEast = CLLocationCoordinate2DMake(center.latitude + 0.001, 
                     center.longitude + 0.001); 
     CLLocationCoordinate2D southWest = CLLocationCoordinate2DMake(center.latitude - 0.001, 
                     center.longitude - 0.001); 
     GMSCoordinateBounds *viewport = [[GMSCoordinateBounds alloc] initWithCoordinate:northEast 
                      coordinate:southWest]; 
     GMSPlacePickerConfig *config = [[GMSPlacePickerConfig alloc] initWithViewport:viewport]; 
     _placePicker = [[GMSPlacePicker alloc] initWithConfig:config]; 

     [_placePicker pickPlaceWithCallback:^(GMSPlace *place, NSError *error) { 
      if (error != nil) { 
       NSLog(@"Pick Place error %@", [error localizedDescription]); 
       return; 
      } 

      if (place != nil) { 
       self.nameLabel.text = place.name; 
       self.addressLabel.text = [[place.formattedAddress 
              componentsSeparatedByString:@", "] componentsJoinedByString:@"\n"]; 
      } else { 
       self.nameLabel.text = @"No place selected"; 
       self.addressLabel.text = @""; 
      } 


}]; 
} 

我現在面臨的問題是如何顯示在谷歌地圖多個引腳。我從數組中的動態Web服務中獲得價值。我需要在谷歌地圖上顯示針腳enter image description here,像這樣圖片

H是在谷歌地圖上創建的圖像,這是Image.How做到這一點??

我用這樣的代碼

for (int i=0; i<NameHSArray.count; i++) { 
       CLLocationCoordinate2D position = CLLocationCoordinate2DMake(18.516726, 73.856255); 
       GMSMarker *marker = [GMSMarker markerWithPosition:position]; 
       marker.title = @"Hello World"; 
       marker.icon = [UIImage imageNamed:@"mapicon1.png"]; 
       marker.map = mapView; 

      } 

但是,當我加入陣列它給我前進誤差

enter image description here

謝謝!

我用這個代碼,並對其做

for (int i=0; i<NameHSArray.count; i++) { 
       double LatitudeDouble = [LatitudeHSArray[i] doubleValue]; 
       double LongitudeDouble = [LongitudeHSArray[i] doubleValue]; 
       CLLocationCoordinate2D position = CLLocationCoordinate2DMake(LatitudeDouble, LongitudeDouble); 
       GMSMarker *marker = [GMSMarker markerWithPosition:position]; 
       marker.title = NameHSArray[i]; 
       marker.icon = [UIImage imageNamed:@"mapicon1.png"]; 
       GMSCameraUpdate *zoomCamera = [GMSCameraUpdate zoomIn]; 
       [mapView animateWithCameraUpdate:zoomCamera]; 
       marker.map = mapView; 

      } 
+0

字符串值在陣列中試試這[[LattitudeHSArray objectAtIndex:0] doubleValue] double value in array然後使用[LattitudeHSArray objectAtIndex:0] 試試這個 –

回答

1

試試這個對GMSMapView中

GMSMarker *marker = [[GMSMarker alloc] init]; 
marker.position = CLLocationCoordinate2DMake([lat doubleValue], [lng doubleValue]); 
marker.title = "" // you can set marker title here 
marker.icon = [UIImage imageNamed:@"gasoline-pump.png"]; // you can set your image here 
marker.snippet = "" // you can set snippet for more details 
marker.tracksViewChanges = YES; 
marker.tracksInfoWindowChanges = YES; 
marker.infoWindowAnchor = CGPointMake(5, 2); 
marker.map = mapView; 

添加地圖針你可以從MapView的刪除標記如下

[mapView clear]; // remove all 

marker.mapView = nil // remove specific 

- - 編輯----

設置多個標誌如下

for (obj in <#your array#>) { 
    GMSMarker *marker = [[GMSMarker alloc] init]; 
    marker.position = CLLocationCoordinate2DMake([lat doubleValue], [lng doubleValue]); 
    marker.title = "" // you can set marker title here 
    marker.icon = [UIImage imageNamed:@"gasoline-pump.png"]; // you can set your image here 
    marker.snippet = "" // you can set snippet for more details 
    marker.tracksViewChanges = YES; 
    marker.tracksInfoWindowChanges = YES; 
    marker.infoWindowAnchor = CGPointMake(5, 2); 
    marker.map = mapView; 
} 
+0

我的經緯度是數組格式。 – Muju

+0

你可以使用for循環多標記 –

+0

如何做到這一點? – Muju

0

您可以通過創建GMSMarker對象創建多個引腳或圖標。 ñ通過與它的緯度多頭頭寸和分配圖標圖像,如果你有一個...或零,如果你想默認的被分配

這標記對象添加到通過GMSMapView對象

GMSMarker *marker = [[GMSMarker alloc] init]; 
marker.position = CLLocationCoordinate2DMake([lat doubleValue], [lng doubleValue]); 

marker.icon = nil; 
marker.map = mapView; // object of GMSMapView 
相關問題