For some reason almost all solutions we have online use Google Maps to show maps, or allow the user to calculate. After a recent full FrontEnd project with some pure WebDeveoper’s and Microsoft consulting, i think i finally found why.
Essentially i think is because almost Web and Script developer’s use Google Maps API at a long time and don’t know how to start with this platform.
In my opinion Bing Maps can be also very interesting to use on a Website depending on the geography or Goals off the project.
In this post we will try to give you a clue off how to start with the use off this Maps platform and also the potential off the platform.
The main house off Bing Maps resource center os the site https://www.microsoft.com/maps/ where you have a lot off cool and easy to read documentation.
Bing Maps API are available for integration using:
- JavaScript
- XAML
- WebAPI
- Windows Store apps controls
In this post we will focus our attention on the JavaScript integration off Bing Maps. To start we will do a quick and guided exercise to allow the use off Bing Maps as illustrated on the first image in this post.
In this exercise we will consider JavaScript and HTML as main development language to integrate BingMaps.
To simplify development Microsoft had release an approach off integration based on controls, this option simplifies a lot our Work.
First of all let’s learn how to instantiate the Maps API and the needed control. So in the Head tag of our HTML let’s instantiate the Maps API and the Bing Maps control.
<script type=’text/javascript’ src=’http://www.bing.com/api/maps/mapcontrol?callback=GetMap’ async defer>
The Script tag links our WebPage to the Maps API and refer’s whats the JavaScript Function that will be called to instantiate our Maps control. In our case we are saying that the Map Control functionality will be injected by calling the GetMap function.
In the HTML we will elect a HTML tag to host the Maps control, the selected tag should be a tag with a container genesis, from a semantic Principle i sugest to use a Div. As an example we would have.
We should have a way to easy identify and manipulate the element so we had used an id to mark the element and become easy to manipulate the control without errors. We had also use an css class to give some styling to the map.
Like in other frameworks to use BingMaps API and Controls we should have a register acount, so we need to ask for a key to use the API. To ask your API key you should go the licensing bing WebSite to understand the terms and conditions and also the rules of the framework usage (https://www.microsoft.com/maps/licensing/licensing.aspx)
After you have the API key is time to build our Map Callback function, for a organization reason we will create a separated file for this. The first action we should consider is to call the Map Control Constructor to inject in to the DIV the MAP surface.
function GetMap() {
//Create an instance off a Bing Map 8 control (passsing the id off the div containing the map and the bing maps credentials)
var map = new Microsoft.Maps.Map(‘#contactMap’, {
credentials: ‘put the credentials you have’,
zoom: 16});
The parameters passed to the constructor are the identifier of the HTML tag that will render the Maps logic, the development keys and the inital zoom level.
using a variable to save a pointer to the mapcontrol is very useful to grant that we can call methods over the Map control to obtain information and change options, in our case we will define the zoom boundaries and to put a pin on an address.
//set initial zoom boundaries to grant a good experience to be defined with the design
map.setOptions({
maxZoom: 22,
minZoom: 8
});
The map Pin in Bing Maps are treated as autonomous entities you can have different pins representation and different actions. To create a new Pin over the Map we have first to instantiate the object
var pushpin = new Microsoft.Maps.Pushpin(map.getCenter(), {
icon: ‘iconpinmapa.png’, //using PNG as pin
anchor: new Microsoft.Maps.Point(12, 39)
});
After that we can put the pin over the map.
map.entities.push(pushpin);
To define a new pushpin we have the need to define what are the point where the pushpin should be present. The Microsoft.Mas.Point is the object that represents a point on the Map.
So at this moment we would have a BingMap in a web Page with a local marked. let´s see all the code together
<!DOCTYPE html>
<html lang=”en” xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta charset=”utf-8″ />
<title>bing Maps for a contact adddress pin</title>
<link rel=”stylesheet” type=”text/css” href=”stylesMap.css”>
<script type=’text/javascript’ src=’main.js’>
<script type=’text/javascript’ src=’http://www.bing.com/api/maps/mapcontrol?callback=GetMap’ async defer>
</head>
<body>
<h1>Presenting a contact Map </h1>
<p>
Example off a bing map instance pointing to the address:
</p>
</body>
</html>
And the JavaScript file:
// JavaScript source code
//Function associated with the creation off a Contactpin Map
function GetMap() {
//Create an instance off a Bing Map 8 control (passsing the id off the div containing the map and the bing maps credentials)
var map = new Microsoft.Maps.Map(‘#contactMap’, {
credentials: ‘AhgeX3IV-9M5Ssvz4NLBdtxk1zF8o1qO5KCB9sAr2S8NT0zUe_tMCv8cG4umDSgq’,
zoom: 16
});
//set initial zoom boundaries to grant a good experience to be defined with the design
map.setOptions({
maxZoom: 22,
minZoom: 8
});
//Add your costume pin to the map
var pushpin = new Microsoft.Maps.Pushpin(map.getCenter(), {
icon: ‘iconpinmapa.png’, //using PNG as pin
anchor: new Microsoft.Maps.Point(12, 39)
});
map.entities.push(pushpin);
}
And we are done with our first exercise, is very easy right?! Maybe next time you need some map you can now consider this platform, for me BingMaps object-oriented approach make him very easy and high extensible and configurable. Other strong point is the fact that Bing Maps has different integrations support and the API follows the same API approach for the different platforms.