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

Why Database Modernization Matters for AI

  When companies transition to the cloud, they typically begin with applications and virtual machines, which is often the easier part of the process. The actual complexity arises later when databases are moved. To save time and effort, cloud adoption is more of a cloud migration in an IaaS manner, fulfilling current, but not future needs. Even organisations that are already in the cloud find that their databases, although “migrated,” are not genuinely modernised. This disparity becomes particularly evident when they begin to explore AI technologies. Understanding Modernisation Beyond Migration Database modernisation is distinct from merely relocating an outdated database to Azure. It's about making your data layer ready for future needs, like automation, real-time analytics, and AI capabilities. AI needs high throughput, which can be achieved using native DB cloud capabilities. When your database runs in a traditional setup (even hosted in the cloud), in that case, you will enc...

Cloud Myths: Migrating to the cloud is quick and easy (Pill 2 of 5 / Cloud Pills)

The idea that migration to the cloud is simple, straightforward and rapid is a wrong assumption. It’s a common misconception of business stakeholders that generates delays, budget overruns and technical dept. A migration requires laborious planning, technical expertise and a rigorous process.  Migrations, especially cloud migrations, are not one-size-fits-all journeys. One of the most critical steps is under evaluation, under budget and under consideration. The evaluation phase, where existing infrastructure, applications, database, network and the end-to-end estate are evaluated and mapped to a cloud strategy, is crucial to ensure the success of cloud migration. Additional factors such as security, compliance, and system dependencies increase the complexity of cloud migration.  A misconception regarding lift-and-shits is that they are fast and cheap. Moving applications to the cloud without changes does not provide the capability to optimise costs and performance, leading to ...

Cloud Myths: Cloud is Cheaper (Pill 1 of 5 / Cloud Pills)

Cloud Myths: Cloud is Cheaper (Pill 1 of 5 / Cloud Pills) The idea that moving to the cloud reduces the costs is a common misconception. The cloud infrastructure provides flexibility, scalability, and better CAPEX, but it does not guarantee lower costs without proper optimisation and management of the cloud services and infrastructure. Idle and unused resources, overprovisioning, oversize databases, and unnecessary data transfer can increase running costs. The regional pricing mode, multi-cloud complexity, and cost variety add extra complexity to the cost function. Cloud adoption without a cost governance strategy can result in unexpected expenses. Improper usage, combined with a pay-as-you-go model, can result in a nightmare for business stakeholders who cannot track and manage the monthly costs. Cloud-native services such as AI services, managed databases, and analytics platforms are powerful, provide out-of-the-shelve capabilities, and increase business agility and innovation. H...