2017-08-02 6 views
1

forループを使用して複数のマーカーをGoogleマップに追加しようとしましたが、Googleマップに追加されません。 1つのマーカーを追加するか、forループなしで各マーカーのコードを記述している場合。 ForループSwift 3:Googleマップでforループのマーカーを設定しない

宣言

@IBOutlet weak var googleMapView: GMSMapView! // set in story board 
var shopsLatLong = [Double: Double]() // shops dictionary for lat long 

に動作していない:

for (key,value) in shopsLatLong{ 
    //Setting camera 
    self.googleMapView.camera = GMSCameraPosition.camera(withLatitude: key, longitude: value, zoom: 6.0) 
    let marker = GMSMarker() 
    marker.position = CLLocationCoordinate2DMake(key, value) 
    marker.title = "Store Name" 
    marker.snippet = "Pakistan" 
    marker.map = self.googleMapView 
    print("Marker is \(marker)") 
} 

この作業を取得する方法は?この問題を理解するために助けてください

+0

gmsmarkerにローカル変数を使用すると、 –

+0

のようにinit gmsmarkerがループしませんか? –

+0

はいローカルと変更緯度が長い –

答えて

0

のタプルの偶数アレイを有する構造体のアレイを示唆し、私はそれを把握することができました。実際に私はセグーの準備をオーバーライドしていませんでした。ボタンはストーリーボードにも直接リンクされていました。だからここに私がしたことがありますあなたのためにそれも動作します。

  1. オーバーライドセグエ準備し、そこ変数を設定
  2. は、ビューコントローラの上部から新しいセグエを追加ボタン
  3. と直接連結されセグエリンク(もしあれば)を削除します新しいセグエ。
  4. ビルドして実行します。

私はそれもあなたの問題を解決することを願っています。

+0

はい私の地図を保持している別のビューへのデータを表示することができます。私はマニュアルリンクを使用しようとしていましたが、ボタンからのセグのリンクも存在しました。マップビューへのポインタをロードできませんでした –

1

マーカーとしてclass variableを作ってはいけません。 にはlocal variableを作成する必要があります。

クラス変数を作成すると、マーカーが1つしか得られません。あなたがforループ

for (key,value) in shopsLatLong{ 
    //Get the latitude and longitude of the marker and pass it here. 
     let lat = //latitude for marker 
     let lon = //longitude for marker 
     var marker = GMSMarker() 
     marker.position = CLLocationCoordinate2DMake(lat, lon) 
     marker.title = "Store Name" 
     marker.snippet = "Pakistan" 
     marker.map = self.googleMapView 
} 
+0

このコードを試してください@Umar – adev

+0

ローカル変数も試しましたが、ループの外側にある1つのマーカーの場合と同じように動作しますが、forループでは失敗します。 –

+0

@UmarHussain forループ内で異なる緯度と経度を取得していますか? – Subramanian

0

変更するには、このコード内の値をオーバーライドしているので...

@IBOutlet weak var googleMapView: GMSMapView! // set in story board 
var shopsLatLong = [Double: Double]() // shops dictionary for lat long 


for (key,value) in shopsLatLong{ 
//Setting camera 
     self.googleMapView.camera = GMSCameraPosition.camera(withLatitude: key, longitude: value, zoom: 6.0) 
     let marker = GMSMarker() 
     marker.position = CLLocationCoordinate2DMake(key, value) 
     marker.title = "Store Name" 
     marker.icon = image 
     marker.snippet = "Pakistan" 
     marker.map = self.googleMapView 
     print("Marker is \(self.marker)") 
} 
+0

こんにちは私の答え @Sara Methuz – Jaydip

+0

@Jaydipをチェックしますが、あなたの答えは –

+0

複数のローカル変数も –

1

まず

func setData(){ 
     //create dictionary and store in mai array 
     var dicMapData : NSMutableDictionary = NSMutableDictionary() 
     dicMapData["title"] = "Kalasagar Shopping Hub" 
     dicMapData["image"] = "background9" 
     dicMapData["subtitle"] = "ShoppingHub" 
     dicMapData["latitude"] = 23.0669 // store latitude for specigic location 
     dicMapData["longitude"] = 72.5328 // store longitude for specigic location 
     arrMultipleAnotation.add(dicMapData) 

     dicMapData = NSMutableDictionary() 
     dicMapData["title"] = "Satadhar Cross Road" 
     dicMapData["image"] = "background8" 
     dicMapData["subtitle"] = "Crossroad" 
     dicMapData["latitude"] = 23.0629 
     dicMapData["longitude"] = 72.5328 
     arrMultipleAnotation.add(dicMapData) 

     dicMapData = NSMutableDictionary() 
     dicMapData["title"] = "Sola Bridge" 
     dicMapData["image"] = "menu-icon" 
     dicMapData["subtitle"] = "Bridge" 
     dicMapData["latitude"] = 23.0695 
     dicMapData["longitude"] = 72.5232 
     arrMultipleAnotation.add(dicMapData) 

     let initialLocation = CLLocation(latitude: 23.0669, longitude: 72.5289) 
     centerMapOnLocation(location: initialLocation) 

     //For Displayig Multiple Pin in mapview using array and loop 
     for locations in arrMultipleAnotation{ 
      //Setting camera 
       self.googleMapView.camera = GMSCameraPosition.camera(withLatitude: dicCurrent["latitude"] as! Double, longitude: dicCurrent["longitude"] as! Double, zoom: 6.0) 
       self.marker = GMSMarker() 
       self.marker.position = CLLocationCoordinate2D(latitude: dicCurrent["latitude"] as! Double, longitude: dicCurrent["longitude"] as! Double) 
       self.marker.title = dicCurrent["title"] as? String 
       self.marker.snippet = dicCurrent["subtitle"] as? String 
       self.marker.map = self.googleMapView 
       print("Marker is \(self.marker)") 

     } 

のような配列を作成し、私はそれはあなた

0
ための作業願っています

markersプロパティ(配列)を使用して、この配列にマーカーを追加する必要があります。そうすれば、すべてのマーカーオブジェクトに強い参照があり、簡単にそれらのマーカーオブジェクトを操作できます。

また、辞書は実際にはこの目的のための優れたデータ構造ではありません。私は、構造体は、私も同様の問題があった緯度及び経度又は(Double,Double)

@IBOutlet weak var googleMapView: GMSMapView! // set in story board 
var shopsLatLong = [(Double,Double)]() // shops dictionary for lat long 
var markers = [GMSMarker]() 


for (position) in shopsLatLong{ 
//Setting camera 
     self.googleMapView.camera = GMSCameraPosition.camera(withLatitude: position.0, longitude: position.1, zoom: 6.0) 
     let marker = GMSMarker() 
     marker.position = CLLocationCoordinate2DMake(position.0, position.1) 
     marker.title = "Store Name" 
     marker.snippet = "Pakistan" 
     marker.map = self.googleMapView 
     self.markers.append(marker) 
     print("Marker is \(marker)") 
} 
+0

あなたのポイントは良いです、私はマーカーの配列サイズもチェックしてみました、マーカー配列は正しいですがそれでもマーカーは表示されません –

+0

辞書は実際には正しい構造体ではありませんが、2つの2倍のタプルの配列に変更しました。[(Double、Double)]。 – Paulw11

+0

はい、私はズームインしましたが、マーカは表示されませんでした。 また、モーダルビューでGoogleマップを使用していますが、これが問題になりますか? –

関連する問題