Google Maps API styled map - colors problem - javascript

I can't properly set color of water with google maps api v3.
My RGB color is: #676a59. I've converted it to HSL using this tool: http://www.workwithcolor.com/hsl-color-picker-01.htm. The result is: color: hsl(71, 9%, 38%);
This is the code I use in Javascript to style water.
stylers: [{hue: "#676a59"}, {lightness: 38}, {saturation: 9}, {visibility: "on" }, {gamma: 1.0}]
The problem is that it doesn't work at all. I don't really know what is the reason of that. Can you see something that I'm doing wrong?
Thanks

When Google Maps API did not quite match the color I selected for a styled map, I I pulled up the map in a browser, took a screenshot of just the area with the two shades that weren't quite matching, opened that screenshot up in an image editor (pixlr.com, in my case), used the color-sucker tool to get the saturation and lightness for the shade that wasn't quite right, adjusted my saturation and/or lightness in my Google Maps APi call by 1 to get it closer to the saturation and/or lightness of the color I wanted, and repeated until I got something that matched. It was tedious--I ended up taking about a half dozen snapshots before it was perfect--but it worked.

I struggled with this for a long time, and tried two different Google Maps colour pickers without success.
However, the second one I tried alerted me to another way of specifying the colour: Rather than struggling with Google's take on HSL, you can just use the color property:
For example:
var mapStyles = [
{
featureType: "water",
stylers: [
{ color: '#b6c5dd' }
]
}
];

I did it like this and it is working.Take a look
var stylez = [{
featureType: "water",
elementType: "all",
stylers: [{
hue: "#006b33"
},
{
lightness: -70
},
{
saturation:100
}]
}];
//Map options
var mapOptions = {
zoom: 6,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControlOptions: {mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'yourName']}
};
//Make a new map
map = new google.maps.Map(document.getElementById("mapDiv"), mapOptions);
var styledMapOptions = {
name: "yourName"
}
var yourNameMapType = new google.maps.StyledMapType(
stylez, styledMapOptions);
map.mapTypes.set('yourName', yourNameMapType);
map.setMapTypeId('yourName');

Related

How can I hide street names in Google Maps JS API? [duplicate]

This question already has answers here:
google maps api v3 no labels?
(2 answers)
Closed 3 years ago.
I want to remove all the street names and pins of various places (e.g. museums, restaurants, and other spots) in Google Maps by using JS API.
Basically, all I need is just a map with no labels at all.
I assume it should be possible. How can I achieve it?
Here is the image of what I want to remove for better clarification.
Yes you can hide the labels which you don't require to display in map
<html>
<body>
<div id="map"></div>
<script>
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -33.86, lng: 151.209},
zoom: 13,
mapTypeControl: false
});
map.setOptions({styles: styles['hide']});
}
var styles = {
hide: [
{
featureType: 'poi.business',
stylers: [{visibility: 'off'}]
},
{
featureType: 'transit',
elementType: 'labels.icon',
stylers: [{visibility: 'off'}]
}
]
};
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
</script>
</script>
</body>
</html>
Still you can have granule control on the label type, to know more about all styles just read here: https://mapstyle.withgoogle.com/

Difficulty changing style of google map on button click

