Problem With Adding Custom Styling to ReactJS Google Map - reactjs

I am trying to add custom styling to my google map on React JS, but I keep running into the same problem.
When I implement the styling(I have tried it in several ways) and launch my app the map disappears and instead I only have a white screen(however, the map shows when I run the default styling).
Here is my code:
import React, { Component } from 'react';
import { Map, GoogleApiWrapper, InfoWindow, Marker } from 'google-maps-react';
const key = 'key'
export class MapContainer extends Component {
state = {
showingInfoWindow: false,
activeMarker: {},
selectedPlace: {},
markers: [
//Array of markers
],
};
onMarkerClick = (props, marker, e) =>
this.setState({
selectedPlace: props,
activeMarker: marker,
showingInfoWindow: true
});
onClose = props => {
if (this.state.showingInfoWindow) {
this.setState({
showingInfoWindow: false,
activeMarker: null
});
}
};
render() {
return (
<Map
google={this.props.google}
zoom={14}
style={this.props.mapStyles}
initialCenter={{ lat: 49.166590, lng: -123.133569 }}
>
{this.state.markers.map((marker, index) => (
<Marker
key={index}
onClick={this.onMarkerClick}
name={marker.name}
position={marker.position}
/>
))}
<InfoWindow
marker={this.state.activeMarker}
visible={this.state.showingInfoWindow}
onClose={this.onClose}
>
<div>
<h4>{this.state.selectedPlace.name}</h4>
</div>
</InfoWindow>
</Map>
);
}
}
MapContainer.defaultProps = {
mapStyles: [
{
elementType: 'geometry',
stylers: [
{
color: '#242f3e'
}
]
},
{
elementType: 'labels.text.fill',
stylers: [
{
color: '#746855'
}
]
},
{
elementType: 'labels.text.stroke',
stylers: [
{
color: '#242f3e'
}
]
},
{
featureType: 'administrative.land_parcel',
elementType: 'labels',
stylers: [
{
visibility: 'off'
}
]
},
{
featureType: 'administrative.locality',
elementType: 'labels.text.fill',
stylers: [
{
color: '#d59563'
}
]
},
{
featureType: 'poi',
elementType: 'labels.text',
stylers: [
{
visibility: 'off'
}
]
},
{
featureType: 'poi',
elementType: 'labels.text.fill',
stylers: [
{
color: '#d59563'
}
]
},
{
featureType: 'poi.business',
stylers: [
{
visibility: 'off'
}
]
},
{
featureType: 'poi.park',
elementType: 'geometry',
stylers: [
{
color: '#263c3f'
}
]
},
{
featureType: 'poi.park',
elementType: 'labels.text',
stylers: [
{
visibility: 'off'
}
]
},
{
featureType: 'poi.park',
elementType: 'labels.text.fill',
stylers: [
{
color: '#6b9a76'
}
]
},
{
featureType: 'road',
elementType: 'geometry',
stylers: [
{
color: '#38414e'
}
]
},
{
featureType: 'road',
elementType: 'geometry.stroke',
stylers: [
{
color: '#212a37'
}
]
},
{
featureType: 'road',
elementType: 'labels.text.fill',
stylers: [
{
color: '#9ca5b3'
}
]
},
{
featureType: 'road.highway',
elementType: 'geometry',
stylers: [
{
color: '#746855'
}
]
},
{
featureType: 'road.highway',
elementType: 'geometry.stroke',
stylers: [
{
color: '#1f2835'
}
]
},
{
featureType: 'road.highway',
elementType: 'labels.text.fill',
stylers: [
{
color: '#f3d19c'
}
]
},
{
featureType: 'road.local',
elementType: 'labels',
stylers: [
{
visibility: 'off'
}
]
},
{
featureType: 'transit',
elementType: 'geometry',
stylers: [
{
color: '#2f3948'
}
]
},
{
featureType: 'transit.station',
elementType: 'labels.text.fill',
stylers: [
{
color: '#d59563'
}
]
},
{
featureType: 'water',
elementType: 'geometry',
stylers: [
{
color: '#17263c'
}
]
},
{
featureType: 'water',
elementType: 'labels.text.fill',
stylers: [
{
color: '#515c6d'
}
]
},
{
featureType: 'water',
elementType: 'labels.text.stroke',
stylers: [
{
color: '#17263c'
}
]
}
]
}
export default GoogleApiWrapper({
apiKey: key
})(MapContainer);
Been struggling with this for a couple hours, turned Google inside out for tutorials and followed many several times but to no avail. Any help would be greatly appreciated :)

