Created
December 21, 2012 15:11
-
-
Save vgrem/4353382 to your computer and use it in GitHub Desktop.
Google Maps Control for List View
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SP.GeoMapControl=function() | |
{ | |
var _listId; | |
this.Init=function(listId) | |
{ | |
_listId = listId; | |
loadMapData(function(entries){ | |
if (entries==null) | |
{ | |
alert('No Map data was found'); | |
} | |
else | |
{ | |
initMapControl(entries); | |
} | |
}); | |
} | |
function initMapControl(mapEntries, params) { | |
var mapOptions = { | |
center: new google.maps.LatLng(-34.397, 150.644), | |
zoom: 8, | |
mapTypeId: google.maps.MapTypeId.ROADMAP | |
}; | |
var map = new google.maps.Map(document.getElementById("map_canvas"),mapOptions); | |
var infowindow = new google.maps.InfoWindow(); | |
var bounds = new google.maps.LatLngBounds; | |
var i,marker; | |
for (i = 0; i < mapEntries.length; i++) { | |
var mapEntry = mapEntries[i]; | |
var pos = new google.maps.LatLng(mapEntry.lat,mapEntry.lng); | |
marker = new google.maps.Marker({ | |
map: map, | |
position: pos | |
}); | |
google.maps.event.addListener(marker, 'click', (function(marker, i) { | |
return function() { | |
infowindow.setContent(mapEntries[i].content); | |
infowindow.open(map, marker); | |
} | |
})(marker, i)); | |
bounds.extend(pos); | |
} | |
map.fitBounds(bounds); | |
} | |
function loadMapData(fnCallback) { | |
var context = new SP.ClientContext.get_current(); | |
var web = context.get_web(); | |
var listMap = web.get_lists().getById(_listId); | |
var viewXml = '<View><RowLimit>1200</RowLimit></View>'; | |
var query = new SP.CamlQuery(); | |
query.set_viewXml(viewXml); | |
var mapItems = listMap.getItems(query); | |
context.load(mapItems); | |
context.add_requestSucceeded(onMapLoaded); | |
context.add_requestFailed(onMapFailure); | |
context.executeQueryAsync(); | |
function onMapLoaded() { | |
var mapsEntries=[]; | |
var count=mapItems.get_count(); | |
for (i=0 ; i < count; i++) | |
{ | |
var mapItem=mapItems.itemAt(i); | |
var dataValues=mapItem.get_fieldValues(); | |
var mapContent = ''; | |
if(dataValues['MapAdditionalInfo'] != null) | |
mapContent = dataValues['MapAdditionalInfo']; | |
mapsEntries.push({ mapId: dataValues['ID'], title: dataValues['Title'], lat: dataValues['MapLatitude'], lng: dataValues['MapLongitude'],content: mapContent}); | |
} | |
fnCallback(mapsEntries); | |
} | |
function onMapFailure() { | |
fnCallback(null); | |
} | |
} | |
}; | |
function initSPGeoMap() { | |
var mapCtl = new SP.GeoMapControl(); | |
mapCtl.Init(ctx.listName); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment