The Multimap API offers features to make requests to XML services (commonly referred to as AJAX) and parse XML via JavaScript. These features are provided in a non-browser-specific way, ensuring they are easy to use.
Important Note: Due to web browser security, XML HTTP requests can only be made to services on the domain from which the page is served.
This example demonstrates making a request to a static XML file located on a web server. Click here to download the file used in this example (you will need to right-click on the link and save it to your local computer).
If you view this in your browser you should see a Multimap draggable map centered on London, Great Britain.
A request is made for an XML file from the server; while this request is being made, a status indicator is visible on the right.
When the request has completed, the map is scaled automatically to fit a number of markers which highlight various London tourist attractions.
|
There are three parts to the code below. The first is the line that links to the Multimap API. The second is an example of the HTML you must have in your page. The third is an example of the JavaScript required to implement the page.
<script type="text/javascript" src="http://clients.multimap.com/API/maps/1.1/demo"></script>
var mapviewer, loading, request;
function onLoad()
{
// Add the map
mapviewer = new MultimapViewer( document.getElementById( 'mapviewer' ) );
mapviewer.addWidget( new MMSmallPanZoomWidget() );
mapviewer.drawAndPositionMap( new MMLocation( new MMLatLon( 51.5145, -0.1085 ), 15 ) );
// Add an Event Handler to show info boxes when the markers are clicked
mapviewer.addEventHandler( 'click', function( type, target, point )
{
if( ! point && target.infobox_html )
{
if( target.infoBoxOpened() ) {
target.closeInfoBox();
} else {
target.openInfoBox( target.infobox_html );
}
}
});
loading = document.getElementById( 'loading' );
loading.style.display = 'block';
// Set up the request, note that we're using a relative path as
// this will not work across domains.
request = mapviewer.getXMLHTTPRequest();
request.open( 'GET', 'demo.xml', true );
request.onreadystatechange = handleXML;
request.send(null);
}
function handleXML()
{
if( request.readyState == 4 ) {
loading.style.display = 'none';
if( request.status == 200 ) {
// For this example we're going to parse the text of the XML that
// comes back as an example, but the browser should parse it
// anyway and place it in request.responseXML if the file is
// correctly served with an application/xml mime type.
var xmlText = request.responseText;
var xmlDoc = mapviewer.parseXML( xmlText );
var markers = xmlDoc.documentElement.getElementsByTagName( "location" );
var points = []
for( var i = 0; i < markers.length && i < 10; ++i ) {
var point = new MMLatLon(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lon")) );
var icon = MM_DEFAULT_ICON.copy();
icon.image = 'http://www.multimap.com/share/mapviewer/i/q4/sm-marker'+(i+1)+'.png';
var marker = mapviewer.createMarker( point, markers[i].getAttribute( "title" ), icon );
marker.infobox_html = markers[i].getAttribute( "title" );
points.push( point );
}
var location = mapviewer.getAutoScaleLocation( points );
mapviewer.goToPosition( location );
} else {
alert( "There was a problem making the XML request." );
}
}
}
MMAttachEvent( window, 'load', onLoad );
For more information on the subject of Multimap API JavaScript code, please read the API documentation at: http://www.multimap.com/share/mapviewerapidocs/1.1/index.html.
If you require further help with your map API implementation, please contact our Customer Support team:
| Australia, Sydney | + 61 (0) 2 9262 6551 |
| Great Britain, London | +44 (0)20 7632 7777 |
| United States, Boston | + 1 617 423 4510 |
| email: | info@multimap.com |
For general account enquiries or further services, please contact your Multimap Sales team:
| Australia, Sydney | + 61 (0) 2 9262 6551 |
| Great Britain, London | +44 (0)20 7632 7800 |
| United States, Boston | + 1 617 423 4510 |
| email: | sales@multimap.com |