I successfully implemented custom mapstyle using the syntax from the google-maps-react library documentation.
Here is my sample code. Make sure to change the value of the API key in the GoogleApiWrapper for the code to run. You can also use the Google Maps Styling Wizard to easily create styles for your map.
import React, { Component } from "react";
import { Map, Marker, GoogleApiWrapper } from "google-maps-react";
const mapStyle = [
{
elementType: "geometry",
stylers: [
{
color: "#1d2c4d"
}
]
},
{
elementType: "labels.text.fill",
stylers: [
{
color: "#8ec3b9"
}
]
},
{
elementType: "labels.text.stroke",
stylers: [
{
color: "#1a3646"
}
]
},
{
featureType: "administrative.country",
elementType: "geometry.stroke",
stylers: [
{
color: "#4b6878"
}
]
},
{
featureType: "administrative.land_parcel",
elementType: "labels.text.fill",
stylers: [
{
color: "#64779e"
}
]
},
{
featureType: "administrative.province",
elementType: "geometry.stroke",
stylers: [
{
color: "#4b6878"
}
]
},
{
featureType: "landscape.man_made",
elementType: "geometry.stroke",
stylers: [
{
color: "#334e87"
}
]
},
{
featureType: "landscape.natural",
elementType: "geometry",
stylers: [
{
color: "#023e58"
}
]
},
{
featureType: "poi",
elementType: "geometry",
stylers: [
{
color: "#283d6a"
}
]
},
{
featureType: "poi",
elementType: "labels.text.fill",
stylers: [
{
color: "#6f9ba5"
}
]
},
{
featureType: "poi",
elementType: "labels.text.stroke",
stylers: [
{
color: "#1d2c4d"
}
]
},
{
featureType: "poi.park",
elementType: "geometry.fill",
stylers: [
{
color: "#023e58"
}
]
},
{
featureType: "poi.park",
elementType: "labels.text.fill",
stylers: [
{
color: "#3C7680"
}
]
},
{
featureType: "road",
elementType: "geometry",
stylers: [
{
color: "#304a7d"
}
]
},
{
featureType: "road",
elementType: "labels.text.fill",
stylers: [
{
color: "#98a5be"
}
]
},
{
featureType: "road",
elementType: "labels.text.stroke",
stylers: [
{
color: "#1d2c4d"
}
]
},
{
featureType: "road.highway",
elementType: "geometry",
stylers: [
{
color: "#2c6675"
}
]
},
{
featureType: "road.highway",
elementType: "geometry.stroke",
stylers: [
{
color: "#255763"
}
]
},
{
featureType: "road.highway",
elementType: "labels.text.fill",
stylers: [
{
color: "#b0d5ce"
}
]
},
{
featureType: "road.highway",
elementType: "labels.text.stroke",
stylers: [
{
color: "#023e58"
}
]
},
{
featureType: "transit",
elementType: "labels.text.fill",
stylers: [
{
color: "#98a5be"
}
]
},
{
featureType: "transit",
elementType: "labels.text.stroke",
stylers: [
{
color: "#1d2c4d"
}
]
},
{
featureType: "transit.line",
elementType: "geometry.fill",
stylers: [
{
color: "#283d6a"
}
]
},
{
featureType: "transit.station",
elementType: "geometry",
stylers: [
{
color: "#3a4762"
}
]
},
{
featureType: "water",
elementType: "geometry",
stylers: [
{
color: "#0e1626"
}
]
},
{
featureType: "water",
elementType: "labels.text.fill",
stylers: [
{
color: "#4e6d70"
}
]
}
];
export class MapContainer extends Component {
_mapLoaded(mapProps, map) {
map.setOptions({
styles: mapStyle
});
}
render() {
const coords = { lat: -21.805149, lng: -49.0921657 };
return (
<Map
style={this.mapStyle}
google={this.props.google}
zoom={7}
initialCenter={coords}
onReady={(mapProps, map) => this._mapLoaded(mapProps, map)}
>
<Marker position={coords} />
</Map>
);
}
}
export default GoogleApiWrapper({
apiKey: "YOUR_API_KEY"
})(MapContainer);
Hope this helps!

