using MapSearch.Model;
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Telerik.Windows.Controls.Map;
using Telerik.Windows.Examples.Map.Search;
using System.Xml.Linq;
using ItemsControl = Telerik.Windows.Controls.ItemsControl;
namespace MapSearch
{
public partial class MainPage : UserControl
{
private string VEKey;
private MapItemsCollection itemCollection = new MapItemsCollection();
private BingSearchProvider searchProvider;
private string stringApiKey = "b14f16672dfsdfdff884887e1e5ab3e38f051c986b05sdfsdf0eca9247c8631c5c9c88e0ae48f";
public MapLayer x_map;
public IpInfo ipInfo = new IpInfo();
public MainPage()
{
InitializeComponent();
bsIndicator.IsBusy = true;
this.GetVEServiceKey();
this.informationLayer.DataMappings.Add(new DataMapping("Location", DataMember.Location));
Binding binding = new Binding();
binding.Source = this.itemCollection;
this.informationLayer.SetBinding(ItemsControl.ItemsSourceProperty, binding);
bsIndicator.IsBusy = false;
}
// Initialize Virtual Earth map provider.
private void SetProvider()
{
BingMapProvider provider = new BingMapProvider(MapMode.Aerial, true, this.VEKey);
this.RadMap1.Provider = provider;
// Init searh provider.
searchProvider = new BingSearchProvider()
{
ApplicationId = this.VEKey,
MapControl = this.RadMap1
};
searchProvider.SearchCompleted += new EventHandler<SearchCompletedEventArgs>(Provider_SearchCompleted);
}
private void GetVEServiceKey()
{
WebClient wc = new WebClient();
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
Uri keyURI = new Uri(HtmlPage.Document.DocumentUri, "AlTsdTXGRrAlAD_AsdfsdfNxsWHkwshIVLoBLCryJjB5adacusdfsdfGgcQSzd9UQZaow8hsdfsdVLPIUNMV");
wc.DownloadStringAsync(keyURI);
}
void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
this.VEKey = "AlTTXGRrAlADsdfsfxWHkwshsdffsdIVLoBLCryJadacuGgcQSzs9UQZaors38hVLPIUNMV";//e.Result;
this.SetProvider();
}
private void SearchHandler(object sender, RoutedEventArgs e)
{
}
private void Provider_SearchCompleted(object sender, SearchCompletedEventArgs args)
{
SearchResponse response = args.Response;
if (response.ResultSets[0].Results.Count > 0)
{
this.itemCollection.Clear();
foreach (SearchResultBase result in response.ResultSets[0].Results)
{
MyMapItem item = new MyMapItem()
{
Title = result.Name,
Location = result.LocationData.Locations[0]
};
this.itemCollection.Add(item);
}
}
if (response.ResultSets[0].SearchRegion != null)
{
// Set map viewport to the best view returned in the search result.
this.RadMap1.SetView(response.ResultSets[0].SearchRegion.GeocodeLocation.BestView);
// Show map shape around bounding area
if (response.ResultSets[0].SearchRegion.BoundingArea != null)
{
MapShape boundingArea = response.ResultSets[0].SearchRegion.BoundingArea;
boundingArea.Stroke = new SolidColorBrush(Colors.Red);
boundingArea.StrokeThickness = 1;
this.informationLayer2.Items.Add(boundingArea);
}
if (response.ResultSets[0].SearchRegion.GeocodeLocation.Address != null
&& response.ResultSets[0].SearchRegion.GeocodeLocation.Locations.Count > 0)
{
foreach (Location location in response.ResultSets[0].SearchRegion.GeocodeLocation.Locations)
{
MyMapItem item = new MyMapItem()
{
Title = response.ResultSets[0].SearchRegion.GeocodeLocation.Address.FormattedAddress,
Location = location
};
this.itemCollection.Add(item);
}
}
}
}
private void btnSearch_Click(object sender, RoutedEventArgs e)
{
string query = this.SearchCondition.Text;
if (!string.IsNullOrEmpty(query))
{
SearchRequest request = new SearchRequest()
{
Culture = new System.Globalization.CultureInfo("en-ZA"),
Query = query
};
this.searchProvider.SearchAsync(request);
}
}
private void ShowMap(String SearchCondition)
{
string query = SearchCondition;
if (!string.IsNullOrEmpty(query))
{
SearchRequest request = new SearchRequest()
{
Culture = new System.Globalization.CultureInfo("en-ZA"),
Query = query
};
this.searchProvider.SearchAsync(request);
}
}
private void btnGetIPLocation_Click(object sender, RoutedEventArgs e)
{
bsIndicator.IsBusy = true;
if (txtIpaddress.Text != "")
{
GetIpInfo();
}
else
{
txtIpaddress.Background = new SolidColorBrush(Colors.Red);
}
bsIndicator.IsBusy = false;
}
private void GetIpInfo()
{
WebClient wbC = new WebClient();
wbC.OpenReadAsync(new Uri("http://api.ipinfodb.com/v2/ip_query.php?key=" + stringApiKey + "&ip=" + txtIpaddress.Text));
wbC.OpenReadCompleted += new OpenReadCompletedEventHandler(wbC_OpenReadCompleted);
}
void wbC_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error != null)
{
//show message with error info.
}
using (Stream s = e.Result)
{
XDocument doc = XDocument.Load(s);
var local = from text in doc.Descendants("Response")
select new
{
City = text.Element("City").Value.ToString(),
Country = text.Element("CountryName").Value.ToString(),
CountryCode = text.Element("CountryCode").Value.ToString(),
Region = text.Element("RegionName").Value.ToString(),
Latitude = text.Element("Latitude").Value.ToString(),
Longitude = text.Element("Longitude").Value.ToString(),
IpAdress = text.Element("Ip").Value.ToString()
};
NumberFormatInfo provider = new NumberFormatInfo();
provider.NumberGroupSeparator = ".";
foreach (var item in local)
{
ipInfo.City = item.City != string.Empty ? item.City : "unknown";
ipInfo.Country = item.Country;
ipInfo.CountryCode = item.CountryCode;
ipInfo.Region = item.Region != string.Empty ? item.Region : "unknown";
ipInfo.Latitude = Convert.ToDouble(item.Latitude.ToString(), provider);
ipInfo.Longitude = Convert.ToDouble(item.Longitude.ToString(), provider);
ipInfo.IpAdress = item.IpAdress;
}
bsIndicator.IsBusy = true;
lblcity.Content = ipInfo.City != string.Empty ? ipInfo.City : "none";
lblCountry.Content = ipInfo.Country != string.Empty ? ipInfo.Country : "none";
lblregion.Content = ipInfo.Region != string.Empty ? ipInfo.Region : "none";
image1.Source = new BitmapImage(new Uri("http://" + App.Current.Host.Source.Host + "/Flags/" + ipInfo.Country +" -Flag-48" + ".png"));
image1.Stretch = Stretch.UniformToFill;
ShowMap(ipInfo.Latitude.ToString() + ", " + ipInfo.Longitude.ToString());
bsIndicator.IsBusy = false;
RadMap1.ZoomLevel = 10;
}
}
}
}