Neutral Scent

App developments & Gadgets

WP8のMapコントロールで表示範囲を緯度経度で取得・更新する


Windows Phone 8Microsoft.Phone.Maps.Controls.Mapコントロールでユーザーの地図操作に伴って地図の表示範囲を緯度経度で取得し、前回のデータロード時の中心が表示範囲外へ移動したらデータをリロードするコードです。
ようするに、今表示してる地図の範囲内のピンを自動的にリロードさせる動作ですね。



private LocationRectangle prevRect = new LocationRectangle();

private void map1_CenterChanged_1(object sender, MapCenterChangedEventArgs e)
{
var nw = map1.ConvertViewportPointToGeoCoordinate(new Point(0, 0));
var se = map1.ConvertViewportPointToGeoCoordinate(new Point(map1.ActualWidth, map1.ActualHeight));
var currentRect = new LocationRectangle(nw, se);

if (!currentRect.Intersects(new LocationRectangle(prevRect.Center, prevRect.Center)))
{
UpdateMap(currentRect);
prevRect = currentRect;
}
}

Via: Map control - how do you get the latitude / longitude span?
http://social.msdn.microsoft.com/Forums/en-US/wpdevelop/thread/e2f20931-f79c-4a4d-9f24-8e97483feb3f

更新:初出時、以下のWidthとHeightが入れ替わっていたため訂正しました。

誤: map1.ConvertViewportPointToGeoCoordinate(new Point(map1.ActualHeight, map1.ActualWidth)
正: map1.ConvertViewportPointToGeoCoordinate(new Point(map1.ActualWidth, map1.ActualHeight)

更新2:prevRectの保存処理他が抜けていたので追加しました。