You can use default props for this. The custom Style json data should be a default props. Here is a working solution :
import React, { Component } from "react";
import { Map, GoogleApiWrapper } from "google-maps-react";
export class MapContainer extends Component {
render() {
return (
<Map
google={this.props.google}
zoom={14}
styles={this.props.mapStyles}
initialCenter={{
lat: 51.1657,
lng: 10.4515,
}}
/>
);
}
}
//this is how you make the style a defaultprops for the component.
//best way is to add the design json data in a separte line
// go here for different Map style and customization : http://snazzymaps.com/
MapContainer.defaultProps = {
mapStyles: [
{
"featureType": "all",
"elementType": "labels",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "all",
"elementType": "labels.text",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "all",
"elementType": "labels.text.fill",
"stylers": [
{
"saturation": 36
},
{
"color": "#111111"
},
{
"lightness": 60
}
]
},
{
"featureType": "all",
"elementType": "labels.text.stroke",
"stylers": [
{
"visibility": "on"
},
{
"color": "#000000"
},
{
"lightness": 16
}
]
},
{
"featureType": "all",
"elementType": "labels.icon",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "administrative",
"elementType": "geometry.fill",
"stylers": [
{
"color": "#000000"
}
]
},
{
"featureType": "administrative",
"elementType": "geometry.stroke",
"stylers": [
{
"color": "#000000"
},
{
"lightness": 17
},
{
"weight": 1.2
}
]
},
{
"featureType": "administrative",
"elementType": "labels",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "administrative",
"elementType": "labels.text",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "administrative.country",
"elementType": "all",
"stylers": [
{
"visibility": "simplified"
}
]
},
{
"featureType": "administrative.country",
"elementType": "geometry",
"stylers": [
{
"visibility": "simplified"
}
]
},
{
"featureType": "administrative.country",
"elementType": "labels.text",
"stylers": [
{
"visibility": "simplified"
}
]
},
{
"featureType": "administrative.province",
"elementType": "all",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "administrative.locality",
"elementType": "all",
"stylers": [
{
"visibility": "simplified"
},
{
"saturation": "-100"
},
{
"lightness": "30"
}
]
},
{
"featureType": "administrative.neighborhood",
"elementType": "all",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "administrative.land_parcel",
"elementType": "all",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "landscape",
"elementType": "all",
"stylers": [
{
"visibility": "simplified"
},
{
"gamma": "0.00"
},
{
"lightness": "74"
}
]
},
{
"featureType": "landscape",
"elementType": "geometry",
"stylers": [
{
"color": "#000000"
},
{
"lightness": 20
}
]
},
{
"featureType": "landscape",
"elementType": "labels",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "landscape",
"elementType": "labels.text",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "landscape.man_made",
"elementType": "all",
"stylers": [
{
"lightness": "3"
}
]
},
{
"featureType": "poi",
"elementType": "all",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "poi",
"elementType": "geometry",
"stylers": [
{
"color": "#000000"
},
{
"lightness": 40
}
]
},
{
"featureType": "poi",
"elementType": "labels",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "poi",
"elementType": "labels.text",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "road",
"elementType": "geometry",
"stylers": [
{
"visibility": "simplified"
},
{
"color": "#424242"
},
{
"lightness": "-61"
}
]
},
{
"featureType": "road",
"elementType": "labels",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "road",
"elementType": "labels.text",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "road.highway",
"elementType": "geometry.fill",
"stylers": [
{
"color": "#000000"
},
{
"lightness": 17
}
]
},
{
"featureType": "road.highway",
"elementType": "geometry.stroke",
"stylers": [
{
"color": "#000000"
},
{
"lightness": 29
},
{
"weight": 0.2
}
]
},
{
"featureType": "road.arterial",
"elementType": "geometry",
"stylers": [
{
"color": "#000000"
},
{
"lightness": 18
}
]
},
{
"featureType": "road.local",
"elementType": "geometry",
"stylers": [
{
"color": "#000000"
},
{
"lightness": 16
}
]
},
{
"featureType": "transit",
"elementType": "geometry",
"stylers": [
{
"color": "#2a2727"
},
{
"lightness": "-61"
},
{
"saturation": "-100"
}
]
},
{
"featureType": "transit",
"elementType": "labels",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "water",
"elementType": "geometry",
"stylers": [
{
"color": "#000000"
},
{
"lightness": 17
}
]
},
{
"featureType": "water",
"elementType": "labels",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "water",
"elementType": "labels.text",
"stylers": [
{
"visibility": "on"
}
]
}
]
};
export default GoogleApiWrapper({
apiKey: "your API key goes here",
})(MapContainer);

Related

How to update the value to the nested object array in mongodb document?

I have following document on which the update needs to be done.
{
"_id": "Colorcode_1",
"Combination": [
{
"color": [
{
"mixture": [
"Red",
"Green"
]
}
],
"code": "Maroon"
},
{
"color": [
{
"mixture": [
"Yellow",
"Green"
]
}
],
"code": "Light Green"
}
]
}
Now what I need to do is to update the document by adding the value "Blue" in the "mixture" field where "code" is "Maroon". Something like this. This needs to be done using $addToSet
{
"_id": "Colorcode_1",
"Combination": [
{
"color": [
{
"mixture": [
"Red",
"Green",
"Blue"
]
}
],
"code": "Maroon"
},
{
"color": [
{
"mixture": [
"Yellow",
"Green"
]
}
],
"code": "Light Green"
}
]
}
Any help regarding this would be highly helpful.
Here is option with arrayFilters:
db.collection.update({
"Combination.code": "Maroon"
},
{
"$addToSet": {
"Combination.$[x].color.$[y].mixture": "Blue"
}
},
{
arrayFilters: [
{
"x.code": "Maroon"
},
{
"y.mixture": {
$exists: true
}
}
]
})
Explained:
Filter all documents having code:Marron , good to have index on this field if collection is big
Use arrayFilter x.code to add the array element to mixture if mixture exists ( identified by y arrayFilter)
playground
I found this update difficult because of the data model, and I'm hoping you'll get a better/simpler answer.
Anyway, here's one way you could do it. I would test this on more/different data to insure it's correct.
db.collection.update({
"_id": "Colorcode_1",
"Combination.code": "Maroon"
},
[
{
"$set": {
"Combination": {
"$map": {
"input": "$Combination",
"as": "elem",
"in": {
"$cond": [
{ "$eq": [ "$$elem.code", "Maroon" ] },
{
"$mergeObjects": [
"$$elem",
{
"color": {
"$map": {
"input": "$$elem.color",
"as": "colorElem",
"in": {
"$cond": [
{
"$reduce": {
"input": { "$objectToArray": "$$colorElem" },
"initialValue": false,
"in": {
"$or": [
"$$value",
{ "$eq": [ "$$this.k", "mixture" ] }
]
}
}
},
{
"mixture": {
"$setUnion": [ "$$colorElem.mixture", [ "Blue" ] ]
}
},
"$$colorElem"
]
}
}
}
}
]
},
"$$elem"
]
}
}
}
}
}
])
Try it on mongoplayground.net.