So I have a map using the google map api and I'm trying to change the water color on a button click.
I've been struggling with this one for a while now.
I initialised the map variable outside of the initMap function but that didn't seem to change anything.
var map;
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {
lat: 40.674,
lng: -73.945
},
zoom: 12,
styles: [
{
featureType: 'water',
elementType: 'geology',
stylers: [{color: '#17263c'}]
}
]
});
}
function showTest() {
var myStyle =[{
featureType: 'water',
elementType: 'geometry',
stylers: [
{ color: '#fc0101' }
]
}];
map.setOptions({styles: myStyle});
}
Even though you initialised a map variable outside your initMap function, the fact you then declare a map variable inside that function prefixed with var, makes it local to just that function.
Change that to:
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {

Show specific countries name on map

I am creating project using JavaScript. In my project I have integrated google map.My requirement is i want to show only some specific countries like:
India
USA
Australia
Currently I am hiding all countries using:
var emptyStyles =
[
{
featureType: "all",
elementType: "labels",
stylers: [ { visibility: "off" } ]
}
];
map.setOptions({styles: emptyStyles});
I have created plunker:
https://plnkr.co/edit/ZQIFZelNROkZcnFZ6mgB?p=preview
I found an example where specific cities were displayed on the map. The idea is to
hide all the labels(like you already did)
create a json array of the place that you want to show labels
for
Iterate through the json array and mark the labels.
You can find the code here: Display labels only to specific cities in Google Maps
Make following changes to the array:
var citiesJSON = {
geonames: [{
lat: 20.5937,
lng: 78.9629,
name: "India"
}, {
lat: 37.0902,
lng: -95.7129,
name: "USA"
}, {
lat: -25.2744,
lng: 133.7751,
name: "Australia"
}]
};

Mapbox geoJSON formatting

I am trying to make an interactive map using MapBox.
The basic idea is that people will hover over certain regions and they will light up and an event will trigger showing some content.
I got the map set up using Mapbox and then went ahead and got some data for the points by first
Going here to create a shapefile - http://www.gadm.org/country
And then going here to convert that file into a json file - http://www.mapshaper.org/
I then load the data into the map via mapbox and here is my result! (code below) - http://dev.touch-akl.com/nzmap/
As you can see thanks to the data I have downloaded it is almost a perfect trace of the country the only problem is that all the regions seem to be ONE object when hovering.
$.ajax({
url: '/nzmap/js/nzmap.json',
dataType: 'json',
success: function load(data) {
// set up the regions based on the JSON data and then call the styles and the hovers
var regions = L.geoJson(data, {
style: getStyle,
onEachFeature: onEachFeature
}).addTo(map);
// selects used to style the regionsLayer
function getStyle(feature) {
return {
weight: 2,
opacity: 0.1,
color: 'black',
fillOpacity: 0.7,
fillColor: 'red'
};
}
// combine the mouse in and mouse out?
function onEachFeature(feature, layer) {
layer.on({
mousemove: mousemove,
mouseout: mouseout
});
}
// mouse over change the layer styles
function mousemove(e) {
var layer = e.target;
// highlight feature
layer.setStyle({
weight: 3,
opacity: 0.3,
fillOpacity: 0.9
});
if (!L.Browser.ie && !L.Browser.opera) {
layer.bringToFront();
}
}
//mouse out reset the style
function mouseout(e) {
regions.resetStyle(e.target);
}
}
});
Here is a link to the json file if that helps understand what is going on - http://dev.touch-akl.com/nzmap//js/nzmap.json
It looks like some of the polygons are 'multipolygons' and some are seperate. I managed to create a new version of the map using just one region and it looks like this - http://dev.touch-akl.com/nzmap2/
My function for doing this is much the same except I have split the region off and use a json file that only has the coordinates for that region.
This is the JSON file for that example - http://dev.touch-akl.com/nzmap2/js/waikato.json
So where I am stuck is... Is there a way to format the original json file in a way that will keep each region as it's own separate object for hovers? And if so how can I go about changing the way I am setting up the map get the desired result?
It looks like I could make a seperate json file for each region like I am doing with the second example but that just does not seem like the right way to do this, been stuck for days so any help would be appreciated!
This is because what you are using isn't proper GeoJSON, at least not the GeoJSON L.GeoJSON expects. What you need is a GeoJSON FeatureCollection. A collection would look something like this:
[{
"type": "Feature",
"properties": {
"foo": "bar"
},
"geometry": {
"type": "Polygon",
"coordinates": [[
[-104.05, 48.99],
[-97.22, 48.98],
[-96.58, 45.94],
[-104.03, 45.94],
[-104.05, 48.99]
]]
}
}, {
"type": "Feature",
"properties": {
"bar": "foo"
},
"geometry": {
"type": "Polygon",
"coordinates": [[
[-109.05, 41.00],
[-102.06, 40.99],
[-102.03, 36.99],
[-109.04, 36.99],
[-109.05, 41.00]
]]
}
}]
As you can see, a FeatureCollection contains individual features which can be or type Point, MultiPoint, LineString, MultiLineString, Polygon or MultiPolygon. If you use a FeatureCollection L.GeoJSON will work as you would expect. Take a look at the reference for FeatureCollection and L.GeoJSON. A tutorial/example on using them both can be found here.

Adding Custom Markers to Custom Styled Google Maps API v3 for Wordpress website

Hi Everybody!
I am trying to create a Custom Styled Google map using API v3. The map will eventually go on the following Wordpress page (310px x 537px blank space on right side of page): http://www.flatschicago.com/edgewater-chicago-apartments/
I have copied the JavaScript/HTML code from the Google Maps Javascript API v3—Simple styled maps page, and have change the color scheme to fit my websites overall theme. I have managed to format the code with the colors that I want, and I have managed to format the size of the map so it fits the space designated on my web page.
Here are the 3 things that I am having issues with:
I want to add 5+ custom plot markers to the map. Where do I add this code?
I want the street labels to appear on the map. Where do I add this code?
I tried adding the code in to my Wordpress page, but I did not work. Is there a certain part of the JavaScript/HTML code that I should only be adding?
Here is the code that I have so far.
<html>
<head>
<title>Simple styled maps</title>
<style>
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
var map;
var edgewater = new google.maps.LatLng(41.987245, -87.661233);
var MY_MAPTYPE_ID = 'custom_style';
function initialize() {
var featureOpts = [
{
stylers: [
{ hue: '#3b3d38' },
{ visibility: 'simplified' },
{ gamma: 0.5 },
{ weight: 0.5 }
]
},
{
featureType: 'poi.business',
elementType: 'labels',
stylers: [
{ visibility: 'off' }
]
},
{
featureType: 'water',
stylers: [
{ color: '#3b3d38' }
]
}
];
var mapOptions = {
zoom: 12,
center: edgewater,
mapTypeControlOptions: {
mapTypeIds: [google.maps.MapTypeId.ROADMAP, MY_MAPTYPE_ID]
},
mapTypeId: MY_MAPTYPE_ID
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
var styledMapOptions = {
name: 'Custom Style'
};
var customMapType = new google.maps.StyledMapType(featureOpts, styledMapOptions);
map.mapTypes.set(MY_MAPTYPE_ID, customMapType);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body onload='intialize()'>
<div id="map-canvas" style='width:310px; height:537px;'></div>
</body>
</html>
1) Adding 5+ Custom Plot Markers
The code I am referring to is from the following page: page link.
I understand that I need to add a marker image and long/lat for each location, but when I try and copy and paste this code into the code that I already have created, the map doesn't work. Where am I supposed to be copying and pasting this code? Do it go inside the function initialize() code? Or is it its own function? Very confused.
2) Adding Street Labels
The code I am referring to is from the Google Maps Javascript API v3—Styled Maps page.
All I want to do is to be able to see the street labels on the map. The code I am referring to is this. I tried putting this in to my code, but again, it didn't work. Is there a simply label like 'roads,' 'on' that gets these labels to work?
var styleArray = [
{
featureType: "all",
stylers: [
{ saturation: -80 }
]
},{
featureType: "road.arterial",
elementType: "geometry",
stylers: [
{ hue: "#00ffee" },
{ saturation: 50 }
]
},{
featureType: "poi.business",
elementType: "labels",
stylers: [
{ visibility: "off" }
]
}
];
3) Add code to Wordpress page
The map will eventually go on the /edgewater-chicago-apartments/ pahe.
The space that I left for the map is labeled
<td class="neighborhood-map" rowspan="5"></td> :
If someone could please help me with this, I will be forever grateful. I feel like I am so close, yet these three things are holding me up and it is very frustrating. Anyone?
You need to have actual data (JSON format) to add the markers. For example:
// yourJsonData = data in valid JSON format
for ( i = 0; i < yourJsonData.length; i++ ) {
var latlng = new google.maps.LatLng(yourJsonData[ i ].lat, yourJsonData[ i ].long);
var marker = new google.maps.Marker({
map: map,
icon: yourURL + '/path-to-custom-markers/' + yourJsonData[ i ].icon,
position: latlng,
title: yourJsonData[ i ].title,
html: '<div class="info-window">Your window text</div>'
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(this.html);
infowindow.setOptions({maxWidth: 800});
infowindow.open(map, this);
});
}
This code loops an array (yourJsonData) that has values for latitude, longitude, etc.

Categories