I am making a single page app where in the second view I need to display the Google map. I am using the Google maps API where the map object is to be created.
var map = new google.maps.Map(mapId, {
zoom: 5,
center: new google.maps.LatLng(55, 11),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
The parameter mapId
is giving me a problem. The view contains a div with id say "mapId", but I am not able to get the id and so the map cannot be displayed. I tried HTML binding, attribute binding but it does not work. I am new to knockout. Please suggest some method
You should use a custom binding like so:
Your HTML would look like this:
Finally your view model:
Here is a fiddle that does a bit more than what you are asking but should work fine for your case as well.
I have modified "schmidlop" binding to reset marker on change in inputs (lat long inputs) and marker always in center of map.
Html
Binding , Include this in some js file and include it in html.
View Model
Instead of
mapId
you'll want to usedocument.getElementById('map')
, where'map'
is the id of the div containing the map (<div id="map"></div>
). This jsFiddle should help.Here's a good example of using Knockout JS and Google Maps. Hopefully, it will help get you on the right track. http://www.codeproject.com/Articles/387626/BikeInCity-2-KnockoutJS-JQuery-Google-Maps