How to reverse $unwind or re-assemble after $lookup?

I´ve been trying to reverse $unwind in nested array. Please, if you could help me it would be great. Thanks in advance.
Here are the details:
checklists collection, this collection has steps and each step has many areas, and I'd like to lookup to fill the area by id. I did it but I cannot reverse $unwind.
{
"steps": [{
"name": "paso1",
"description": "paso1",
"estimated_time": 50,
"active": true,
"areas": [{
"area_id": "60b6e728c44f0365c0d547d6"
}, {
"area_id": "60b6e7a2c44f0365c0d547d8"
}]
}, {
"name": "paso2",
"description": "o",
"estimated_time": 7,
"active": true,
"areas": [{
"area_id": "60b6e76ac44f0365c0d547d7"
}]
}, {
"name": "paso2",
"description": "l",
"estimated_time": 7,
"active": true,
"areas": [{
"area_id": "60b6e728c44f0365c0d547d6"
}]
}],
"name": "prueba",
"description": "prueba",
"type": "prueba",
"active": true,
"updated_at": {
"$date": "2021-06-02T23:56:02.232Z"
},
"created_at": {
"$date": "2021-06-01T22:44:57.114Z"
},
"__v": 0
}
area collection
{
"_id":"60b6e706c44f0365c0d547d5"
"name": "Development",
"short_name": "DEV",
"description": "Development area",
"updated_at": {
"$date": "2021-06-02T02:03:50.383Z"
},
"created_at": {
"$date": "2021-06-02T02:03:50.383Z"
},
"__v": 0,
"active": true
}
My aggregation
db.checklists.aggregate([
{
"$unwind": "$steps"
},
{
"$unwind": "$steps.areas"
},
{
"$lookup": {
"from": "areas",
"let": {
"area_id": {
"$toObjectId": "$steps.areas.area_id"
}
},
"pipeline": [
{
"$match": {
"$expr": {
"$eq": [
"$_id",
"$$area_id"
]
}
}
}
],
"as": "convertedItems"
}
},
{
"$group": {
"_id": "$steps.name",
"root": {
"$first": "$$ROOT"
},
"items": {
"$push": {
"$mergeObjects": [
"$steps.areas",
{
"$arrayElemAt": [
"$convertedItems",
0
]
}
]
}
},
}
},
{
"$addFields": {
"values": {
"$reduce": {
"input": "$items",
"initialValue": [],
"in": {
"$concatArrays": [
"$$value",
{
"$cond": [
{
"$in": [
"$$this.area_id",
"$$value.area_id"
]
},
[],
[
"$$this"
]
]
}
]
}
}
}
}
},
{
"$addFields": {
"root.steps.areas": "$values"
}
},
{
"$replaceRoot": {
"newRoot": "$root"
}
},
{
"$group": {
"_id": "$_id",
"root": {
"$first": "$$ROOT"
},
"steps": {
"$push": "$steps"
}
}
},
{
"$addFields": {
"root.steps": "$steps"
}
},
{
"$replaceRoot": {
"newRoot": "$root"
}
},
{
"$project": {
"convertedItems": 0
}
}
])
I don´t get to form this output:
{
"steps": [{
"name": "paso1",
"description": "paso1",
"estimated_time": 50,
"active": true,
"areas": [{
"_id": "60b6e728c44f0365c0d547d6",
"name":"Development",
..... //join or lookup
}, {
"_id": "60b6e7a2c44f0365c0d547d8",
"name":"Development",
..... //join or lookup
}]
}],
"name": "prueba",
"description": "prueba",
"type": "prueba",
"active": true,
"updated_at": {
"$date": "2021-06-02T23:56:02.232Z"
},
"created_at": {
"$date": "2021-06-01T22:44:57.114Z"
},
"__v": 0
}
Thank you very much!
$unwind deconstruct steps array
$lookup with areas collection pass area_id in let
$match to check is _id in area_ids after converting to string
$project to show required fields
$group by _id and reconstruct the steps array and pass your required fields
db.checklists.aggregate([
{ $unwind: "$steps" },
{
$lookup: {
from: "areas",
let: { area_id: "$steps.areas.area_id" },
pipeline: [
{
$match: {
$expr: { $in: [{ $toString: "$_id" }, "$$area_id"] }
}
},
{ $project: { name: 1 } }
],
as: "steps.areas"
}
},
{
$group: {
_id: "$_id",
steps: { $push: "$steps" },
name: { $first: "$name" },
description: { $first: "$description" },
type: { $first: "$type" },
active: { $first: "$active" },
updated_at: { $first: "$updated_at" },
created_at: { $first: "$created_at" },
__v: { $first: "$__v" }
}
}
])
Playground

