Skip to main content

How to use Bing Maps v7

In ziua de azi, de la o poza sau un document pana la un colet trimis prin curier sau localul la care ai iesit aseara poate sa fie track-uita pe harta. Aceste harti sunt oferite gratis (Bing, Maps, Google Maps etc), iar singurul lucru pe care trebuie sa il cunoastem sunt coordonate GPS.
Un utilizator poate sa foloseasca aceste harti fara nici o problema, iar cea mai mare parte din persoanele care iti acceseaza web situl nu o sa aibe probleme cu folosirea unei harti. Mai jos o sa discutam despre cum putem sa integram si sa folosim Bing Maps in web situl nostru.
Exista mai multe versiuni de API pentru Bing Maps, ultima versiune (vs 7.0) a fost regandita de la 0 si a devenit extrem de usor de folosit. O sa ne uitam peste versiunea 7.0, care este integrata in totalitate cu ajax.
Tot setup-ul se face din java script. Primul pas este sa ne cream un obiect de tip Map si sa specificam zona de pe pagina unde sa apara:
<head>
...
<script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>
<script type="text/javascript">
var map = null;
function getMap()
{
map = new Microsoft.Maps.Map(document.getElementById('mapDiv'), null);
}
</script>
</head>
<body onload="getMap();">
<div id='mapDiv' style="position:relative; width:600px; height:500px;"></div>
</body>
In momentul de fata avem harta afisata, dar avem un mesaj de eroare cane ne indeamna sa folosim credentiale valide. Harta se poate folosii si asa, doar ca banerul cu mesajul de eroare nu o sa il putem sterge. Ca sa scapam de el trebuie sa ne inregistram la adresa http://www.microsoft.com/maps/developers/web.aspx .
O sa primim o cheie unica pe care o sa o setam cand cream un nou obiect de tip map.
map = new Microsoft.Maps.Map(document.getElementById('myMap'),
{
credentials: '[CheieUnica]'
});
Avem harta pe pagina noastra, putem sa facem zoom in si zoom out si sa ne pozitionam oriunde vrem pe harta.
Urmatorul pas este sa adaugam cateva puncte( pushpin-uri). Ce este important la aceste pushpin-uri sunt coordonatele GPS. In exemplul de mai jos, unul din pushpin-uri va puncta spre o locatia data, iar celalat spre centrul hartii.
function updatePushpinLocation()
{
var pushpinLocatieData= new Microsoft.Maps.Pushpin(map.getCenter(), null);
map.entities.push(pushpinLocatieData);
pushpinLocatieData.setLocation(new Microsoft.Maps.Location(48, 52));

var pushpinCentruHarta= new Microsoft.Maps.Pushpin(map.getCenter(), null);
map.entities.push(pushpinCentruHarta);
}
In cazul in care pe evenimentul de click la un pushpin dorim sa afisam un info box, putem sa folosim obiecte de tip Infobox in loc de pushpin, iar de restul de ocupa API-ul de la Bing Maps. Fiecare infobox poate sa aibe un titlu si o descriere care sa se afiseze cand se da click pe acesta. Este bine de stiut ca putem insera si cod html, doar ca atunci trebuie sa folosim metoda setHtmlContent a obiectului Infobox.
var infoboxOptions = { title:'Titlu', description:'Descriere ...'};
var pushpin = new Microsoft.Maps.Infobox(map.getCenter(), infoboxOptions );
map.entities.push(pushpin);
Pe fiecare pushpin sau infobox putem sa adaugam evenimente precum click, etc.
pushpinClick= Microsoft.Maps.Events.addHandler(pushpin, 'click', clickEventInfo);
clickEventInfo= function (e) { alert("Click"); }
Orice eveniment pe harta poate sa fie prins si manipulat. In cazul in care dorim sa stergem toate punctele de pe harta este nevoie sa apelam functia map.entities.clear().
Unele browsere ne permit sa accesam locatia GPS a aparatului si sa localizam userul pe harta. Trebuie sa tinem cont ca utilizatorul o sa fie intrebat daca face share la aceasta informatie de catre brower.

var geoLocationProvider = new Microsoft.Maps.GeoLocationProvider(map);
geoLocationProvider.getCurrentPosition();
Ce mi s-a parut cel mai interesant am lasat la sfarsit. Putem sa incarcam sau sa folosim module scrise de alte persoane pentru a personaliza harta sau sa extindem functionalizatiile. Inregistrarea unui modul se face prin intermediul comenzii Microsoft.Maps.registerModule. Build-in putem sa incarcam module precum:
  • Traffic Module
  • Clustering Module
  • Direction Module
  • VenueMaps Module
  • Navigation Bar Module
Cel mai interesant modul mi se pare ca este Clustering Module, care ne permite sa grupam 1..n locatii pe harta sunt prea apropiate. Pe codeplex, puteti sa gasiti o colectie de module destul de utile bingmapsv7modules.codeplex.com.
O alta functionalitate este cautare pe harta. Tot ce trebuie sa facem este sa apelam un serviciu REST oferit de Bing Maps.

map.getCredentials(function(credentials) {
var searchRequest = 'http://dev.virtualearth.net/REST/v1/Locations/ClujNapoca?output=json&jsonp=SearchServiceCallback&key=' + credentials;
var mapscript = document.createElement('script');
mapscript.type = 'text/javascript';
mapscript.src = searchRequest;
document.getElementById('SDKmap').appendChild(mapscript);
});
Puteti sa gasiti exemple de cod in urmatoarea locatie http://www.bingmapsportal.com/ISDK/AjaxV7.

Enjoy.

Comments

Popular posts from this blog

Windows Docker Containers can make WIN32 API calls, use COM and ASP.NET WebForms

After the last post , I received two interesting questions related to Docker and Windows. People were interested if we do Win32 API calls from a Docker container and if there is support for COM. WIN32 Support To test calls to WIN32 API, let’s try to populate SYSTEM_INFO class. [StructLayout(LayoutKind.Sequential)] public struct SYSTEM_INFO { public uint dwOemId; public uint dwPageSize; public uint lpMinimumApplicationAddress; public uint lpMaximumApplicationAddress; public uint dwActiveProcessorMask; public uint dwNumberOfProcessors; public uint dwProcessorType; public uint dwAllocationGranularity; public uint dwProcessorLevel; public uint dwProcessorRevision; } ... [DllImport("kernel32")] static extern void GetSystemInfo(ref SYSTEM_INFO pSI); ... SYSTEM_INFO pSI = new SYSTEM_INFO(

Azure AD and AWS Cognito side-by-side

In the last few weeks, I was involved in multiple opportunities on Microsoft Azure and Amazon, where we had to analyse AWS Cognito, Azure AD and other solutions that are available on the market. I decided to consolidate in one post all features and differences that I identified for both of them that we should need to take into account. Take into account that Azure AD is an identity and access management services well integrated with Microsoft stack. In comparison, AWS Cognito is just a user sign-up, sign-in and access control and nothing more. The focus is not on the main features, is more on small things that can make a difference when you want to decide where we want to store and manage our users.  This information might be useful in the future when we need to decide where we want to keep and manage our users.  Feature Azure AD (B2C, B2C) AWS Cognito Access token lifetime Default 1h – the value is configurable 1h – cannot be modified

ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded

Today blog post will be started with the following error when running DB tests on the CI machine: threw exception: System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information. at System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(String providerTypeName, String providerInvariantName) This error happened only on the Continuous Integration machine. On the devs machines, everything has fine. The classic problem – on my machine it’s working. The CI has the following configuration: TeamCity .NET 4.51 EF 6.0.2 VS2013 It see