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.
Related
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);
I'm trying to use a custom style on my map, but no matter what I do, I can't them them working. My initialize function is:
this.init = (mapSelector, api, title = '', poi = false, prefix = '', cache = true) => {
return new Promise((r, j) => {
if (api.slice(-1) !== '/')
this.setPrefix(prefix);
this.setCache(cache);
this.setPOI(poi);
this.setTitle(title);
try {
getMapKey().then(key => {
if(mapSelector) {
_map = new Microsoft.Maps.Map(mapSelector, {
credentials: key,
center: new Microsoft.Maps.Location(_latitude, _longitude),
zoom: 13,
disableBirdseye: true,
disableStreetside: true,
showDashboard: false,
customMapStyle: JSON.parse(sampleStyle),
});
} else {
j('error');
}
});
} catch (error) {
console.log(error);
}
});
};
It currently renders the standard map just fine. but no matter what I pass into customMapStyle nothing works, JSON.parse was added to make sure the data was in JSON format, because I'm running out of ideas.. I've copied the JSON from the examples on:
https://bingmapsv8samples.azurewebsites.net/#Set%20Custom%20Map%20Style
Some of the styles I've tried to use are:
const darkMode = {
"version": "1.0",
"settings": {
"landColor": "#0B334D"
},
"elements": {
"mapElement": {
"labelColor": "#FFFFFF",
"labelOutlineColor": "#000000"
},
"political": {
"borderStrokeColor": "#144B53",
"borderOutlineColor": "#00000000"
},
"point": {
"iconColor": "#0C4152",
"fillColor": "#000000",
"strokeColor": "#0C4152"
},
"transportation": {
"strokeColor": "#000000",
"fillColor": "#000000"
},
"highway": {
"strokeColor": "#158399",
"fillColor": "#000000"
},
"controlledAccessHighway": {
"strokeColor": "#158399",
"fillColor": "#000000"
},
"arterialRoad": {
"strokeColor": "#157399",
"fillColor": "#000000"
},
"majorRoad": {
"strokeColor": "#157399",
"fillColor": "#000000"
},
"railway": {
"strokeColor": "#146474",
"fillColor": "#000000"
},
"structure": {
"fillColor": "#115166"
},
"water": {
"fillColor": "#021019"
},
"area": {
"fillColor": "#115166"
}
}
};
const sampleStyle = {
"elements": {
"park": { "fillColor": "#A9A9D4BE" },
"controlledAccessHighway": { "fillColor": "#e6c317", "strokeColor": "#D3B300", "labelColor": "#444444", "labelOutlineColor": "#60ffffff" },
"highway": { "fillColor": "#e6c317", "strokeColor": "#D3B300", "labelColor": "#444444", "labelOutlineColor": "#60ffffff" },
"water": { "fillColor": "#B7CDDE" },
"medicalBuilding": { "fillColor": "#fceced" },
"majorRoad": { "fillColor": "#f0d85a" },
"education": { "fillColor": "#f0e8f8" },
"arterialRoad": { "fillColor": "#ffed91" },
"structure": { "fillColor": "#faf8ed" },
"buildinglobal": { "fillColor": "#e5e0d8" },
"forest": { "fillColor": "#deebdd" },
"vegetation": { "fillColor": "#deebdd" },
"reserve": { "fillColor": "#deebdd" },
"street": { "fillColor": "#ffffff", "strokeColor": "#e6e3df" },
"roadShield": { "fillColor": "#ffffff" },
"medical": { "fillColor": "#ffddee" },
"educationBuildinglobal": { "fillColor": "#f6f0f1" },
"golfCourse": { "fillColor": "#c5dabb" }
},
"settings": { "landColor": "#F6F4E3" }
};
Does anyone know how to get a custom style activated? If that's not possible, is there a dark mode that I can activate, which is what I'm trying to do.
Thanks
I would suggest taking a look at the map style sheet editor app - it will allow you to interactively adjust the style and shows sample code for how to initialize that style in all of the Bing maps controls (HTML, UWP and Static maps).
https://www.microsoft.com/en-us/p/map-style-sheet-editor/9nbhtcjt72ft
I had to set setOption, it had nothing to do with the map style I was trying to apply.
I am currently stuck in a situation where I have to add a button to drop down options but can't find any help to add custom button to drop down options using this library
main.js
$scope.example14model = [];
$scope.example14settings = {
scrollableHeight: '200px',
scrollable: true,
enableSearch: true
};
$scope.example14data = [{
"label": "Alabama",
"id": "AL"
}, {
"label": "Alaska",
"id": "AK"
}
];
main.html
<div ng-dropdown-multiselect=""
options="example14data"
selected-model="example14model"
checkboxes="true"
extra-settings="example14settings"></div>
How can I add button to my drop down options? Any help will be highly appreciated.
I think this is what you are looking for :
https://jsfiddle.net/michaeldeongreen/22et6sao/9/
JS
'use strict';
var app = angular.module('myApp', ['angularjs-dropdown-multiselect']);
app.controller('AppCtrl', function ($scope) {
$scope.example14model = [];
$scope.example14settings = {
scrollableHeight: '200px',
scrollable: true,
enableSearch: true
};
$scope.example14data = [{
"label": "Alabama",
"id": "AL"
}, {
"label": "Alaska",
"id": "AK"
}, {
"label": "American Samoa",
"id": "AS"
}, {
"label": "Arizona",
"id": "AZ"
}, {
"label": "Arkansas",
"id": "AR"
}, {
"label": "California",
"id": "CA"
}, {
"label": "Colorado",
"id": "CO"
}, {
"label": "Connecticut",
"id": "CT"
}, {
"label": "Delaware",
"id": "DE"
}, {
"label": "District Of Columbia",
"id": "DC"
}, {
"label": "Federated States Of Micronesia",
"id": "FM"
}, {
"label": "Florida",
"id": "FL"
}, {
"label": "Georgia",
"id": "GA"
}, {
"label": "Guam",
"id": "GU"
}, {
"label": "Hawaii",
"id": "HI"
}, {
"label": "Idaho",
"id": "ID"
}, {
"label": "Illinois",
"id": "IL"
}, {
"label": "Indiana",
"id": "IN"
}, {
"label": "Iowa",
"id": "IA"
}, {
"label": "Kansas",
"id": "KS"
}, {
"label": "Kentucky",
"id": "KY"
}, {
"label": "Louisiana",
"id": "LA"
}, {
"label": "Maine",
"id": "ME"
}, {
"label": "Marshall Islands",
"id": "MH"
}, {
"label": "Maryland",
"id": "MD"
}, {
"label": "Massachusetts",
"id": "MA"
}, {
"label": "Michigan",
"id": "MI"
}, {
"label": "Minnesota",
"id": "MN"
}, {
"label": "Mississippi",
"id": "MS"
}, {
"label": "Missouri",
"id": "MO"
}, {
"label": "Montana",
"id": "MT"
}, {
"label": "Nebraska",
"id": "NE"
}, {
"label": "Nevada",
"id": "NV"
}, {
"label": "New Hampshire",
"id": "NH"
}, {
"label": "New Jersey",
"id": "NJ"
}, {
"label": "New Mexico",
"id": "NM"
}, {
"label": "New York",
"id": "NY"
}, {
"label": "North Carolina",
"id": "NC"
}, {
"label": "North Dakota",
"id": "ND"
}, {
"label": "Northern Mariana Islands",
"id": "MP"
}, {
"label": "Ohio",
"id": "OH"
}, {
"label": "Oklahoma",
"id": "OK"
}, {
"label": "Oregon",
"id": "OR"
}, {
"label": "Palau",
"id": "PW"
}, {
"label": "Pennsylvania",
"id": "PA"
}, {
"label": "Puerto Rico",
"id": "PR"
}, {
"label": "Rhode Island",
"id": "RI"
}, {
"label": "South Carolina",
"id": "SC"
}, {
"label": "South Dakota",
"id": "SD"
}, {
"label": "Tennessee",
"id": "TN"
}, {
"label": "Texas",
"id": "TX"
}, {
"label": "Utah",
"id": "UT"
}, {
"label": "Vermont",
"id": "VT"
}, {
"label": "Virgin Islands",
"id": "VI"
}, {
"label": "Virginia",
"id": "VA"
}, {
"label": "Washington",
"id": "WA"
}, {
"label": "West Virginia",
"id": "WV"
}, {
"label": "Wisconsin",
"id": "WI"
}, {
"label": "Wyoming",
"id": "WY"
}];
$scope.example2settings = {
displayProp: 'id'
};
});
var directiveModule = angular.module('angularjs-dropdown-multiselect', []);
directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$compile', '$parse',
function ($filter, $document, $compile, $parse) {
return {
restrict: 'AE',
scope: {
selectedModel: '=',
options: '=',
extraSettings: '=',
events: '=',
searchFilter: '=?',
translationTexts: '=',
groupBy: '#'
},
template: function (element, attrs) {
var checkboxes = attrs.checkboxes ? true : false;
var groups = attrs.groupBy ? true : false;
var template = '<div class="multiselect-parent btn-group dropdown-multiselect">';
template += '<button type="button" class="dropdown-toggle" ng-class="settings.buttonClasses" ng-click="toggleDropdown()">{{getButtonText()}} <span class="caret"></span></button>';
template += '<ul class="dropdown-menu dropdown-menu-form" ng-style="{display: open ? \'block\' : \'none\', height : settings.scrollable ? settings.scrollableHeight : \'auto\' }" style="overflow: scroll" >';
template += '<li ng-hide="!settings.showCheckAll || settings.selectionLimit > 0"><a data-ng-click="selectAll()"><span class="glyphicon glyphicon-ok"></span> {{texts.checkAll}}</a>';
template += '<li ng-show="settings.showUncheckAll"><a data-ng-click="deselectAll();"><span class="glyphicon glyphicon-remove"></span> {{texts.uncheckAll}}</a></li>';
template += '<li ng-hide="(!settings.showCheckAll || settings.selectionLimit > 0) && !settings.showUncheckAll" class="divider"></li>';
template += '<li ng-show="settings.enableSearch"><div class="dropdown-header"><input type="text" class="form-control" style="width: 100%;" ng-model="searchFilter" placeholder="{{texts.searchPlaceholder}}" /></li>';
template += '<li ng-show="settings.enableSearch" class="divider"></li>';
if (groups) {
template += '<li ng-repeat-start="option in orderedItems | filter: searchFilter" ng-show="getPropertyForObject(option, settings.groupBy) !== getPropertyForObject(orderedItems[$index - 1], settings.groupBy)" role="presentation" class="dropdown-header">{{ getGroupTitle(getPropertyForObject(option, settings.groupBy)) }}</li>';
template += '<li ng-repeat-end role="presentation">';
} else {
template += '<li role="presentation" ng-repeat="option in options | filter: searchFilter">';
}
template += '<a role="menuitem" tabindex="-1" ng-click="setSelectedItem(getPropertyForObject(option,settings.idProp))">';
if (checkboxes) {
template += '<div class="checkbox"><label><input class="checkboxInput" type="checkbox" ng-click="checkboxClick($event, getPropertyForObject(option,settings.idProp))" ng-checked="isChecked(getPropertyForObject(option,settings.idProp))" /> {{getPropertyForObject(option, settings.displayProp)}}</label></div></a>';
} else {
template += '<span data-ng-class="{\'glyphicon glyphicon-ok\': isChecked(getPropertyForObject(option,settings.idProp))}"></span> {{getPropertyForObject(option, settings.displayProp)}}</a>';
}
template += '</li>';
template += '<li class="divider" ng-show="settings.selectionLimit > 1"></li>';
template += '<li role="presentation" ng-show="settings.selectionLimit > 1"><a role="menuitem">{{selectedModel.length}} {{texts.selectionOf}} {{settings.selectionLimit}} {{texts.selectionCount}}</a></li>';
template += '</ul>';
template += '</div>';
element.html(template);
},
link: function ($scope, $element, $attrs) {
var $dropdownTrigger = $element.children()[0];
$scope.toggleDropdown = function () {
$scope.open = !$scope.open;
};
$scope.checkboxClick = function ($event, id) {
$scope.setSelectedItem(id);
$event.stopImmediatePropagation();
};
$scope.externalEvents = {
onItemSelect: angular.noop,
onItemDeselect: angular.noop,
onSelectAll: angular.noop,
onDeselectAll: angular.noop,
onInitDone: angular.noop,
onMaxSelectionReached: angular.noop
};
$scope.settings = {
dynamicTitle: true,
scrollable: false,
scrollableHeight: '300px',
closeOnBlur: true,
displayProp: 'label',
idProp: 'id',
externalIdProp: 'id',
enableSearch: false,
selectionLimit: 0,
showCheckAll: true,
showUncheckAll: true,
closeOnSelect: false,
buttonClasses: 'btn btn-default',
closeOnDeselect: false,
groupBy: $attrs.groupBy || undefined,
groupByTextProvider: null,
smartButtonMaxItems: 0,
smartButtonTextConverter: angular.noop
};
$scope.texts = {
checkAll: 'Check All',
uncheckAll: 'Uncheck All',
selectionCount: 'checked',
selectionOf: '/',
searchPlaceholder: 'Search...',
buttonDefaultText: 'Select',
dynamicButtonTextSuffix: 'checked'
};
$scope.searchFilter = $scope.searchFilter || '';
if (angular.isDefined($scope.settings.groupBy)) {
$scope.$watch('options', function (newValue) {
if (angular.isDefined(newValue)) {
$scope.orderedItems = $filter('orderBy')(newValue, $scope.settings.groupBy);
}
});
}
angular.extend($scope.settings, $scope.extraSettings || []);
angular.extend($scope.externalEvents, $scope.events || []);
angular.extend($scope.texts, $scope.translationTexts);
$scope.singleSelection = $scope.settings.selectionLimit === 1;
function getFindObj(id) {
var findObj = {};
if ($scope.settings.externalIdProp === '') {
findObj[$scope.settings.idProp] = id;
} else {
findObj[$scope.settings.externalIdProp] = id;
}
return findObj;
}
function clearObject(object) {
for (var prop in object) {
delete object[prop];
}
}
if ($scope.singleSelection) {
if (angular.isArray($scope.selectedModel) && $scope.selectedModel.length === 0) {
clearObject($scope.selectedModel);
}
}
if ($scope.settings.closeOnBlur) {
$document.on('click', function (e) {
var target = e.target.parentElement;
var parentFound = false;
while (angular.isDefined(target) && target !== null && !parentFound) {
if (_.contains(target.className.split(' '), 'multiselect-parent') && !parentFound) {
if (target === $dropdownTrigger) {
parentFound = true;
}
}
target = target.parentElement;
}
if (!parentFound) {
$scope.$apply(function () {
$scope.open = false;
});
}
});
}
$scope.getGroupTitle = function (groupValue) {
if ($scope.settings.groupByTextProvider !== null) {
return $scope.settings.groupByTextProvider(groupValue);
}
return groupValue;
};
$scope.getButtonText = function () {
if ($scope.settings.dynamicTitle && ($scope.selectedModel.length > 0 || (angular.isObject($scope.selectedModel) && _.keys($scope.selectedModel).length > 0))) {
if ($scope.settings.smartButtonMaxItems > 0) {
var itemsText = [];
angular.forEach($scope.options, function (optionItem) {
if ($scope.isChecked($scope.getPropertyForObject(optionItem, $scope.settings.idProp))) {
var displayText = $scope.getPropertyForObject(optionItem, $scope.settings.displayProp);
var converterResponse = $scope.settings.smartButtonTextConverter(displayText, optionItem);
itemsText.push(converterResponse ? converterResponse : displayText);
}
});
if ($scope.selectedModel.length > $scope.settings.smartButtonMaxItems) {
itemsText = itemsText.slice(0, $scope.settings.smartButtonMaxItems);
itemsText.push('...');
}
return itemsText.join(', ');
} else {
var totalSelected;
if ($scope.singleSelection) {
totalSelected = ($scope.selectedModel !== null && angular.isDefined($scope.selectedModel[$scope.settings.idProp])) ? 1 : 0;
} else {
totalSelected = angular.isDefined($scope.selectedModel) ? $scope.selectedModel.length : 0;
}
if (totalSelected === 0) {
return $scope.texts.buttonDefaultText;
} else {
return totalSelected + ' ' + $scope.texts.dynamicButtonTextSuffix;
}
}
} else {
return $scope.texts.buttonDefaultText;
}
};
$scope.getPropertyForObject = function (object, property) {
if (angular.isDefined(object) && object.hasOwnProperty(property)) {
return object[property];
}
return '';
};
$scope.selectAll = function () {
$scope.deselectAll(false);
$scope.externalEvents.onSelectAll();
angular.forEach($scope.options, function (value) {
$scope.setSelectedItem(value[$scope.settings.idProp], true);
});
};
$scope.deselectAll = function (sendEvent) {
sendEvent = sendEvent || true;
if (sendEvent) {
$scope.externalEvents.onDeselectAll();
}
if ($scope.singleSelection) {
clearObject($scope.selectedModel);
} else {
$scope.selectedModel.splice(0, $scope.selectedModel.length);
}
};
$scope.setSelectedItem = function (id, dontRemove) {
var findObj = getFindObj(id);
var finalObj = null;
if ($scope.settings.externalIdProp === '') {
finalObj = _.find($scope.options, findObj);
} else {
finalObj = findObj;
}
if ($scope.singleSelection) {
clearObject($scope.selectedModel);
angular.extend($scope.selectedModel, finalObj);
$scope.externalEvents.onItemSelect(finalObj);
if ($scope.settings.closeOnSelect) $scope.open = false;
return;
}
dontRemove = dontRemove || false;
var exists = _.findIndex($scope.selectedModel, findObj) !== -1;
if (!dontRemove && exists) {
$scope.selectedModel.splice(_.findIndex($scope.selectedModel, findObj), 1);
$scope.externalEvents.onItemDeselect(findObj);
} else if (!exists && ($scope.settings.selectionLimit === 0 || $scope.selectedModel.length < $scope.settings.selectionLimit)) {
$scope.selectedModel.push(finalObj);
$scope.externalEvents.onItemSelect(finalObj);
}
if ($scope.settings.closeOnSelect) $scope.open = false;
};
$scope.isChecked = function (id) {
if ($scope.singleSelection) {
return $scope.selectedModel !== null && angular.isDefined($scope.selectedModel[$scope.settings.idProp]) && $scope.selectedModel[$scope.settings.idProp] === getFindObj(id)[$scope.settings.idProp];
}
return _.findIndex($scope.selectedModel, getFindObj(id)) !== -1;
};
$scope.externalEvents.onInitDone();
}
};
}]);
HMTL
<div ng-app="myApp" ng-controller="AppCtrl">
<div ng-dropdown-multiselect="" options="example14data" selected-model="example14model" checkboxes="true" extra-settings="example14settings"></div> <pre>Selected Model: {{example14model}} | json</pre>
After fighting with this for a couple of hours, I think I need to rephrase the question.
I have an object that contains and array of objects;
Playlist: [
{
"added_at": "2015-11-13T20:55:06Z",
"added_by": {
"external_urls": {
"spotify": "http://open.spotify.com/user/spotify_canada"
},
"href": "https://api.spotify.com/v1/users/spotify_canada",
"id": "spotify_canada",
"type": "user",
"uri": "spotify:user:spotify_canada"
},
"is_local": false,
"track": {
"album": {
"album_type": "album",
"available_markets": [
"CA",
"MX",
"US"
],
"external_urls": {
"spotify": "https://open.spotify.com/album/6Fr2rQkZ383FcMqFyT7yPr"
},
"href": "https://api.spotify.com/v1/albums/6Fr2rQkZ383FcMqFyT7yPr",
"id": "6Fr2rQkZ383FcMqFyT7yPr",
"images": [
{
"height": 640,
"url": "https://i.scdn.co/image/8b47495ce0c4a341f7196f70bcf4361e6257c1a0",
"width": 640
},
{
"height": 300,
"url": "https://i.scdn.co/image/da1e8958b6260e832660d0c5f3c80e9569c388c8",
"width": 300
},
{
"height": 64,
"url": "https://i.scdn.co/image/478dbfd0e579dee7392707b9a6848faff0cdfefd",
"width": 64
}
],
"name": "Purpose (Deluxe)",
"type": "album",
"uri": "spotify:album:6Fr2rQkZ383FcMqFyT7yPr"
},
"artists": [
{
"external_urls": {
"spotify": "https://open.spotify.com/artist/1uNFoZAHBGtllmzznpCI3s"
},
"href": "https://api.spotify.com/v1/artists/1uNFoZAHBGtllmzznpCI3s",
"id": "1uNFoZAHBGtllmzznpCI3s",
"name": "Justin Bieber",
"type": "artist",
"uri": "spotify:artist:1uNFoZAHBGtllmzznpCI3s"
}
],
"available_markets": [
"CA",
"MX",
"US"
],
"disc_number": 1,
"duration_ms": 205680,
"explicit": false,
"external_ids": {
"isrc": "USUM71511919"
},
"external_urls": {
"spotify": "https://open.spotify.com/track/4B0JvthVoAAuygILe3n4Bs"
},
"href": "https://api.spotify.com/v1/tracks/4B0JvthVoAAuygILe3n4Bs",
"id": "4B0JvthVoAAuygILe3n4Bs",
"name": "What Do You Mean?",
"popularity": 93,
"preview_url": "https://p.scdn.co/mp3-preview/13da79dc4803f65092d583f6e3bdf1fc4d8344e5",
"track_number": 3,
"type": "track",
"uri": "spotify:track:4B0JvthVoAAuygILe3n4Bs"
}
},
In side of each of these objects is another object I am trying to assign to scope.
So this - playlist {items [ {track {name: songname} } ] }
How do I go about assigning the track.name to scope?
Thanks!
I'm not sure if you just have a typo but why are you assigning your blank array to data.items instead of the other way around?
$scope.playListInfo = [];
$scope.getPlayList = function() {
Spotify.getPlaylistTracks('spotify_canada', '7ndCD5pklOTcrTcv4DErmI')
.then(function(data) {
var i = 0;
for(i; i < data.items.length; i++) {
var dataToKeep = {};
dataToKeep.name = data.items[i].track.name;
dataToKeep.artist = data.items[i].track.artist;
$scope.playListInfo[i] = dataToKeep;
}
});
};
Apparently, I don't have sufficient points to comment. So, I am creating this post.
The JSON data has items and not item. Also, I think you'd have to create a $scope.playlistTrack array outside the function.
$scope.playlistTrack = [];
$scope.getPlayList = function () {
Spotify.getPlaylistTracks('spotify_canada', '7ndCD5pklOTcrTcv4DErmI')
.then(function (datas) {
angular.forEach(datas, function(data){
data.items = $scope.playlistTrack;
});
});
};
Hope this answer helps.
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.