Iterate though a json object and get specific data

I have a json which comes from an API,
"nutrient_value": [
{
"Calcium": [
"29.16",
"mg",
"Red",
"0.00102858984",
"ounce"
]
},
{
"Choline": [
"118.97",
"mg",
"Red",
"0.00419654778",
"ounce"
]
},
{
"Copper": [
"0.12",
"mg",
"Red",
"0.00000423288",
"ounce"
]
},
{
"Crude fat": [
"29.16",
"g",
"Green",
"1.02858984",
"ounce"
]
},
{
"Folate": [
"11.66",
"mcg",
"Red",
"0.00000041129484",
"ounce"
]
},
{
"Iodine": [
"0.0",
"mcg",
"Red",
"0.0",
"ounce"
]
},
{
"Iron": [
"4.08",
"mg",
"Yellow",
"0.00014391792",
"ounce"
]
},
{
"Magnesium": [
"34.99",
"mg",
"Green",
"0.00123423726",
"ounce"
]
},
{
"Manganese": [
"0.02",
"mg",
"Red",
"0.00000070548",
"ounce"
]
},
{
"Niacin (B3)": [
"9.04",
"mg",
"Green",
"0.00031887696",
"ounce"
]
},
{
"Omega-3 excl. ALA and SDA": [
"0.02",
"g",
"Red",
"0.00070548",
"ounce"
]
},
{
"Omega-6": [
"0.02",
"g",
"Red",
"0.00070548",
"ounce"
]
},
{
"Pantothenic acid (B5)": [
"1.07",
"mg",
"Red",
"0.00003774318",
"ounce"
]
},
{
"Phosphorus": [
"332.42",
"mg",
"Red",
"0.01172578308",
"ounce"
]
},
{
"Potassium": [
"573.48",
"mg",
"Yellow",
"0.02022893352",
"ounce"
]
},
{
"Protein": [
"36.94",
"g",
"Green",
"1.30302156",
"ounce"
]
},
{
"Riboflavin (B2)": [
"0.29",
"mg",
"Red",
"0.00001022946",
"ounce"
]
},
{
"Selenium": [
"30.72",
"mcg",
"Red",
"0.00000108361728",
"ounce"
]
},
{
"Sodium (Na)": [
"128.3",
"mg",
"Green",
"0.0045256542",
"ounce"
]
},
{
"Thiamin (B1)": [
"0.08",
"mg",
"Red",
"0.00000282192",
"ounce"
]
},
{
"Vitamin A": [
"8.16",
"mcg",
"Red",
"0.00000028783584",
"ounce"
]
},
{
"Vitamin C": [
"0.0",
"mg",
"Green",
"0.0",
"ounce"
]
},
{
"Vitamin D": [
"0.19",
"mcg",
"Red",
"0.00000000670206",
"ounce"
]
},
{
"Vitamin E": [
"0.33",
"mg",
"Red",
"0.00001164042",
"ounce"
]
},
{
"Zinc (Zn)": [
"8.71",
"mg",
"Red",
"0.00030723654",
"ounce"
]
},
{
"Calories": [
"417.96",
"cal",
null,
"N/A",
"ounce"
]
},
{
"Omega-3/6 ratio": [
"0.65",
"ratio",
"Green",
"N/A",
"ounce"
]
},
{
"Calcium/Phosphorus ratio": [
"0.06",
"ratio",
"Red",
"N/A",
"ounce"
]
}
],
From the above json im decoding it into this
var resJson = json.decode(res);
MPD.Result _mealPlan = MPD.Result.fromJson(resJson["result"]);
///
factory Result.fromJson(Map<String, dynamic> json) => Result(
id: json["id"],
....
nutrientValue: List<NutrientValue>.from(json["nutrient_value"].map((x) => NutrientValue.fromJson(x))),
....
);
"nutrient_value": []
And inside this nutrient_value array, for each item I need to check if given key (eg: "Calcium") exists and if yes, then fetch the values of index [0] and [2] of that key:
Example key "Calcium" exists and
valueCalcium = value of index[0]
colorCalcium = value of index[2]
"nutrient_value": [
{
"Calcium": [
"29.16",
"mg",
"Red",
"0.00102858984",
"ounce"
}
how can I do this in an iterative way?
I have tried this
_mealPlan.nutrientValue.forEach((element) {
if(element.calcium.isNotEmpty){
valCalcium = element.calcium[0];
print(valCalcium);//_mealPlan.nutrientValue[0].calcium[0];
calciumClr = element.calcium[2];
print(calciumClr);
}
});
but it doesn't seem to be the right way.
I'm using each of those two index values in later parts in my code.
You can do like this:
void main() {
Map<String, dynamic> data = {
"success": true,
"result": {
"id": 7,
"daily_allowance_actual_percentage": {
"Bone": [0, "Red"],
"Muscle Meat": [100, "Red"]
},
"daily_calories_from_this_meal_plan": "417.96",
"imperial_weight_of_one_meal": "2.2857552",
"meal_ingredients_would_make": 3,
"meals_per_day": 3,
"nutrient_value": [
{
"Calcium": ["29.16", "mg", "Red", "0.00102858984", "ounce"]
},
{
"Choline": ["118.97", "mg", "Red", "0.00419654778", "ounce"]
},
{
"Copper": ["0.12", "mg", "Red", "0.00000423288", "ounce"]
},
{
"Crude fat": ["29.16", "g", "Green", "1.02858984", "ounce"]
},
{
"Folate": ["11.66", "mcg", "Red", "0.00000041129484", "ounce"]
},
{
"Iodine": ["0.0", "mcg", "Red", "0.0", "ounce"]
},
{
"Iron": ["4.08", "mg", "Yellow", "0.00014391792", "ounce"]
},
{
"Magnesium": ["34.99", "mg", "Green", "0.00123423726", "ounce"]
},
{
"Manganese": ["0.02", "mg", "Red", "0.00000070548", "ounce"]
},
{
"Niacin (B3)": ["9.04", "mg", "Green", "0.00031887696", "ounce"]
},
{
"Omega-3 excl. ALA and SDA": [
"0.02",
"g",
"Red",
"0.00070548",
"ounce"
]
},
{
"Omega-6": ["0.02", "g", "Red", "0.00070548", "ounce"]
},
{
"Pantothenic acid (B5)": [
"1.07",
"mg",
"Red",
"0.00003774318",
"ounce"
]
},
{
"Phosphorus": ["332.42", "mg", "Red", "0.01172578308", "ounce"]
},
{
"Potassium": ["573.48", "mg", "Yellow", "0.02022893352", "ounce"]
},
{
"Protein": ["36.94", "g", "Green", "1.30302156", "ounce"]
},
{
"Riboflavin (B2)": ["0.29", "mg", "Red", "0.00001022946", "ounce"]
},
{
"Selenium": ["30.72", "mcg", "Red", "0.00000108361728", "ounce"]
},
{
"Sodium (Na)": ["128.3", "mg", "Green", "0.0045256542", "ounce"]
},
{
"Thiamin (B1)": ["0.08", "mg", "Red", "0.00000282192", "ounce"]
},
{
"Vitamin A": ["8.16", "mcg", "Red", "0.00000028783584", "ounce"]
},
{
"Vitamin C": ["0.0", "mg", "Green", "0.0", "ounce"]
},
{
"Vitamin D": ["0.19", "mcg", "Red", "0.00000000670206", "ounce"]
},
{
"Vitamin E": ["0.33", "mg", "Red", "0.00001164042", "ounce"]
},
{
"Zinc (Zn)": ["8.71", "mg", "Red", "0.00030723654", "ounce"]
},
{
"Calories": ["417.96", "cal", null, "N/A", "ounce"]
},
{
"Omega-3/6 ratio": ["0.65", "ratio", "Green", "N/A", "ounce"]
},
{
"Calcium/Phosphorus ratio": ["0.06", "ratio", "Red", "N/A", "ounce"]
}
],
"pet_name": "tim",
"show_weight_of_balanced_meal": true,
"suggestion": {
"add_food": {
"Red": [
{
"Calcium": ["Amaranth, grain, whole, uncooked", "Apple, dried"]
},
{
"Copper": ["Amaranth, grain, whole, uncooked", "Apple, dried"]
},
{
"Folate": [
"Amaranth, grain, whole, uncooked",
"Apple, fuji, unpeeled, raw"
]
},
{
"Iodine": ["Amaranth, grain, whole, uncooked", "Apricot, dried"]
},
{
"Manganese": ["Amaranth, grain, whole, uncooked", "Apple, dried"]
},
{
"Omega-3 excl. ALA and SDA": ["Bass, fillet, raw", "Beef Brain"]
},
{
"Omega-6": ["Amaranth, grain, whole, uncooked", "Avocado, raw"]
},
{
"Pantothenic acid (B5)": [
"Amaranth, grain, whole, uncooked",
"Apple, dried"
]
},
{
"Riboflavin (B2)": [
"Amaranth, grain, whole, uncooked",
"Apple, fuji, unpeeled, raw"
]
},
{
"Thiamin (B1)": [
"Amaranth, grain, whole, uncooked",
"Apple, fuji, unpeeled, raw"
]
},
{
"Vitamin A": ["Bean, edamame, from frozen, cooked", "Beef Brain"]
},
{
"Vitamin D": ["Beef kidney, raw", "Beef liver, raw"]
},
{
"Vitamin E": ["Amaranth, grain, whole, uncooked", "Apple, dried"]
},
{
"Crude fat": ["Amaranth, grain, whole, uncooked", "Apple, dried"]
},
{
"Magnesium": ["Amaranth, grain, whole, uncooked", "Apple, dried"]
},
{
"Niacin (B3)": [
"Apple, fuji, unpeeled, raw",
"Apple, golden delicious, unpeeled, raw"
]
},
{
"Protein": ["Amaranth, grain, whole, uncooked", "Apple, dried"]
},
{
"Sodium (Na)": [
"Amaranth, grain, whole, uncooked",
"Apple, dried"
]
}
],
"Yellow": []
},
"remove_food": {
"Red": [
{
"Choline": ["Beef, mince, 15% fat, raw"]
},
{
"Phosphorus": ["Beef, mince, 15% fat, raw"]
},
{
"Selenium": ["Beef, mince, 15% fat, raw"]
},
{
"Zinc (Zn)": ["Beef, mince, 15% fat, raw"]
}
],
"Yellow": [
{
"Iron": ["Beef, mince, 15% fat, raw"]
},
{
"Potassium": ["Beef, mince, 15% fat, raw"]
}
]
}
},
"total_calories_in_a_meal_plan": 645,
"total_imperial_weight_of_a_meal_plan": 10.5822,
"total_weight_of_a_meal_plan": 300,
"weight_of_one_meal": "64.8",
"feeding_plan_details": [
{
"id": 23,
"imperial_quantity": 10.5822,
"quantity": 300,
"food": {
"id": 6,
"bone_percentage": 0,
"name": "Beef, mince, 15% fat, raw"
}
}
]
}
};
List<Map<String, dynamic>> nutrientValue = data["result"]["nutrient_value"];
List<Map<String, dynamic>> filteredData = nutrientValue
.where((Map<String, dynamic> element) =>
element.keys.toList()[0] == "Calcium")
.toList();
for (Map<String, dynamic> i in filteredData) {
print("${i[i.keys.toList()[0]][0]} && ${i[i.keys.toList()[0]][2]}");
}
}

Google map fitBounds not working

I am creating a trip view using google map with angularjs directive. All works fine except the google map fitbound to a latitude longitude collection.
I have tried all the way to use the fitbounds methods but failed.
Thanks,
Below is my code efforts.
Directive:
<div class="trip-google-map" trip-map="" mapid="2" latitude="22.998673" longitude="72.514346"></div>
Directive Code:
app.directive('tripMap', function ($compile) {
return {
controller: function ($scope, $location, mapService) {
this.registerMap = function (myMap) {
mapService.setTripMap(myMap);
};
},
link: function (scope, elem, attrs, ctrl) {
var mapOptions,
latitude,
longitude,
mapStyles,
map;
latitude = attrs.latitude;
longitude = attrs.longitude;
mapStyles =
[
{
"featureType": "water",
"elementType": "geometry",
"stylers": [
{
"color": "#a2daf2"
}
]
},
{
"featureType": "landscape.man_made",
"elementType": "geometry",
"stylers": [
{
"color": "#f7f1df"
}
]
},
{
"featureType": "landscape.natural",
"elementType": "geometry",
"stylers": [
{
"color": "#d0e3b4"
}
]
},
{
"featureType": "landscape.natural.terrain",
"elementType": "geometry",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "poi.park",
"elementType": "geometry",
"stylers": [
{
"color": "#bde6ab"
}
]
},
{
"featureType": "poi",
"elementType": "labels",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "poi.medical",
"elementType": "geometry",
"stylers": [
{
"color": "#fbd3da"
}
]
},
{
"featureType": "poi.business",
"elementType": "all",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "road",
"elementType": "geometry.stroke",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "road",
"elementType": "labels",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "road.highway",
"elementType": "geometry.fill",
"stylers": [
{
"color": "#ffe15f"
}
]
},
{
"featureType": "road.highway",
"elementType": "geometry.stroke",
"stylers": [
{
"color": "#efd151"
}
]
},
{
"featureType": "road.arterial",
"elementType": "geometry.fill",
"stylers": [
{
"color": "#ffffff"
}
]
},
{
"featureType": "road.local",
"elementType": "geometry.fill",
"stylers": [
{
"color": "black"
}
]
},
{
"featureType": "transit.station.airport",
"elementType": "geometry.fill",
"stylers": [
{
"color": "#cfb2db"
}
]
}
];
mapOptions = {
zoom: 12,
disableDefaultUI: true,
center: new google.maps.LatLng(latitude, longitude),
mapTypeId: google.maps.MapTypeId.ROADMAP,
styles: mapStyles,
mapTypeControl: true,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
position: google.maps.ControlPosition.BOTTOM_CENTER
},
panControl: true,
panControlOptions: {
position: google.maps.ControlPosition.LEFT_CENTER
},
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.LARGE,
position: google.maps.ControlPosition.LEFT_CENTER
},
scaleControl: true,
streetViewControl: true,
streetViewControlOptions: {
position: google.maps.ControlPosition.LEFT_CENTER
},
};
/*
var mapOptions = {
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false
};
*/
//google.maps.visualRefresh = true;
var map = new google.maps.Map(elem[0], mapOptions);
ctrl.registerMap(map);
scope.InitializeTripdetailController();
scope.$apply(function () {
window.setTimeout(function () {
google.maps.event.trigger(map, 'resize');
}, 100);
});
}
};
});
Controller Code:
app.controller('tripdetailController', ['$scope', '$rootScope', '$timeout', 'mapService', 'ngDialog', function ($scope, $rootScope, $timeout, mapService, ngDialog) {
$scope.tripPathPolylines = [];
var trips = [];
var insertTripLatlng = function (trip) {
var isExists = false;
for (var v in trips) {
if (trips[v].hash == trip.hash) {
isExists = true
break;
}
}
if (isExists == false) {
trips.push(trip);
}
}
function settrip() {
var marker;
var tripmap = mapService.getTripMap();
var bounds = new google.maps.LatLngBounds();
for (var j = 0; j < trips.length; j++) {
var ltlng = new google.maps.LatLng(trips[j].lat, trips[j].lng);
bounds.extend(ltlng);
marker = new google.maps.Marker({
position: ltlng,
map: tripmap
});
}
tripmap.fitBounds(bounds);
}
$scope.InitializeTripdetailController = function () {
var tripData = $scope.$parent.ngDialogData;
for (var p = 0; p < tripData.messages.length; p++) {
insertTripLatlng({ lat: tripData.messages[p].trackPoint.pos.lat, lng: tripData.messages[p].trackPoint.pos.lng, hash: tripData.messages[p].trackPoint.pos.lat + "-" + tripData.messages[p].trackPoint.pos.lng })
}
settrip();
}
}]);
FitBounds Code:
var tripmap = mapService.getTripMap();
var bounds = new google.maps.LatLngBounds();
for (var j = 0; j < trips.length; j++) {
var ltlng = new google.maps.LatLng(trips[j].lat, trips[j].lng);
bounds.extend(ltlng);
marker = new google.maps.Marker({position: ltlng,map: tripmap});
}
tripmap.fitBounds(bounds);
The map looks like:
It should be after fitbounds as:
Update: css for map:
Update: The map is loaded in ngDialog popup.
.trip-google-map {
width: 100%;
height: 450px;
}
It was due to map is resized by ngDialog popup directive.
Solved by call fitbounds in map resize callback with as
$scope.$apply(function () {
window.setTimeout(function () {
google.maps.event.trigger(tripmap, 'resize');
tripmap.fitBounds(bounds);
}, 100);
});
Thanks all for help.

