2016-02-16 60 views
0

所以我想要做的是當用戶點擊一個Google地圖標記,一個活動會顯示,將顯示該標記內的所有信息。但問題是,所有標記都顯示相同的結果(這是我的parse.com數據庫第一行中的項目)。我目前正在使用Xamarin進行開發。我會很感激任何答案。先進的謝謝你們。Xamarin安卓 - 谷歌地圖標記始終顯示相同的信息

public async void getGeoPoint(){ 
     string getPlaceName, getPlacePrice, getPlaceAddress, getOwnerContact, getRentalType, getOwnerName; 

     string xx = autoCompleteTextView.Text; 
     ParseQuery<ParseObject> query = ParseObject.GetQuery ("Rentals") 
     .WhereEqualTo ("rentalCity", xx); 
     IEnumerable<ParseObject> results = await query.FindAsync(); 

     foreach(var temp in results){ 
      getLatitude = temp.Get<double> ("rentalLatitude"); 
      getLongitude = temp.Get<double> ("rentalLongitude"); 
      getPlaceName = temp.Get<string> ("rentalName"); 
      getPlacePrice = temp.Get<string> ("rentalPrice"); 
      getPlaceAddress = temp.Get<string> ("rentalFullAddress"); 
      getOwnerContact = temp.Get<string> ("ownerContactNo"); 
      getRentalType = temp.Get<string> ("rentalType"); 
      getOwnerName = temp.Get<string> ("ownerName"); 
      myMarker = map.AddMarker(new MarkerOptions() 
       .SetPosition(new LatLng(getLatitude, getLongitude)) 
       .SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.edimeow)) 
       ); 
     } 

     map.MarkerClick += (object sender, GoogleMap.MarkerClickEventArgs e) => { 
      string selected = getPlaceName; 
      var passToRentalProfile = new Intent (this, typeof(HostRentalProfileList)); 
      passToRentalProfile.PutExtra ("selected", selected); 
      StartActivity (passToRentalProfile); 
      this.Finish(); 
     }; 


     btnList.Click += (object sender, EventArgs e) => { 
      var passToUserList = new Intent (this, typeof(UserListMode)); 
      passToUserList.PutExtra("arrData", xx); 
      StartActivity(passToUserList); 
      this.Finish(); 
     }; 

    }//getGeoPoint 

回答

0

在你foreach循環的枚舉所有返回的結果,並同時要添加在不同的地緣位置的新的地圖標記,你不存儲任何的標記中的其他細節被添加。

隨後,當您枚舉下一個結果時,將您的本地變量重置爲新信息。

您的map.MarkerClick是針對任何地圖標記點擊而發生的通用處理程序。

您試圖引用前面設置的getPlaceName,並且總是等於結果中的最後一項。

您需要將這些結果存儲在某個集合中,並將一些標識添加到已創建的地圖標記中,以便您可以在單擊地圖標記時引用回原始結果以供使用。

0

我有類似的東西,當點擊一個標記時我需要顯示一條自定義消息,我將它存儲在一個字典中,並通過GoogleMap.IOnMarkerClickListener將它傳遞給已被點擊的標記。

公共類MarkerManager:java.lang.Object中,GoogleMap.IOnMarkerClickListener {

private readonly GoogleMap _map; 

private Dictionary<Marker, int> _markerDictionary; 

public MarkerManager(GoogleMap map, BaseActivity activity) 
    { 
     _activity = activity; 
     _map = map; 
     _markerDictionary = new Dictionary<Marker, int>(); 
    } 


    public void AddMarkerCallback(LatLng position, string title, string bodyText, int? icon, Action<int, Marker> callback, 
     bool draggable = false, int id = 0) 
    { 
     var markerOptions = new MarkerOptions(); 
     markerOptions.SetPosition(position); 
     markerOptions.SetTitle(title); 
     markerOptions.SetSnippet(bodyText); 
     markerOptions.Draggable(draggable); 

     CallBack = callback; 

     if (icon.HasValue) 
     { 
      markerOptions.InvokeIcon(BitmapDescriptorFactory.FromResource(icon.Value)); 
     } 

     var marker = _map.AddMarker(markerOptions); 
     _markerDictionary.Add(marker, id); 

     _map.SetOnMarkerClickListener(this); 
    } 

    public void ClearMap() 
    { 
     if(_map != null) 
     _map.Clear(); 

     _markerDictionary = new Dictionary<Marker, int>(); 
    } 


    public bool OnMarkerClick(Marker p0) 
    { 
     p0.ShowInfoWindow(); 

     Console.WriteLine("maker click"); 

     HideMarkerAfterTime(p0); 

     foreach (var i in _markerDictionary) 
     { 
      if (!i.Key.Equals(p0)) continue; 
      if (CallBack == null) return true; 

      SeletedMarkerId = i.Value; 
      CallBack(i.Value, p0); 
      return true; 
     } 

     if (CallBackLocation != null) 
      CallBackLocation(p0.Position); 

     return false; 
    } 


    /// <summary> 
    /// The timer 
    /// </summary> 
    private System.Timers.Timer _timer; 

    /// <summary> 
    /// Hides the marker after time. 
    /// </summary> 
    /// <param name="p0">The p0.</param> 
    /// <param name="miliSeconds">The mili seconds.</param> 
    private void HideMarkerAfterTime(Marker p0, double miliSeconds = 3000) 
    { 
     //need to clean down timer object. if i click on another 
     //icon wierd things happen 
     if (_timer != null) _timer.Dispose(); 

     _timer = new System.Timers.Timer { Interval = miliSeconds }; 
     _timer.Start(); 
     _timer.Elapsed += (sender, args) => _activity.RunOnUiThread(() => 
     { 
      p0.HideInfoWindow(); 
      _timer.Stop(); 
     }); 
    } 
} 
相關問題