×
Namespaces

Variants
Actions

在WP7的Bing地图上加入Google地标

From Nokia Developer Wiki
Jump to: navigation, search
SignpostIcon HereMaps 99.png
SignpostIcon XAML 40.png
SignpostIcon WP7 70px.png
Article Metadata

代码示例
兼容于
文章
翻译:
最后由 hamishwillee 在 22 Jul 2013 编辑

这篇文章展示了如何在Bing地图上绘制附近的建筑。

Contents

简介

这篇文章解释了如何利用Google Places API来获取建筑物的信息,然后我们将它们绘制在Bing地图上。当点击屏幕上图标时,弹出建筑物的信息并且显示它与用户之间的距离。 首先我们需要用Windows Live ID从Bing Maps Account Center申请一个Bing地图账户。当账户被创建时,从账户中获得KEY并应运到我们的应用程序中。


实现

让我们利用Windows Phone Application模板来创建一个WP7项目。我们利用WP7 ListBox控件在MainPage.xaml创建一系列图标。当用户点击任何一个列表项时,Menu_SelectionChanged事件处理程序将被MainPage.xaml.cs类调用。 当加载地图后,首先我们用GeoCoordinateWatcher:: StatusChanged事件来检查程序是否能提供位置更新,并利用GeoCoordinateWatcher:: PositionChanged事件来检测用户位置的改变。

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;
}
}//end of funt
void myPositionChanged(GeoPositionChangedEventArgs<GeoCoordinate> e)
{
latitude = e.Position.Location.Latitude;
longitude = e.Position.Location.Longitude;
 
LoadLandMarkList();
}//end of funt

LoadLandMarkList()方法下载并解析从Google Places API获得的XML数据,在那里我们获得每个图标的name, address, latitudelongitude。要访问Google Places API,请从这里 获得key并用下面的代码替换原来的代码。

void LoadLandMarkList()
{
 
 
string url = "https://maps.googleapis.com/maps/api/place/search/xml?location=" + latitude + "," + longitude + "&radius=1000&name=" + LandMarkName + "&sensor=false&key=<Your Google API Key>";
 
WebClient xmlClient = new WebClient();
xmlClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(LoadLandMarkList_Completed);
xmlClient.DownloadStringAsync(new Uri(url, UriKind.RelativeOrAbsolute));
}//end of funt
 
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++;
 
}
 
 
 
 
 
if (LandMarkCount > 0)
{
 
PlotUserAndLandMarks();
}
else
{
MessageBox.Show("There is no information available for your location");
progressBar.IsIndeterminate = false;
progressBar.Visibility = Visibility.Collapsed;
}
}
}//end of funt

我们获得了地标数据,然后我们计算地标总数并将每个地标都绘制在Bing地图上。

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);
}

当用户点击任何一个图标时,我们调用EventHandler来显示建筑物地址信息并显示它与用户的距离。

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;
 
}//end of funt

calcutaleDistance()方法被调用来计算用户与建筑物之间的距离。

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");
}//end of funt

我们还要添加一个应用条来改变Bing地图的视角模式。

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);
}
}//end of funt

概要

这只是有关Bing地图的基本操作,还有很多相关资源可在MSDN上查找。我在这里发现一个。但是这篇文章显示怎样简单并灵活的使用Bing Map控件。

源代码

完整的源代码实例可在如下网站获得:File:GoogleLandmarksWP7.zip

This page was last modified on 22 July 2013, at 08:25.
94 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×