Google Map not displaying after applying custom style - google-maps

I have nearly finished the code below which displays markers from an array, each with their own infoBox.
The final step is to style the map but despite following many online tutorials to the letter (changing the relevant bits to match my code) all I am getting is a totally grey map.
Here is my code:
// Set up map
var map;
var pop_up_info = "border: 0px solid black; background-color: #ffffff; padding:15px; margin-top: 8px; border-radius:10px; -moz-border-radius: 10px; -webkit-border-radius: 10px; box-shadow: 1px 1px #888;";
google.maps.event.addDomListener(window, 'load', initialize);
function initialize() {
var store_pins = new Array();
var pin_marker = new Array();
var pin_info = new Array();
var pin_infoop = new Array();
var pin_infotxt = new Array();
// Style Map
var style_map = [
{
"featureType": "administrative",
"stylers": [
{ "visibility": "off" }
]
},{
"featureType": "poi",
"stylers": [
{ "visibility": "off" }
]
},{
"featureType": "transit",
"stylers": [
{ "visibility": "off" }
]
},{
"featureType": "road",
"stylers": [
{ "visibility": "off" }
]
},{
"featureType": "landscape",
"stylers": [
{ "visibility": "on" },
{ "color": "#ffffff" }
]
},{
"featureType": "water",
"stylers": [
{ "visibility": "on" },
{ "color": "#00ffff" }
]
}
];
var styledMap = new google.maps.StyledMapType(style_map,{name: "Styled Map"});
store_pins = [
[‘Test Pin 1’, 55.144178, -2.254122,'pin','bellinghamcoop.jpg’,’Test Pin 1’],
[‘Test Pin 2’, 55.018754, -1.672230,'rugby','kingparktigers.jpg’,’Test Pin 2’]
];
var myOptions = {
zoom: 3,
minZoom: 3,
center: google.maps.LatLng(55.144178,-2.254122),
mapTypeControlOptions:
{
mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'map_style']
}
}
map = new google.maps.Map(document.getElementById("trackingT-map"), myOptions);
map.mapTypes.set('map_style', styledMap);
map.setMapTypeId('map_style');
// Main Loop
for(i=0;i<store_pins.length;i++)
{
var pos = new google.maps.LatLng(store_pins[i][1], store_pins[i][2]);
var pinIcon = {
url: 'icons/shirtpin.png',
//The size image file.
size: new google.maps.Size(50, 55),
//The point on the image to measure the anchor from. 0, 0 is the top left.
origin: new google.maps.Point(0, 0),
//The x y coordinates of the anchor point on the marker. e.g. If your map marker was a drawing pin then the anchor would be the tip of the pin.
anchor: new google.maps.Point(25, 20)
};
var pinShape = {
coord: [0,0,50,55],
type: 'rect'
};
pin_marker[i] = new google.maps.Marker(
{
position: pos,
map: map,
title: store_pins[i][0],
icon: pinIcon,
shape: pinShape,
zIndex: 107
}
);
//Creates the information to go in the pop-up info box.
pin_infotxt[i] = document.createElement("div");
pin_infotxt[i].style.cssText = pop_up_info;
pin_infotxt[i].innerHTML = '<span class="pop_up_box_text"><img src="content/TShirts/'+store_pins[i][4]+'" border="0" id="imgid'+i+'"/><h align="center">'+store_pins[i][5]+'</h></span>';
pin_infoop[i] = {
disableAutoPan: false,
maxWidth: 0,
pixelOffset: new google.maps.Size(-241, 0),
zIndex: null,
boxStyle: {
background: "url('infobox/pop_up_box_top_arrow.png') no-repeat",
opacity: 1,
width: "380px"
},
closeBoxMargin: "10px 2px 2px 2px",
closeBoxURL: "icons/button_close.png",
infoBoxClearance: new google.maps.Size(1, 1),
isHidden: false,
pane: "floatPane",
enableEventPropagation: false,
content: pin_infotxt[i]
};
google.maps.event.addListener(pin_marker[i], 'click', (function(marker, i) {
return function() {
for ( h = 0; h < pin_marker[i].length; h++ ) {
pin_marker[h].infobox.close();
}
map.panTo(this.position);
map.setZoom(15);
pin_info[i].open(map, this);
}
})(pin_marker[i], i));
pin_info[i] = new InfoBox(pin_infoop[i]);
}
};
I am sure it will be something simple.
Hope someone can help.
Jim

The simplest way to do it is to just apply the styles to the map:
var myOptions = {
zoom: 3,
minZoom: 3,
center: map_center,
styles: style_map,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("trackingT-map"), myOptions);
proof of concept fiddle (unless you really want to have another map type)
code snippet:
var map;
var pop_up_info = "border: 0px solid black; background-color: #ffffff; padding:15px; margin-top: 8px; border-radius:10px; -moz-border-radius: 10px; -webkit-border-radius: 10px; box-shadow: 1px 1px #888;";
google.maps.event.addDomListener(window, 'load', initialize);
var store_pins = [];
var pin_marker = [];
var pin_info = [];
var pin_infoop = [];
function initialize() {
var map_center = new google.maps.LatLng(55.144178, -2.254122);
// Style Map
var style_map = [{
"featureType": "administrative",
"stylers": [{
"visibility": "off"
}]
}, {
"featureType": "poi",
"stylers": [{
"visibility": "off"
}]
}, {
"featureType": "transit",
"stylers": [{
"visibility": "off"
}]
}, {
"featureType": "road",
"stylers": [{
"visibility": "off"
}]
}, {
"featureType": "landscape",
"stylers": [{
"visibility": "on"
}, {
"color": "#ffffff"
}]
}, {
"featureType": "water",
"stylers": [{
"visibility": "on"
}, {
"color": "#00ffff"
}]
}];
// var styledMap = new google.maps.StyledMapType(style_map,{name: "Styled Map"});
store_pins = [
['Test Pin 1', 55.144178, -2.254122, 'pin', 'bellinghamcoop.jpg', 'Test Pin 1'],
['Test Pin 2', 55.018754, -1.672230, 'rugby', 'kingparktigers.jpg', 'Test Pin 2']
];
var myOptions = {
zoom: 3,
minZoom: 3,
center: map_center,
styles: style_map,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("trackingT-map"), myOptions);
// Main Loop
for (i = 0; i < store_pins.length; i++) {
var pos = new google.maps.LatLng(store_pins[i][1], store_pins[i][2]);
var pinIcon = {
url: 'icons/shirtpin.png',
//The size image file.
size: new google.maps.Size(50, 55),
//The point on the image to measure the anchor from. 0, 0 is the top left.
origin: new google.maps.Point(0, 0),
//The x y coordinates of the anchor point on the marker. e.g. If your map marker was a drawing pin then the anchor would be the tip of the pin.
anchor: new google.maps.Point(25, 20)
};
var pinShape = {
coord: [0, 0, 50, 55],
type: 'rect'
};
pin_marker[i] = new google.maps.Marker({
position: pos,
map: map,
title: store_pins[i][0],
// icon: pinIcon,
// shape: pinShape,
zIndex: 107
});
pin_infoop[i] = {
disableAutoPan: false,
maxWidth: 0,
pixelOffset: new google.maps.Size(-75, 0),
zIndex: null,
boxStyle: {
background: "white",
opacity: 1,
width: "150px"
},
closeBoxMargin: "10px 2px 2px 2px",
closeBoxURL: "icons/button_close.png",
infoBoxClearance: new google.maps.Size(1, 1),
isHidden: false,
pane: "floatPane",
enableEventPropagation: false,
content: store_pins[i][5]
};
pin_info[i] = new InfoBox(pin_infoop[i]);
google.maps.event.addListener(pin_marker[i], 'click', (function(marker, i) {
return function() {
map.panTo(this.position);
map.setZoom(10);
pin_info[i].open(map, this);
}
})(pin_marker[i], i));
}
};
#trackingT-map {
height: 400px;
width: 400px;
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<script src="http://www.staff.org.au/infobox.js"></script>
<div id="trackingT-map"></div>

Related

Adding MarkerClustererPlus to existing Google Maps API embedded code [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm looking to add clustering to a google Maps API embed that I have on a webflow website. Is anyone able to explain how I would implement this ( https://codelabs.developers.google.com/codelabs/maps-platform-101-js#5 ). I'm not sure what to do in step one when it states 'Import the marketcluster' with it being an embed. Is it even possibly with this solution?
Here's the current set up... https://jsfiddle.net/geocodezip/nz14hubc/1/
// Variables for Google maps
var map, mapElem, markerImg, infoWindow, marker;
var markers = [], infoWindows = [],
races = [{lat: 40.7127753, lng: -74.0059728, url:""},
{lat: 40.735657, lng:-74.1723667, url:""}];
var mapOptions = {
mapTypeId: 'roadmap',
//zoom: 13,
//scrollwheel: false,
styles: [
{
"featureType": "administrative",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#444444"
}
]
},
{
"featureType": "landscape",
"elementType": "all",
"stylers": [
{
"color": "#f2f2f2"
}
]
},
{
"featureType": "poi",
"elementType": "all",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "road",
"elementType": "all",
"stylers": [
{
"saturation": -100
},
{
"lightness": 45
}
]
},
{
"featureType": "road.highway",
"elementType": "all",
"stylers": [
{
"visibility": "simplified"
}
]
},
{
"featureType": "road.arterial",
"elementType": "labels.icon",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "transit",
"elementType": "all",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "water",
"elementType": "all",
"stylers": [
{
"color": "#2bb0e6"
},
{
"visibility": "on"
}
]
}
]
};
function initialize() {
markerImg = {
url:'https://uploads-ssl.webflow.com/5f58a4616a9e71d63ca059c8/5fa18680b95c219254ad0c9c_place-marker.png',
size: new google.maps.Size(46, 57),
anchor: new google.maps.Point(23, 54),
}
// Display a map on the page
mapElem = document.getElementById('map_canvas');
map = new google.maps.Map(mapElem, mapOptions);
map.setTilt(45);
// Loop through our array of races
for(i = 0; i < races.length; i++) {
var race = races[i];
// Generate an infowindow content for the marker
var infoWindow = new google.maps.InfoWindow();
infoWindow.setContent(
'<div class="bg-race"</div>' +
'<p>'+race.name+'<br>Next race: '+race.date+'</p>' +
' Race wesbsite '
);
infoWindows.push(infoWindow);
// Place a marker on the map
createMarker(race.lat, race.lng, i);
}
// Center the map fitting all markers on the screen
fitToMarkers();
}
function createMarker(x, y, i) {
marker = new google.maps.Marker({
map: map,
icon: markerImg,
position: new google.maps.LatLng(x,y),
title: races[i].name
});
marker._index = i;
markers.push(marker);
// Click event on marker
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
// Close last opened infowindow if any
if(infoWindow) infoWindow.close();
// Open clicked infowindow
infoWindow = infoWindows[i];
infoWindow.open(map, marker);
}
})(marker, i));
}
function fitToMarkers() {
map.setZoom(13);
var bounds = new google.maps.LatLngBounds();
for(var i = 0; i < markers.length; i++) {
bounds.extend(markers[i].getPosition());
}
map.fitBounds(bounds);
map.setZoom(13); // zoom out when done so markers on the top can be seen
}
/*
// When Webflow has loaded,
Webflow.push(function() {
// Resize event
$(window).resize(function() {
// Do nothing if mobile
if($(window).width() < 768) return;
// Resize map if function is defined
if(typeof mapResize === 'function') mapResize();
});
});
*/
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map_canvas {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
<!DOCTYPE html>
<html>
<head>
<title>Simple Map</title>
<script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
<script
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBIwzALxUPNbatRBj3Xi1Uhp0fFzwWNBkE&callback=initialize&libraries=&v=weekly"
defer
></script>
<!-- jsFiddle will insert css and js -->
</head>
<body>
<div id="map_canvas"></div>
</body>
</html>
This tutorial: Marker Clustering would probably be more useful for your example.
Include the MarkerClusterer script:
<script src="https://unpkg.com/#google/markerclustererplus#4.0.1/dist/markerclustererplus.min.js"></script>
(or you can use a version from a different CDN)
Instantiate a MarkerClusterer:
// Add a marker clusterer to manage the markers.
new MarkerClusterer(map, markers, {
imagePath:
"https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m",
});
proof of concept fiddle
(note: commented out the fitToMarkers call and set the zoom and center so it would show the cluster)
// Variables for Google maps
var map, mapElem, markerImg, infoWindow, marker;
var markers = [], infoWindows = [],
races = [{lat: 40.7127753, lng: -74.0059728, url:""},
{lat: 40.735657, lng:-74.1723667, url:""}];
var mapOptions = {
center: races[0], // add center to initialize map
zoom: 8, // zoom out so can see cluster
mapTypeId: 'roadmap',
//zoom: 13,
//scrollwheel: false,
styles: [
{
"featureType": "administrative",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#444444"
}
]
},
{
"featureType": "landscape",
"elementType": "all",
"stylers": [
{
"color": "#f2f2f2"
}
]
},
{
"featureType": "poi",
"elementType": "all",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "road",
"elementType": "all",
"stylers": [
{
"saturation": -100
},
{
"lightness": 45
}
]
},
{
"featureType": "road.highway",
"elementType": "all",
"stylers": [
{
"visibility": "simplified"
}
]
},
{
"featureType": "road.arterial",
"elementType": "labels.icon",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "transit",
"elementType": "all",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "water",
"elementType": "all",
"stylers": [
{
"color": "#2bb0e6"
},
{
"visibility": "on"
}
]
}
]
};
function initialize() {
markerImg = {
url:'https://uploads-ssl.webflow.com/5f58a4616a9e71d63ca059c8/5fa18680b95c219254ad0c9c_place-marker.png',
size: new google.maps.Size(46, 57),
anchor: new google.maps.Point(23, 54),
}
// Display a map on the page
mapElem = document.getElementById('map_canvas');
map = new google.maps.Map(mapElem, mapOptions);
map.setTilt(45);
// Loop through our array of races
for(i = 0; i < races.length; i++) {
var race = races[i];
// Generate an infowindow content for the marker
var infoWindow = new google.maps.InfoWindow();
infoWindow.setContent(
'<div class="bg-race"</div>' +
'<p>'+race.name+'<br>Next race: '+race.date+'</p>' +
' Race wesbsite '
);
infoWindows.push(infoWindow);
// Place a marker on the map
createMarker(race.lat, race.lng, i);
}
// Add a marker clusterer to manage the markers.
new MarkerClusterer(map, markers, {
imagePath: 'https://unpkg.com/#google/markerclustererplus#4.0.1/images/m',
});
// Center the map fitting all markers on the screen
// fitToMarkers(); // comment out so can see clustering
}
function createMarker(x, y, i) {
marker = new google.maps.Marker({
map: map,
icon: markerImg,
position: new google.maps.LatLng(x,y),
title: races[i].name
});
marker._index = i;
markers.push(marker);
// Click event on marker
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
// Close last opened infowindow if any
if(infoWindow) infoWindow.close();
// Open clicked infowindow
infoWindow = infoWindows[i];
infoWindow.open(map, marker);
}
})(marker, i));
}
function fitToMarkers() {
map.setZoom(13);
var bounds = new google.maps.LatLngBounds();
for(var i = 0; i < markers.length; i++) {
bounds.extend(markers[i].getPosition());
}
map.fitBounds(bounds);
map.setZoom(13); // zoom out when done so markers on the top can be seen
}
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map_canvas {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
<!DOCTYPE html>
<html>
<head>
<title>Simple Map</title>
<script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
<script src="https://unpkg.com/#google/markerclustererplus#4.0.1/dist/markerclustererplus.min.js"></script>
<script
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initialize&libraries=&v=weekly"
defer
></script>
<!-- jsFiddle will insert css and js -->
</head>
<body>
<div id="map_canvas"></div>
</body>
</html>

Clickable countries using Google Maps API

I have just started dabbling in Google Maps API, but I am already stuck. I am looking for a way to recreate this type of map with google maps. I would need to remove all labels, get a blank background (I tried using a map style, but that didn't work for me, code example below) and light up the countries as I hover over them.
Are there any tutorials that I seem to have missed in my search that could help me or can anyone point me in the right direction? :)
var _mapstyle = [
{
featureType: "all",
elementType: "labels",
stylers: [
{ visibility: "off" }
]
}
];
function create_map()
{
_map = new google.maps.Map(document.getElementById("eyewebz-map"),
{
zoom: 2,
center: new google.maps.LatLng(20, 0),
navigationControl: true,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.DEFAULT
},
mapTypeControl: true,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
},
scaleControl: true,
mapTypeIds: ['_mapstyle']
});
_display.setMap(_map);
_display.setPanel(document.getElementById("NavigationText"));
}
EDIT
This is what the map would be used for: I have traveled a bit in my life and expect to do a lot more of it. Since my first big trip, I have been keeping a blog. I have now developed a new blog and I would like to make the countries that I've been to clickable and follow a url once they have been clicked. The ideal situation would be that when I click a country, it takes me to a page where only that specific country is shown in a map with some markers (places to visit). Once you click those markers, it should show a specific post/some information.
Something like this would be ok? Copy and paste inside a HTML page body (JSFiddler here).
<style type="text/css">
#map-canvas {
height: 600px;
width: 800px;
}
</style>
<script type="text/javascript"
src="https://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
// the map
var map;
function initialize() {
var myOptions = {
zoom: 2,
center: new google.maps.LatLng(10, 0),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
// initialize the map
map = new google.maps.Map(document.getElementById('map-canvas'),
myOptions);
// these are the map styles
var styles = [
{
stylers: [
{ hue: "#00ffe6" },
{ saturation: -20 }
]
},
{
featureType: "landscape",
stylers: [
{ hue: "#ffff66" },
{ saturation: 100 }
]
},{
featureType: "road",
stylers: [
{ visibility: "off" }
]
},{
featureType: "administrative.land_parcel",
stylers: [
{ visibility: "off" }
]
},{
featureType: "administrative.locality",
stylers: [
{ visibility: "off" }
]
},{
featureType: "administrative.neighborhood",
stylers: [
{ visibility: "off" }
]
},{
featureType: "administrative.province",
stylers: [
{ visibility: "off" }
]
},{
featureType: "landscape.man_made",
stylers: [
{ visibility: "off" }
]
},{
featureType: "landscape.natural",
stylers: [
{ visibility: "off" }
]
},{
featureType: "poi",
stylers: [
{ visibility: "off" }
]
},{
featureType: "transit",
stylers: [
{ visibility: "off" }
]
}
];
map.setOptions({styles: styles});
// Initialize JSONP request
var script = document.createElement('script');
var url = ['https://www.googleapis.com/fusiontables/v1/query?'];
url.push('sql=');
var query = 'SELECT name, kml_4326 FROM ' +
'1foc3xO9DyfSIF6ofvN0kp2bxSfSeKog5FbdWdQ';
var encodedQuery = encodeURIComponent(query);
url.push(encodedQuery);
url.push('&callback=drawMap');
url.push('&key=AIzaSyAm9yWCV7JPCTHCJut8whOjARd7pwROFDQ');
script.src = url.join('');
var body = document.getElementsByTagName('body')[0];
body.appendChild(script);
}
function drawMap(data) {
var rows = data['rows'];
for (var i in rows) {
if (rows[i][0] != 'Antarctica') {
var newCoordinates = [];
var geometries = rows[i][1]['geometries'];
if (geometries) {
for (var j in geometries) {
newCoordinates.push(constructNewCoordinates(geometries[j]));
}
} else {
newCoordinates = constructNewCoordinates(rows[i][1]['geometry']);
}
var country = new google.maps.Polygon({
paths: newCoordinates,
strokeColor: '#ff9900',
strokeOpacity: 1,
strokeWeight: 0.3,
fillColor: '#ffff66',
fillOpacity: 0,
name: rows[i][0]
});
google.maps.event.addListener(country, 'mouseover', function() {
this.setOptions({fillOpacity: 0.4});
});
google.maps.event.addListener(country, 'mouseout', function() {
this.setOptions({fillOpacity: 0});
});
google.maps.event.addListener(country, 'click', function() {
alert(this.name);
});
country.setMap(map);
}
}
}
function constructNewCoordinates(polygon) {
var newCoordinates = [];
var coordinates = polygon['coordinates'][0];
for (var i in coordinates) {
newCoordinates.push(
new google.maps.LatLng(coordinates[i][1], coordinates[i][0]));
}
return newCoordinates;
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<div id="map-canvas"></div>
This is a modified version of this
Fusion Tables Layer Example: Mouseover Map Styles.
You should also take a look to Styled Maps.
Another thing you may be interested in is Natural Earth Data. This in the case you need a polygon data source. And here an example of using it GViz API Example: Fusion Tables Data Source.
As the fusion tables are deprecated there is no inbuilt google solution to do this.
Here is small sample code that i made:
https://github.com/arturssmirnovs/Clickable-countries-using-Google-Maps-API
and working sample here: https://arturssmirnovs.github.io/Clickable-countries-using-Google-Maps-API/json/

google maps api - heatmap.set(data) property

I am creating a simple google map with heatmap layer. Importantly, I am not setting the data property of the heatmap initially:
var map, heatmap;
var home = new google.maps.LatLng(37.75, -122.4)
function initialize() {
var mapOptions = {
zoom: 10,
minZoom: 3,
center: home,
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: true,
panControl: false,
zoomControl: true,
mapTypeControl: false,
scaleControl: false,
streetViewControl: false,
overviewMapControl: false
};
var styles = [
{
stylers: [
{ "gamma": 0.83 },
{ hue: "#00ffe6" },
{ saturation: -20 }
]
},{
featureType: "road",
elementType: "geometry",
stylers: [
{ lightness: 100 },
{ visibility: "simplified" }
]
},{
featureType: "road",
elementType: "labels",
stylers: [
{ visibility: "off" }
]
}
];
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
map.setOptions({styles: styles});
heatmap = new google.maps.visualization.HeatmapLayer({
radius: 20,
opacity: 0.5
});
heatmap.setMap(map);
then I create a function which should set the data property of the heatmap, like this:
function update(){
all = [[37.782551, -122.445368, 0.75, 0.25],
[37.782745, -122.444586, 0.5, 0.5],
[37.782842, -122.443688, 0.2, 0.8]]
temp = []
for (var i = 0; i < all.length; i++){
temp.push(new google.maps.LatLng(all[i][0], all[i][1]), all[i][2])
}
var newdata = new google.maps.MVCArray(temp);
heatmap.set('data', heatmap.get('data') ? null : newdata)
}
finally, i call the update function in a bounds_changed event and initialize the map:
google.maps.event.addListener(map, 'bounds_changed', function() {
update;
});
}
google.maps.event.addDomListener(window, 'load', initialize);
However, when I inspect the heatmap variable in the console, the data property is null. Why is this? and how do I set the data property in a function call?
calling heatmap.set('data', heatmap.get('data') ? null : newdata); is problematic.
Remove that and it works.
working fiddle

Getting JSON code from Styled Maps Wizard to Integrate into Google Maps API html?

I used the styled maps wizard to create a styled version of my google maps, but I am not sure how to integrate the JSON into my html? Any help is appreciated. Thanks!
Here's a clip of the code.
{
"featureType": "road.highway.controlled_access",
"stylers": [
{ "color": "#808080" }
]
}
Also, here's the link to the jsfiddle.
Here's all of my current code.
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html {
height: 100%
}
body {
height: 100%;
margin: 0;
padding: 0
}
#map_canvas {
height: 100%
}
</style>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAoiYR3lgzcF8CxuOwe7SYsJAPjZQXOhQQ&sensor=false"></script>
<script type="text/javascript">
var line;
var title;
var map;
var loc;
var locations = [];
var image = new google.maps.MarkerImage('parkingIconRed.png');
var styles = [
{
"featureType": "road.highway.controlled_access",
"stylers": [
{ "color": "#808080" }
]
},{
"featureType": "road.local",
"elementType": "geometry.stroke",
"stylers": [
{ "color": "#80d9ff" },
{ "weight": 1.6 }
]
},{
"featureType": "road.arterial",
"elementType": "geometry.fill",
"stylers": [
{ "color": "#d2d3ff" }
]
},{
"featureType": "road.arterial",
"elementType": "labels.text.stroke",
"stylers": [
{ "color": "#ffffff" }
]
},{
"featureType": "landscape.man_made",
"elementType": "geometry.fill",
"stylers": [
{ "weight": 0.1 },
{ "color": "#f3f6f7" }
]
},{
"featureType": "road.highway",
"stylers": [
{ "color": "#b4b4ff" }
]
},{
"featureType": "road.highway",
"elementType": "labels.text.stroke",
"stylers": [
{ "color": "#ffffff" }
]
},{
"featureType": "road.highway",
"elementType": "geometry.stroke",
"stylers": [
{ "color": "#808080" }
]
},{
"featureType": "poi.school",
"stylers": [
{ "color": "#f1f1f1" }
]
},{
"featureType": "landscape.man_made",
"elementType": "geometry.stroke",
"stylers": [
{ "color": "#808080" },
{ "weight": 3.2 }
]
},{
"featureType": "administrative",
"stylers": [
{ "color": "#808080" }
]
},{
},{
}
]
map.setOptions({styles: styles});
function initialize() {
var raleigh = new google.maps.LatLng(35.77425, -78.639248);
var mapOptions = {
zoom : 17,
minZoom : 17,
center : raleigh,
mapTypeId : google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
var cityPlaza;
/*var RLINELayer = new google.maps.KmlLayer('http://caseymmiller.com/j586/MapsTest/RLINE.kml');
RLINELayer.setMap(map);*/
var plazaCoords = [
new google.maps.LatLng(35.773893, -78.63854),
new google.maps.LatLng(35.772944, -78.638594),
new google.maps.LatLng(35.772962, -78.639345),
new google.maps.LatLng(35.773884, -78.63928)
];
cityPlaza = new google.maps.Polygon({
paths : plazaCoords,
strokeColor : "#33CCFF",
strokeOpacity : 0.8,
strokeWeight : 2,
fillColor : "#33CCFF",
fillOpacity : 0.35
});
cityPlaza.setMap(map);
var parking = [
['Salisbury Deck', 35.775007, -78.640804],
['Cabarrus Deck', 35.774589, -78.640793],
['Hannover Deck', 35.774511, -78.640031],
['Convention Center Underground Deck', 35.773292, -78.639355],
['Performing Arts Deck', 35.772666, -78.641576],
['Charter Square Deck', 35.773893, -78.638551],
['Blount Street Deck', 35.776226, -78.637499],
['McDowell Street Surface Parking', 35.775303, -78.641673],
['Salisbury Parking Lot', 35.775442, -78.640814],
['Convention Center Parking Lot', 35.772553, -78.639527],
['Lenoir Street Parking Lot', 35.773231, -78.638207],
['Carrabus street Parking Lot', 35.774032, -78.63824]
];
for (var i = 0; i < parking.length; i++) {
createMarker(parking[i]);
}
var lineCoordinates = [new google.maps.LatLng(35.779742, -78.643389), new google.maps.LatLng(35.77438, -78.643733), new google.maps.LatLng(35.774259, -78.640396), new google.maps.LatLng(35.772953, -78.640482), new google.maps.LatLng(35.772866, -78.638465), new google.maps.LatLng(35.776879, -78.638315), new google.maps.LatLng(35.776766, -78.634989), new google.maps.LatLng(35.778097, -78.634883), new google.maps.LatLng(35.778202, -78.638209), new google.maps.LatLng(35.781178, -78.638059)];
var lineSymbol = {
path : google.maps.SymbolPath.CIRCLE,
scale : 8,
strokeColor : '#000'
};
line = new google.maps.Polyline({
path : lineCoordinates,
strokeColor : '#C82536',
icons : [{
icon : lineSymbol,
offset : '100%'
}],
map : map
});
animateCircle();
}
function animateCircle() {
var count = 0;
var time = offsetId = window.setInterval(function() {
count = (count + 1) % 2400;
var icons = line.get('icons');
icons[0].offset = (count / 24) + '%';
line.set('icons', icons);
}, 20);
}
function createMarker(parking) {
var myLatLng = new google.maps.LatLng(parking[1], parking[2]);
console.log(myLatLng.toUrlValue());
var marker = new google.maps.Marker({
position : myLatLng,
map : map,
icon : image,
title: parking[0],
});
return marker;
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="width:100%; height:100%"></div>
</body>
</html>​
You are currently setting the new style map option before the map is initialized which won't work ...
So either put the setOptions line after your "map = ... " line, or you can just include the styles in your existing mapOptions object:
var mapOptions = {
zoom : 17,
styles: styles,
minZoom : 17,
center : raleigh,
mapTypeId : google.maps.MapTypeId.ROADMAP
}
According documentation
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html {
height: 100%
}
body {
height: 100%;
margin: 0;
padding: 0
}
#map_canvas {
height: 100%
}
</style>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAoiYR3lgzcF8CxuOwe7SYsJAPjZQXOhQQ&sensor=false"></script>
<script type="text/javascript">
var line;
var title;
var map;
var loc;
var locations = [];
var image = new google.maps.MarkerImage('parkingIconRed.png');
var styles = [
{
"featureType": "road.highway.controlled_access",
"stylers": [
{ "color": "#808080" }
]
},{
"featureType": "road.local",
"elementType": "geometry.stroke",
"stylers": [
{ "color": "#80d9ff" },
{ "weight": 1.6 }
]
},{
"featureType": "road.arterial",
"elementType": "geometry.fill",
"stylers": [
{ "color": "#d2d3ff" }
]
},{
"featureType": "road.arterial",
"elementType": "labels.text.stroke",
"stylers": [
{ "color": "#ffffff" }
]
},{
"featureType": "landscape.man_made",
"elementType": "geometry.fill",
"stylers": [
{ "weight": 0.1 },
{ "color": "#f3f6f7" }
]
},{
"featureType": "road.highway",
"stylers": [
{ "color": "#b4b4ff" }
]
},{
"featureType": "road.highway",
"elementType": "labels.text.stroke",
"stylers": [
{ "color": "#ffffff" }
]
},{
"featureType": "road.highway",
"elementType": "geometry.stroke",
"stylers": [
{ "color": "#808080" }
]
},{
"featureType": "poi.school",
"stylers": [
{ "color": "#f1f1f1" }
]
},{
"featureType": "landscape.man_made",
"elementType": "geometry.stroke",
"stylers": [
{ "color": "#808080" },
{ "weight": 3.2 }
]
},{
"featureType": "administrative",
"stylers": [
{ "color": "#808080" }
]
},{
},{
}
]
function initialize() {
var raleigh = new google.maps.LatLng(35.77425, -78.639248);
var mapOptions = {
zoom : 17,
minZoom : 17,
center : raleigh,
mapTypeId : google.maps.MapTypeId.ROADMAP,
styles: styles
}
map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
var cityPlaza;
/*var RLINELayer = new google.maps.KmlLayer('http://caseymmiller.com/j586/MapsTest/RLINE.kml');
RLINELayer.setMap(map);*/
var plazaCoords = [
new google.maps.LatLng(35.773893, -78.63854),
new google.maps.LatLng(35.772944, -78.638594),
new google.maps.LatLng(35.772962, -78.639345),
new google.maps.LatLng(35.773884, -78.63928)
];
cityPlaza = new google.maps.Polygon({
paths : plazaCoords,
strokeColor : "#33CCFF",
strokeOpacity : 0.8,
strokeWeight : 2,
fillColor : "#33CCFF",
fillOpacity : 0.35
});
cityPlaza.setMap(map);
var parking = [
['Salisbury Deck', 35.775007, -78.640804],
['Cabarrus Deck', 35.774589, -78.640793],
['Hannover Deck', 35.774511, -78.640031],
['Convention Center Underground Deck', 35.773292, -78.639355],
['Performing Arts Deck', 35.772666, -78.641576],
['Charter Square Deck', 35.773893, -78.638551],
['Blount Street Deck', 35.776226, -78.637499],
['McDowell Street Surface Parking', 35.775303, -78.641673],
['Salisbury Parking Lot', 35.775442, -78.640814],
['Convention Center Parking Lot', 35.772553, -78.639527],
['Lenoir Street Parking Lot', 35.773231, -78.638207],
['Carrabus street Parking Lot', 35.774032, -78.63824]
];
for (var i = 0; i < parking.length; i++) {
createMarker(parking[i]);
}
var lineCoordinates = [new google.maps.LatLng(35.779742, -78.643389), new google.maps.LatLng(35.77438, -78.643733), new google.maps.LatLng(35.774259, -78.640396), new google.maps.LatLng(35.772953, -78.640482), new google.maps.LatLng(35.772866, -78.638465), new google.maps.LatLng(35.776879, -78.638315), new google.maps.LatLng(35.776766, -78.634989), new google.maps.LatLng(35.778097, -78.634883), new google.maps.LatLng(35.778202, -78.638209), new google.maps.LatLng(35.781178, -78.638059)];
var lineSymbol = {
path : google.maps.SymbolPath.CIRCLE,
scale : 8,
strokeColor : '#000'
};
line = new google.maps.Polyline({
path : lineCoordinates,
strokeColor : '#C82536',
icons : [{
icon : lineSymbol,
offset : '100%'
}],
map : map
});
animateCircle();
}
function animateCircle() {
var count = 0;
var time = offsetId = window.setInterval(function() {
count = (count + 1) % 2400;
var icons = line.get('icons');
icons[0].offset = (count / 24) + '%';
line.set('icons', icons);
}, 20);
}
function createMarker(parking) {
var myLatLng = new google.maps.LatLng(parking[1], parking[2]);
console.log(myLatLng.toUrlValue());
var marker = new google.maps.Marker({
position : myLatLng,
map : map,
icon : image,
title: parking[0],
});
return marker;
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="width:100%; height:100%"></div>
</body>
</html>

How to add multiple styles of Google Maps using StyledMapType?

I have added one additional style to a Google Map, following Google's instructions.
However I have had no sucess adding more than one style. Can this be achieved? If so, how?
Here is an example: http://jsfiddle.net/mikegoodstadt/A9zwa/3/
Many thanks for any suggestions,
Mike
var geocoder;
var map;
var MAPTYPE_1;
var MAPTYPE_2;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlngLondon = new google.maps.LatLng(51.507335, -0.127683);
var latlngMarrakech = new google.maps.LatLng(34.02590, -6.83640);
var style1 = [
{
"stylers": [
{ "visibility": "on" },
{ "weight": 0.9 },
{ "gamma": 0.99 },
{ "lightness": -4 },
{ "hue": "#ffc300" },
{ "saturation": -14 }
]
}
];
var style2 = [
{
"stylers": [
{ "visibility": "on" },
{ "weight": 0.9 },
{ "gamma": 0.99 },
{ "lightness": -4 },
{ "hue": "#ff6e00" },
{ "saturation": -14 }
]
}
];
var mapOptions = {
zoom: 12,
center: latlngLondon,
mapTypeControl: 1,
panControl: 0,
streetViewControl: 0,
zoomControl: 0,
mapTypeControlOptions: {
mapTypeIds: [google.maps.MapTypeId.ROADMAP, MAPTYPE_1, MAPTYPE_2]
},
};
map = new google.maps.Map(document.getElementById('map_canvas'),
mapOptions);
var mapType1 = new google.maps.StyledMapType(style1, {name: 'London'});
var mapType2 = new google.maps.StyledMapType(style2, {name: 'Marrakech'});
map.mapTypes.set(MAPTYPE_1, mapType1);
map.mapTypes.set(MAPTYPE_2, mapType2);
}
function codeAddress() {
var address = document.getElementById("search-address").value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert("Buscedad sin exito: " + status);
}
});
}
You need to declare/set the setMapTypeIDs
map.mapTypes.set('MAPTYPE_1', mapType1);
map.setMapTypeId('MAPTYPE_1');
map.mapTypes.set('MAPTYPE_2', mapType2);
map.setMapTypeId('MAPTYPE_2');