ak
url http://developer.baidu.com/map/index.php?title=car
后台cs
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using System.Runtime.Serialization.Json; using System.Text; using Windows.Data.Json; using Windows.Devices.Geolocation; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; using Windows.Web.Http; // “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234238 上提供 namespace App3 { /// <summary> /// 可独立使用或用于导航至 Frame 内部的空白页。 /// </summary> public sealed partial class MainPage : Page { string ak = "XXXXXXXXXXXXXXXXXXXXXXXXXX"; public MainPage() { this.InitializeComponent(); } /// <summary> /// 导航到该页面的时候被执行 /// </summary> /// <param name="e"></param> protected async override void OnNavigatedTo(NavigationEventArgs e) { Load.IsActive = true; var cityName = await GetCityName(); var data = await GetWeatherByCityName(cityName); var viewModel = new MainPageViewModel(); viewModel.CityName = cityName; viewModel.IndexInfos = data.results[0].index; viewModel.WeatherInfos = data.results[0].weather_data; this.DataContext = viewModel; Load.IsActive = false; } private async System.Threading.Tasks.Task<weatherDate> GetWeatherByCityName(string cityName) { var apiAddress = string.Format("http://api.map.baidu.com/telematics/v3/weather?location={0}&output=json&ak={1}", cityName, ak); HttpClient client = new HttpClient(); var json = await client.GetStringAsync(new Uri(apiAddress)); DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(weatherDate)); using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json))) { return serializer.ReadObject(ms) as weatherDate; } } private static async System.Threading.Tasks.Task<string> GetCityName() { Geolocator o = new Geolocator(); var geo = await o.GetGeopositionAsync(); var position = geo.Coordinate.Point.Position; var apiAddress = string.Format("http://api.map.baidu.com/geocoder/v2/?ak=XXXXXXXXXXXXXXXXXXXXXX&output=json&location={0},{1}", position.Latitude, position.Longitude); HttpClient client = new HttpClient(); var json = await client.GetStringAsync(new Uri(apiAddress)); var jsonObject = JsonObject.Parse(json); var cityName = jsonObject["result"].GetObject()["addressComponent"].GetObject()["city"].GetString(); return cityName; } } public class MainPageViewModel { public string CityName { get; set; } public IList<indexList> IndexInfos { get; set; } public IList<weather_dataList> WeatherInfos { get; set; } } public class weatherDate { public int error { get; set; } public string status { get; set; } public string date { get; set; } public List<resultsList> results { get; set; } } public class resultsList { public string currentCity { get; set; } public string pm25 { get; set; } public List<indexList> index { get; set; } public List<weather_dataList> weather_data { get; set; } } public class indexList { public string title { get; set; } public string zs { get; set; } public string tipt { get; set; } public string des { get; set; } } public class weather_dataList { public string date { get; set; } public string dayPictureUrl { get; set; } public string nightPictureUrl { get; set; } public string weather { get; set; } public string wind { get; set; } public string temperature { get; set; } } }
前台xaml
<Page x:Class="App3.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:App3" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid> <Hub> <HubSection x:Uid="index1"> <HubSection.Background> <ImageBrush ImageSource="/Assets/aa.jpg" Stretch="UniformToFill"/> </HubSection.Background> <DataTemplate> <Grid Width="350" Height="500" > <TextBlock Text="{Binding CityName}" Foreground="Red" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="22" /> </Grid> </DataTemplate> </HubSection> <HubSection x:Uid="index2"> <DataTemplate> <ListView ItemsSource="{Binding WeatherInfos}"> <ListView.ItemTemplate> <DataTemplate> <Border Width="360" BorderThickness="2" BorderBrush="#FF2996AE"> <StackPanel> <TextBlock Text="{Binding date}" FontSize="25"/> <TextBlock Text="{Binding weather}" FontSize="30"/> <StackPanel Orientation="Horizontal"> <Image Source="{Binding dayPictureUrl}"></Image> </StackPanel> <TextBlock Text="{Binding wind}" FontSize="25"/> <TextBlock Text="{Binding temperature}" FontSize="30"/> </StackPanel> </Border> </DataTemplate> </ListView.ItemTemplate> </ListView> </DataTemplate> </HubSection> <HubSection x:Uid="index3"> <DataTemplate> <ListView ItemsSource="{Binding IndexInfos}"> <ListView.ItemTemplate> <DataTemplate> <Border> <StackPanel> <TextBlock Text="{Binding tipt}" FontSize="30"/> <TextBlock Text="{Binding zs}" FontSize="30"/> <TextBlock Text="{Binding des}" FontSize="30" TextWrapping="Wrap"/> </StackPanel> </Border> </DataTemplate> </ListView.ItemTemplate> </ListView> </DataTemplate> </HubSection> </Hub> <ProgressRing Height="100" Width="100" x:Name="Load"/> </Grid> </Page>