我是 Windows 开发的新手,我正在开发一个应用程序,我必须在地图中显示商店位置。所以我使用 Bing 地图来显示商店。我正在为 Windows Phone 8 开发应用程序。
但最后我收到一个关于 Microsoft.Phone.Controls Version=8.0.0 的错误
所以任何人都可以请解决我的问题。我的代码部分是这样的:
private GeoCoordinateWatcher watcher;
private GeoPositionAccuracy accuracy = GeoPositionAccuracy.High;
private MapLayer pushpinLayer = null;
Array LandMarkPositionArray = Array.CreateInstance(typeof(RssLandMark), 100);
double latitude;
double longitude;
int LandMarkCount = 0;
int zoomLevel = 15;
string LandMarkName = "";
string LandMarkTitle = "";
public MapScreen()
{
InitializeComponent();
startLocationService();
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string msg = "";
if (NavigationContext.QueryString.TryGetValue("msg", out msg))
LandMarkName = msg;
}
private void startLocationService()
{
if (pushpinLayer == null)
{
pushpinLayer = new MapLayer();
pushpinLayer.Name = "PushPinLayer";
map1.Children.Add(pushpinLayer);
}
watcher = new GeoCoordinateWatcher(accuracy);
watcher.MovementThreshold = 50;
watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
watcher.Start();
}
void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
Deployment.Current.Dispatcher.BeginInvoke(() => myStatusChanged(e));
}
void myStatusChanged(GeoPositionStatusChangedEventArgs e)
{
switch (e.Status)
{
case GeoPositionStatus.Disabled:
progressBar.IsIndeterminate = false;
progressBar.Visibility = Visibility.Collapsed;
MessageBox.Show("location is unsupported on this device");
break;
case GeoPositionStatus.Initializing:
break;
case GeoPositionStatus.NoData:
progressBar.IsIndeterminate = false;
progressBar.Visibility = Visibility.Collapsed;
MessageBox.Show("data unavailable");
break;
case GeoPositionStatus.Ready:
break;
}
}
void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
Deployment.Current.Dispatcher.BeginInvoke(() => myPositionChanged(e));
}
void myPositionChanged(GeoPositionChangedEventArgs<GeoCoordinate> e)
{
latitude = e.Position.Location.Latitude;
MessageBox.Show(latitude.ToString());
longitude = e.Position.Location.Longitude;
LoadLandMarkList();
}
void LoadLandMarkList()
{
MessageBox.Show(LandMarkName + "" + LandMarkCount);
//string url = "https://maps.googleapis.com/maps/api/place/search/xml?location=" + latitude + "," + longitude + "&radius=1000&name=" + LandMarkName + "&sensor=false&key=AIzaSyCHKtTaHDzKtvDydDAVw1fv5PbnL69BGY0";
string url = "https://maps.googleapis.com/maps/api/place/search/xml?location=" + latitude + "," + longitude + "&radius=50000&query=burial|flowers&types=funeral_home|florist&sensor=false&key=<google Api key>";
WebClient xmlClient = new WebClient();
xmlClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(LoadLandMarkList_Completed);
xmlClient.DownloadStringAsync(new Uri(url, UriKind.RelativeOrAbsolute));
}
void LoadLandMarkList_Completed(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
LandMarkCount = 0;
XElement xmlDataStill = XElement.Parse(e.Result);
foreach (var item in xmlDataStill.Descendants("result"))
{
string name = (string)item.Element("name").Value;
string address = (string)item.Element("vicinity").Value;
string latitude = (string)item.Element("geometry").Element("location").Element("lat").Value;
string longitude = (string)item.Element("geometry").Element("location").Element("lng").Value;
LandMarkPositionArray.SetValue(new RssLandMark() { name = name, address = address, longitude = longitude, latitude = latitude }, LandMarkCount);
LandMarkCount++;
}
MessageBox.Show(LandMarkName + "" + LandMarkCount);
if (LandMarkCount > 0)
{
PlotUserAndLandMarks();
}
else
{
MessageBox.Show("There is no information available for your location");
progressBar.IsIndeterminate = false;
progressBar.Visibility = Visibility.Collapsed;
}
}
}
void PlotUserAndLandMarks()
{
//plot user
Pushpin user = new Pushpin();
user.Location = new GeoCoordinate(latitude, longitude);
Image pinImage1 = new Image();
pinImage1.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri(@"/Images/user.png", UriKind.Relative));
pinImage1.Margin = new Thickness(-69, -52, 0, 0);
pinImage1.Opacity = 0.8;
pinImage1.Stretch = System.Windows.Media.Stretch.None;
pushpinLayer.AddChild(pinImage1, user.Location);
map1.SetView(user.Location, zoomLevel);
//-----------------------------------------------
//plot landmarks
for (int i = 0; i < LandMarkCount; i++)
{
RssLandMark st = LandMarkPositionArray.GetValue(i) as RssLandMark;
Pushpin landMark = new Pushpin();
landMark.Location = new GeoCoordinate(Convert.ToDouble(st.latitude), Convert.ToDouble(st.longitude));
if (st.name.Length > 8)
{
LandMarkTitle = st.name.Substring(0, 8) + "..";
}
landMark.Foreground = new SolidColorBrush(Colors.Black);
landMark.Background = new SolidColorBrush(Colors.Orange);
landMark.Content = LandMarkTitle;
landMark.FontSize = 21;
landMark.Opacity = .9;
landMark.Tag = i + "";
landMark.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(showandMarkDetails_Completed);
map1.Children.Add(landMark);
}
progressBar.IsIndeterminate = false;
progressBar.Visibility = Visibility.Collapsed;
}
void showandMarkDetails_Completed(object sender, System.Windows.Input.GestureEventArgs e)
{
int id = Convert.ToInt32((sender as Pushpin).Tag);
RssLandMark st = LandMarkPositionArray.GetValue(id) as RssLandMark;
title_txt.Text = st.name;
address_txt.Text = st.address;
dist_txt.Text = "You are around " + calcutaleDistance(latitude, longitude, Convert.ToDouble(st.latitude), Convert.ToDouble(st.longitude)) + " away";
popup.Visibility = Visibility.Visible;
bg.Visibility = Visibility.Visible;
}
public static string calcutaleDistance(double ulat, double ulon, double lat, double lon)
{
var sCoord = new GeoCoordinate(ulat, ulon);
var eCoord = new GeoCoordinate(lat, lon);
var R = 6371;
double lat1 = (sCoord.Latitude) / 180 * Math.PI;
double lat2 = (eCoord.Latitude) / 180 * Math.PI;
double lng1 = (sCoord.Longitude) / 180 * Math.PI;
double lng2 = (eCoord.Longitude) / 180 * Math.PI;
double dlng = lng2 - lng1;
double dlat = lat2 - lat1;
var a = Math.Pow(Math.Sin(dlat / 2), 2) + Math.Cos(lat1) *
Math.Cos(lat2) * Math.Pow(Math.Sin(dlng / 2), 2);
var c = 2 * Math.Asin(Math.Min(1, Math.Sqrt(a)));
var d = R * c;
return (String.Format("{0:0.00}", d) + " km");
}
void Close_Click(object sender, System.Windows.Input.GestureEventArgs e)
{
popup.Visibility = Visibility.Collapsed;
bg.Visibility = Visibility.Collapsed;
}
private void ApplicationBarIconButton_Click(object sender, EventArgs e)
{
// TODO : Change map view mode.
if (map1.Mode is AerialMode)
{
map1.Mode = new RoadMode();
}
else
{
map1.Mode = new AerialMode(true);
}
}
我使用了 Windows Phone 7.1 的代码,它工作正常。但是当我为 Windows Phone 8 编写相同的代码时遇到了问题。我还添加了 Microsoft.Phone.Controls.Maps.dll 文件。它给出了一个错误,我将其作为图像与这篇文章一起粘贴。
除此之外,我正在使用另一个名为 RssData.cs 的类。我使用这个类来存储数据,然后在需要时显示该类中的所有数据。
public class RssData
{
}
public class RssLandMark
{
public string name { get; set; }
public string address { get; set; }
public string latitude { get; set; }
public string longitude { get; set; }
}
所以请如果有人可以解决它,请帮助我。提前谢谢。