Error ImageI used the below code and passed location.
But it doesn’t return any map.I replaced "New York" with variable.
console.log does show the value but somehow map is not being displayed.
<!DOCTYPE html>
<html>
<head>
<title></title>
<body>
Location: <input type="text" name="a" id="a"><br>
<button onclick="GetMap(document.getElementById('a').value )">Submit</button>
</body>
<meta charset="utf-8" />
<script type='text/javascript'>
var map, searchManager;
function GetMap(qry) {
map = new Microsoft.Maps.Map('#myMap', {
credentials: 'MY Key'
});
var qry1=qry;
//Make a request to geocode New York, NY.
geocodeQuery(qry1);
}
function geocodeQuery(query) {
console.log(query);
//If search manager is not defined, load the search module.
if (!searchManager) {
//Create an instance of the search manager and call the geocodeQuery function again.
Microsoft.Maps.loadModule('Microsoft.Maps.Search', function () {
searchManager = new Microsoft.Maps.Search.SearchManager(map);
geocodeQuery(query);
});
} else {
var searchRequest = {
where: query,
callback: function (r) {
//Add the first result to the map and zoom into it.
if (r && r.results && r.results.length > 0) {
var pin = new Microsoft.Maps.Pushpin(r.results[0].location);
map.entities.push(pin);
map.setView({ bounds: r.results[0].bestView });
}
},
errorCallback: function (e) {
//If there is an error, alert the user about it.
alert("No results found.");
}
};
//Make the geocode request.
searchManager.geocode(searchRequest);
}
}
</script>
<script type='text/javascript' src='http://www.bing.com/api/maps/mapcontrol?callback=GetMap' async defer></script>
</head>
<body>
<div id="myMap" style="position:relative;width:600px;height:400px;"></div>
</body>
</html>
2
Answers
In the
<head>
section of the HTML document should not contain a<body>
tag. Move the body-related HTML elements to the<body>
section.myMap
, but I didn’t see the HTML element with that ID. You need to add adiv
element with the IDmyMap
to your HTML.Updated code:
GetMap
as thecallback
parameter in the script source to ensure that the map is initialized after the Bing Maps API is loaded.