2012-02-11 4 views
1

Bing 맵에 압정을 추가하려고합니다. 푸시 핀은 JSON 피드에서 가져옵니다. 나는 이런 식으로하고 싶다. enter image description here 내 코드가 혼자 처음으로 작동하지 않아 그 이유를 이해할 수 없다. CS가Bing 맵에 PushPins 추가

public class MapViewModel : INotifyPropertyChanged 
{ 
    public static ObservableCollection<PushpinModel> pushpins = new ObservableCollection<PushpinModel>(); 
    public static ObservableCollection<PushpinModel> Pushpins 
    { 
      get { return pushpins; } 
     set { pushpins = value; } 
    } 
} 

지도 XAML입니다 내지도 뷰 모델입니다 :

마지막으로
//Map.xaml.cs 
public partial class Map : PhoneApplicationPage 
{ 
    #define DEBUG_AGENT 
    private IGeoPositionWatcher<GeoCoordinate> watcher; 
    private MapViewModel mapViewModel; 

    public Map() 
    { 
     InitializeComponent(); 
     mapViewModel = new MapViewModel(); 
     this.DataContext = mapViewModel; 
    } 

    private void page_Loaded(object sender, RoutedEventArgs e) 
    { 
     if (watcher == null) 
     { 

    #if DEBUG_AGENT 
      watcher = new Shoporific.My.FakeGPS(); 
    #else  
      watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default); 
    #endif 
     } 
     watcher.Start(); 

     mapViewModel.Center = watcher.Position.Location; 
     PushpinModel myLocation = new PushpinModel() { Location = mapViewModel.Center, Content = "My Location" }; 
     MapViewModel.Pushpins.Add(myLocation); 
     myLocation.RefreshNearbyDeals(); 
     watcher.Stop(); 
    } 
} 

의 PushPinModelClass :

public class PushPinModel 
{ 
    public void RefreshNearbyDeals() 
    { 
     System.Net.WebClient wc = new WebClient(); 
     wc.OpenReadCompleted += wc_OpenReadCompleted; 
     wc.OpenReadAsync(" a valid uri"); 
    } 

    void wc_OpenReadCompleted(object sender, System.Net.OpenReadCompletedEventArgs e) 
    { 
     var jsonStream = e.Result; 
     DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Deal[])); 
     Deal[] deals = (ser.ReadObject(jsonStream) as Deal[]); 

     if (deals.Any()) 
     { 
       var currentLocation = MapViewModel.Pushpins.Where(pin => pin.Content == "My Location"); 
       MapViewModel.Pushpins = new ObservableCollection<PushpinModel>(); 
       foreach (var deal in deals) 
        MapViewModel.Pushpins.Add(new PushpinModel() 
        { 
         Content = deal.Store, 
         Location = new GeoCoordinate(deal.Location.Latitude, deal.Location.Longtitude), 
         Offers = deal.Offers, 
        }); 

     } 
    } 
} 

나는 조금 혼란 스러워요 "내 위치"를 제외하고 압정이 처음에만 나타나지 마라. 두 번째 이후로 예상대로 나타납니다 (다시 탐색 한 다음지도 화면으로 다시 이동하면).

+0

빠른 팁, WP7.1 도구를 사용하면 에뮬레이터에 GPS 시뮬레이터가 있습니다. Shoporific.My.FakeGPS 클래스는 필요하지 않습니다. – ColinE

+0

그래, @ 콜린 : 나는 그것에 대해 알았다. 하지만 디버거가 부착되지 않은 경우에도 가짜 GPS를 원했습니다. #def DEBUG_AGENT에 주목하십시오. 지도가 jacobs & Javits 센터를 중심으로 배치 할 수 있도록이 기능이 필요했습니다. 나는 이것을 프로토 타입으로하고있다. – gprasant

답변

1

wc_OpenReadCompleted 안에 MapViewModel.Pushpins을 다시 인스턴스하고 있습니다.

번만 ObservableCollection으로 생성자를 호출하십시오 (MainViewModel 내에서). 다시 xaml 페이지에 있다고 가정하는 바인딩을 다시 호출합니다.

나는 당신이 PushpinViewModel에서 해당 줄을 제거하거나 (달성하려는 대상에 따라) MainViewModel.Pushpins.Clear()을 호출해야한다고 생각합니다.

관련 문제