Google Maps Color B&W

Can I use this code in the an iframe for a wordpress page post to make my map black and white? I would like a map just like this: http://wegrowcherries.com/about/
[ {
"featureType": "water",
"stylers": [ {
"color": "#ffffff" }
] },
{
"featureType": "landscape",
"elementType": "geometry",
"stylers": [ {
"color": "#000000" }
] },
{
"featureType": "administrative",
"elementType": "labels.text.fill",
"stylers": [ {
"color": "#ffffff" }
] },
{
"featureType": "administrative",
"elementType": "labels.text.stroke",
"stylers": [ {
"visibility": "simplified" }
] },
{
"featureType": "road",
"elementType": "geometry",
"stylers": [ {
"visibility": "off" }
] },
{
"featureType": "poi",
"elementType": "geometry.fill",
"stylers": [ {
"color": "#000000" }, {
"visibility": "simplified" }
]},
{
"featureType": "administrative.province",
"stylers": [ {
"visibility": "off" }
]},
{
"featureType": "road",
"elementType": "geometry.stroke",
"stylers": [ {
"color": "#ffffff" }, {
"weight": 2 }, {
"visibility": "off" }
]},
{
"featureType": "administrative.locality",
"stylers": [ { "visibility": "off" }
]},
{
"featureType": "road",
"elementType": "labels",
"stylers": [ {
"visibility": "off" }
] },
{
"featureType": "poi",
"stylers": [ {
"visibility": "off" }
] },
{
"featureType": "transit",
"stylers": [ {
"visibility": "off" }
]},
{
"featureType": "administrative.neighborhood",
"stylers": [ {
"visibility": "off" }
] }]
iframe code in wordpress blog. Can I paste the above in here with a style="" tag?
<iframe style="border: 0; width: 100%" src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d4046390.158269976!2d114.49410816221383!3d-7.941674640018978!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x2dd141d3e8100fa1%3A0x24910fb14b24e690!2sBali%2C+Indonesia!5e0!3m2!1sen!2sus!4v1390525274478" height="250" width="900" frameborder="0"></iframe>
The correct (and legal ) way to do this is using the use their Styled Maps Api
It looks like the code above is using that API. As detailed in the Styled Maps API page, you have to create a StyledMapType (like your code above) and pass it to the constructor in your javascript.
Creating a StyledMapType
You can create a new map type to which to apply styles, by creating a
StyledMapType and passing the feature and styler information to the
constructor. This approach does not affect the style of the default
map types.

Resources