Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
$Data = @"
Place,Address,Item,Qty,Tot
Gillette Stadium,1 Patriot Pl Foxborough MA 02035,Green Frogs,550,1100
Fenway Park,4 Yawkey Way Boston MA 02215,Yellow Toads,100,5000
Boston Garden,100 Legends Way Boston MA 02114,Blue Dragons,5,10000
"@
$Path = Split-path $MyInvocation.MyCommand.Definition
$Locations = ForEach ($Addr in ($Data | ConvertFrom-Csv ))
{ $Addr.Address = $Addr.Address -replace " ","+"
$Location = Invoke-RestMethod -uri "http://maps.googleapis.com/maps/api/geocode/json?address=$($Addr.Address)&sensor=false"
[PSCustomObject]@{
Place = $Addr.Place
Location = $Location.Results.Formatted_Address
Lat = $Location.Results.Geometry.Location.Lat
Lng = $Location.Results.Geometry.Location.Lng
Item = $Addr.Item
Qty = $Addr.Qty
Tot = $Addr.Tot
}
}
$Markers = "var markers = [`n"
ForEach ($Num in (0..($Locations.Count - 1)))
{ $Location = $Locations[$Num].Location.Replace("`'","\'")
$Markers += " ['$($Locations[$Num].Place)','$Location',$($Locations[$Num].Lat),$($Locations[$Num].Lng)]"
If ($Num -lt ($Locations.Count - 1))
{ $Markers += ",`n"
}
}
$Markers += "`n ];`n"
ForEach ($Location in $Locations)
{ $TableInfo += "<li><p>$($Location.Place)<br>Item: $($Location.Item)<br>Qty: $($Location.Qty)<br>Tot: `$$($Location.Tot)</p></li>"
}
$HTML = @"
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Address Mapper</title>
<style>
html, body {
margin: 0px;
padding: 0px
}
#map-canvas {
height: 500px;
width: 800px;
margin: 0px;
padding: 0px
}
td {
vertical-align: top;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
function initialize() {
$Markers
var bounds = new google.maps.LatLngBounds();
var mapOptions = {
mapTypeId: google.maps.MapTypeId.HYBRID,
}
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
for( i = 0; i < markers.length; i++ ) {
var position = new google.maps.LatLng(markers[i][2], markers[i][3]);
bounds.extend(position);
var image = 'http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=' + (i + 1) + '|FF0000|000000';
marker = new google.maps.Marker({
position: position,
map: map,
title: markers[i][0] + '\n' + markers[i][1],
icon: image
});
}
if (markers.length > 1) {
map.fitBounds(bounds);
}
else {
map.setCenter(new google.maps.LatLng(markers[0][1],markers[0][2]));
map.setZoom(4);
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<table>
<td id="map-canvas"></td>
<td><b><ol>$TableInfo</ol></b></td>
</table>
</body>
</html>
"@
Write-Verbose "$(Get-Date): Saving file..."
Try {
$HTML | Out-File $Path\AddrMap.html -Encoding ASCII -ErrorAction Stop
}
Catch {
Write-Warning "Unable to save HTML at $Path\AddrMap.html because $($Error[0])"
Exit
}
& $Path\AddrMap.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment