1
我试图将图钉添加到必应地图。推针从JSON进给中获得。我想得到这样的东西: 我的代码不能单独工作,我不明白为什么。 我的地图视图模型被在Bing地图中添加图钉
public class MapViewModel : INotifyPropertyChanged
{
public static ObservableCollection<PushpinModel> pushpins = new ObservableCollection<PushpinModel>();
public static ObservableCollection<PushpinModel> Pushpins
{
get { return pushpins; }
set { pushpins = value; }
}
}
地图XAML CS是:
//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,
});
}
}
}
我有点糊涂了,除了 “我的位置” 的图钉不要只在第一次出现。它们如预期的那样第二次出现(如果我导航回来,然后再次移动到地图屏幕)。
一个快速提示给你,用WP7.1工具模拟器有一个GPS模拟器。你不需要你的Shoporific.My.FakeGPS类。 – ColinE 2012-02-11 07:29:37
对@柯林斯:我知道这件事。但即使在未连接调试器的情况下,我也需要伪造的GPS。注意#def DEBUG_AGENT?我需要这个来确保地图将围绕着jacobs和Javits中心。我正在做这个原型。 – gprasant 2012-02-13 04:32:37