Below code try to draw stations and path on google map, current version stations draw on map correctly but path not show on map!
var tooltip = d3.select("body")
.append("div")
.attr("class", "tooltip_map")
.style('width','auto')
.style('height','auto')
.style('text-align','left')
.style('visibility','hidden')
.style('position','absolute')
.style('padding','15px')
.style('font','20px sans-serif')
.style('background','white')
.attr('pointer-events','none')
// Create the Google Map…
var map = new google.maps.Map(d3.select("#map").node(), {
zoom: 8,
center: new google.maps.LatLng(44.331216, 23.927536),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var jsonobj = load_json()
main(jsonobj)
function main(json) {
var overlay = new google.maps.OverlayView();
overlay.onAdd = function() {
var layer = d3.select(this.getPanes().overlayLayer).append("div")
.attr("class", "stations");
draw_marker(map,overlay,layer,json)
};
// Bind our overlay to the map…
overlay.setMap(map);
}
function draw_marker(map,overlay,layer,json) {
// Draw each marker as a separate SVG element.
// We could use a single SVG, but what size would it have?
overlay.draw = function() {
var projection = this.getProjection(),
padding = 10;
var markerLink = layer.selectAll("link")
.data(json.links)
.each(pathTransform) // update existing markers
.enter().append("line")
.each(pathTransform)
.attr("class", "link");
var marker = layer.selectAll("svg")
.data(json.nodes)
.each(transform) // update existing markers
.enter().append("svg:svg")
.each(transform)
.attr("class", "marker");
// Add a circle.
marker.append("svg:circle")
.attr("r", 5)
.attr("cx", padding)
.attr("cy", padding)
.on("mouseover", function(d){
var desc = "hello"
console.log(desc)
tooltip.html(desc)
return tooltip.style("visibility", "visible");
})
.on("mousemove", function(){
return tooltip.style("top", (event.pageY-10)+"px").style("left",(event.pageX+10)+"px");})
.on("mouseout", function(){
return tooltip.style("visibility", "hidden");
})
// Add a label.
marker.append("svg:text")
.attr("x", padding + 7)
.attr("y", padding)
.attr("dy", ".37em")
.text(function(d) {
return d.id;
});
function pathTransform(d) {
dsrc = new google.maps.LatLng(d.source.lat, d.source.lng);
dtrg = new google.maps.LatLng(d.target.lat, d.target.lng);
dsrc = projection.fromLatLngToDivPixel(dsrc);
dtrg = projection.fromLatLngToDivPixel(dtrg);
return d3.select(this)
.attr("x1", dsrc.x - padding)
.attr("y1", dsrc.y - padding)
.attr("x2", dtrg.x - padding)
.attr("y2", dtrg.y - padding);
}
function transform(d) {
d = new google.maps.LatLng(d.lat, d.lng);
d = projection.fromLatLngToDivPixel(d);
return d3.select(this)
.style("left", (d.x - padding) + "px")
.style("top", (d.y - padding) + "px");
}
}
}
function load_json() {
var jsonobj = {
"directed": true,
"graph": [],
"nodes": [{
"lat": 44.391643516091975,
"lng": 23.159677682342053,
"id": "1:a"
},
{
"lat": 44.315988,
"lng": 23.818359,
"id": "a:a::"
},
{
"lat": 44.29844994776969,
"lng": 24.402314492323608,
"id": "b:b"
},
{
"lat": 44.351118152120485,
"lng": 23.341791630955303,
"id": "a:c"
},
{
"lat": 44.889424527442685,
"lng": 23.960970697645276,
"id": "e:d"
},
{
"lat": 43.46084400349923,
"lng": 23.975774627524885,
"id": "d:6104:1"
},
{
"lat": 44.64680010013528,
"lng": 23.20292820976948,
"id": "c:6104:2"
},
{
"lat": 44.40446080879215,
"lng": 23.953536570796015,
"id": "b:6104:3"
},
{
"lat": 44.18593375168617,
"lng": 23.769879901486856,
"id": "af:6104:4"
},
{
"lat": 44.09051846584001,
"lng": 24.14130778735744,
"id": "aaaa3bab:3d:7305"
},
{
"lat": 44.66376251969314,
"lng": 23.77379490100736,
"id": "aaaa3bab:3d:5507"
},
{
"lat": 44.6240449587762,
"lng": 24.08347249542858,
"id": "aaaa3bab:3d:6f06"
},
{
"lat": 45.00138334367271,
"lng": 24.092331272179138,
"id": "aaaa3bab:3d:1306"
},
{
"lat": 44.55033831045195,
"lng": 24.312914121854526,
"id": "aaaa3bab:3c:ef05"
},
{
"lat": 44.74421652327631,
"lng": 24.728457702115804,
"id": "aaaa3bab:3c:ea03"
},
{
"lat": 43.79401723931746,
"lng": 23.77846416630604,
"id": "aaaa3bab:3d:7200"
},
{
"lat": 43.67351687345779,
"lng": 23.00140978137842,
"id": "aaaa3bab:3d:5d07"
},
{
"lat": 43.87692500855015,
"lng": 24.28543591328852,
"id": "aaaa3bab:3d:550b"
},
{
"lat": 44.28189405244278,
"lng": 23.972410391551893,
"id": "aaaa3bab:3d:2706"
},
{
"lat": 43.94916218711252,
"lng": 23.9733463072956,
"id": "aaaa3bab:3d:2704"
},
{
"lat": 44.61479884874806,
"lng": 24.27581898293906,
"id": "aaaa3bab:3d:2608"
},
{
"lat": 44.92223011339065,
"lng": 23.505887513934034,
"id": "aaaa3bab:3d:6502"
},
{
"lat": 44.20117807597118,
"lng": 23.70555450810448,
"id": "aaaa3bab:3d:2603"
},
{
"lat": 43.547714841247966,
"lng": 24.56985383484244,
"id": "aaaa3bab:3d:2601"
},
{
"lat": 43.92116991202797,
"lng": 22.82805535024416,
"id": "aaaa3bab:3d:5803"
},
{
"lat": 44.56587414638437,
"lng": 22.970799697228976,
"id": "aaaa3bab:3d:7406"
},
{
"lat": 44.10230727065641,
"lng": 23.701204095342597,
"id": "aaaa3bab:3d:7407"
},
{
"lat": 45.25416535851712,
"lng": 24.434312172789625,
"id": "aaaa3bab:3d:7404"
},
{
"lat": 44.91647619491961,
"lng": 23.678252259828515,
"id": "aaaa3bab:3d:7405"
},
{
"lat": 45.03473433359779,
"lng": 24.07596179597473,
"id": "aaaa3bab:3d:7402"
},
{
"lat": 45.16855171992733,
"lng": 23.435986773864467,
"id": "aaaa3bab:3d:7403"
},
{
"lat": 44.553669079256146,
"lng": 23.05123326220677,
"id": "aaaa3bab:3d:7400"
},
{
"lat": 43.32871087231798,
"lng": 23.325707869122013,
"id": "aaaa3bab:3d:5308"
},
{
"lat": 43.40444516345915,
"lng": 23.485798521785892,
"id": "aaaa3bab:3c:f107"
},
{
"lat": 43.9435337313432,
"lng": 22.968285824722354,
"id": "aaaa3bab:3d:7401"
},
{
"lat": 44.74549949495889,
"lng": 22.832034225254052,
"id": "aaaa3bab:3d:7408"
},
{
"lat": 44.34901730307382,
"lng": 24.33506529636527,
"id": "aaaa3bab:3d:7409"
},
{
"lat": 43.53125851464172,
"lng": 24.763229039168245,
"id": "aaaa3bab:3d:6602"
},
{
"lat": 44.155575603194634,
"lng": 23.250881840942217,
"id": "aaaa3bab:3c:e300"
}
],
"links": [{
"source": 1,
"target": 25
},
{
"source": 1,
"target": 26
},
{
"source": 1,
"target": 27
},
{
"source": 1,
"target": 28
},
{
"source": 1,
"target": 29
},
{
"source": 1,
"target": 30
},
{
"source": 1,
"target": 31
},
{
"source": 1,
"target": 34
},
{
"source": 1,
"target": 35
},
{
"source": 1,
"target": 36
},
{
"source": 3,
"target": 5
},
{
"source": 3,
"target": 6
},
{
"source": 4,
"target": 15
},
{
"source": 4,
"target": 9
},
{
"source": 5,
"target": 19
},
{
"source": 5,
"target": 23
},
{
"source": 6,
"target": 18
},
{
"source": 6,
"target": 20
},
{
"source": 7,
"target": 22
},
{
"source": 8,
"target": 37
},
{
"source": 8,
"target": 3
},
{
"source": 10,
"target": 11
},
{
"source": 17,
"target": 21
}, {
"source": 18,
"target": 13
}, {
"source": 18,
"target": 14
}, {
"source": 19,
"target": 33
}, {
"source": 19,
"target": 38
}, {
"source": 23,
"target": 2
}, {
"source": 25,
"target": 10
}, {
"source": 28,
"target": 4
}, {
"source": 28,
"target": 17
}, {
"source": 29,
"target": 32
}, {
"source": 32,
"target": 25
}, {
"source": 34,
"target": 24
}, {
"source": 35,
"target": 8
}, {
"source": 35,
"target": 16
}, {
"source": 37,
"target": 7
}, {
"source": 37,
"target": 12
}
],
"multigraph": false
}
return jsonobj
}
html,
body,
#map {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
.stations,
.stations svg {
position: absolute;
}
.stations link {
position: absolute;
stroke: black;
stroke-width: 2px;
}
.stations svg {
width: 60px;
height: 20px;
padding-right: 100px;
font: 10px sans-serif;
}
.stations circle {
fill: brown;
stroke: black;
stroke-width: 1.5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?key=YOUR_API_KEY""></script>
<body>
<div id="map"></div>
</body>
Add one svg with the same dimension as map, but since google map (0,0) position at center, svg (0,0) at top left, svg should be shift (-width/2,-height/2) to move to top left. each point should shift (width/2,height/2) to match google map coordinate.
Each station add one g element, then append circle and text.
The links field is index of nodes, that index do not have lat and lng property, Add below code to map it to nodes:
.attr("x1", d => {
var source = json.nodes[d.source]
var target = json.nodes[d.target]
d.src = coor2px(projection,source,width,height)
d.trg = coor2px(projection,target,width,height)
return d.src.x
})
.attr("y1", d => d.src.y)
.attr("x2", d => d.trg.x)
.attr("y2", d => d.trg.y)
Change d3.js to v7 then use join to replace the each transform. v5 code not work.
var data = load_json()
main(data)
function main(json) {
/*
var m = d3.select('body').append('div')
.style('width','100%')
.style('height','100%')
.style("margin",0)
.style("padding",0)
*/
var map = new google.maps.Map(d3.select("#map").node(), {
zoom: 9,
center: new google.maps.LatLng(44.391643516091975,23.159677682342053),
mapTypeId: google.maps.MapTypeId.ROADMAP,//TERRAIN
disableDefaultUI:false,
navigationControl:false,
mapTypeControl:false,
scaleControl:false,
});
var overlay = new google.maps.OverlayView();
overlay.onAdd = function() {
var olayer = this.getPanes().overlayLayer
var projection = this.getProjection()
var [width,height] = getMapDimension(map,projection)
console.log(width,height)
var layer = d3.select(olayer).append("svg")
.attr("class", "stations")
.attr('width',width)
.attr('height',height)
.attr('transform',`translate(${-width/2},${-height/2})`)
//.style('border','1px solid red')
overlay.draw = function() {
draw_marker(map,layer,projection,json,width,height)
}
}
// Bind our overlay to the map…
overlay.setMap(map);
//fit_screen(map,json.nodes,'lat','lng')
}
function getMapDimension(map,projection) {
let bounds = map.getBounds();
let ne = bounds.getNorthEast(),
sw = bounds.getSouthWest(),
center = bounds.getCenter();
ne = coor2px(projection,{lat:ne.lat(), lng:ne.lng()},0,0)
sw = coor2px(projection,{lat:sw.lat(), lng:sw.lng()},0,0)
var width = ne.x - sw.x
var height = sw.y - ne.y
if (height == 0) height = width
return [width,height]
}
function fit_screen(map,data,lat,lng) {
var lat_min = d3.min(data,d => d[lat])
var lat_max = d3.max(data,d => d[lat])
var lng_min = d3.min(data,d => d[lng])
var lng_max = d3.max(data,d => d[lng])
map.setCenter(new google.maps.LatLng(
((lat_max + lat_min) / 2.0),
((lng_max + lng_min) / 2.0)
));
map.fitBounds(new google.maps.LatLngBounds(
new google.maps.LatLng(lat_min, lng_min),//bottom left
new google.maps.LatLng(lat_max, lng_max)//top right
));
//map.setZoom(map.getZoom() + 1.8);
}
function draw_marker(map,layer,projection,json,width,height) {
var markerLink = layer.selectAll(".link")
.data(json.links)
.join('line')
.attr("class", "link")
.attr("x1", d => {
var source = json.nodes[d.source]
var target = json.nodes[d.target]
d.src = coor2px(projection,source,width,height)
d.trg = coor2px(projection,target,width,height)
return d.src.x
})
.attr("y1", d => d.src.y)
.attr("x2", d => d.trg.x)
.attr("y2", d => d.trg.y)
.attr("stroke-width",2)
.attr("fill","none")
.attr("stroke","blue")
var marker = layer.selectAll(".marker")
.data(json.nodes)
.join("g")
.attr('class','marker')
.attr('transform',d => {
var p = coor2px(projection,d,width,height)
return `translate(${p.x},${p.y})`
})
marker.append("circle")
.attr("r", 5)
.attr('fill','red')
marker.append("text")
.attr("x", 7)
.attr("y", 0)
.attr("dy", ".37em")
.text(function(d,i) {
return i;
})
.attr('font-size',30)
}
function coor2px(projection,d,width,height) {
var p = new google.maps.LatLng(d.lat, d.lng);
var p = projection.fromLatLngToDivPixel(p);
return {x:p.x+width/2,y:p.y+height/2}
}
function getwidth(){
return window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth
|| 0;
}
function getheight(){
return window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight
|| 0;
}
function load_json() {
var jsonobj = {
"directed": true,
"graph": [],
"nodes": [{
"lat": 44.391643516091975,
"lng": 23.159677682342053,
"id": "1:a"
},
{
"lat": 44.315988,
"lng": 23.818359,
"id": "a:a::"
},
{
"lat": 44.29844994776969,
"lng": 24.402314492323608,
"id": "b:b"
},
{
"lat": 44.351118152120485,
"lng": 23.341791630955303,
"id": "a:c"
},
{
"lat": 44.889424527442685,
"lng": 23.960970697645276,
"id": "e:d"
},
{
"lat": 43.46084400349923,
"lng": 23.975774627524885,
"id": "d:6104:1"
},
{
"lat": 44.64680010013528,
"lng": 23.20292820976948,
"id": "c:6104:2"
},
{
"lat": 44.40446080879215,
"lng": 23.953536570796015,
"id": "b:6104:3"
},
{
"lat": 44.18593375168617,
"lng": 23.769879901486856,
"id": "af:6104:4"
},
{
"lat": 44.09051846584001,
"lng": 24.14130778735744,
"id": "aaaa3bab:3d:7305"
},
{
"lat": 44.66376251969314,
"lng": 23.77379490100736,
"id": "aaaa3bab:3d:5507"
},
{
"lat": 44.6240449587762,
"lng": 24.08347249542858,
"id": "aaaa3bab:3d:6f06"
},
{
"lat": 45.00138334367271,
"lng": 24.092331272179138,
"id": "aaaa3bab:3d:1306"
},
{
"lat": 44.55033831045195,
"lng": 24.312914121854526,
"id": "aaaa3bab:3c:ef05"
},
{
"lat": 44.74421652327631,
"lng": 24.728457702115804,
"id": "aaaa3bab:3c:ea03"
},
{
"lat": 43.79401723931746,
"lng": 23.77846416630604,
"id": "aaaa3bab:3d:7200"
},
{
"lat": 43.67351687345779,
"lng": 23.00140978137842,
"id": "aaaa3bab:3d:5d07"
},
{
"lat": 43.87692500855015,
"lng": 24.28543591328852,
"id": "aaaa3bab:3d:550b"
},
{
"lat": 44.28189405244278,
"lng": 23.972410391551893,
"id": "aaaa3bab:3d:2706"
},
{
"lat": 43.94916218711252,
"lng": 23.9733463072956,
"id": "aaaa3bab:3d:2704"
},
{
"lat": 44.61479884874806,
"lng": 24.27581898293906,
"id": "aaaa3bab:3d:2608"
},
{
"lat": 44.92223011339065,
"lng": 23.505887513934034,
"id": "aaaa3bab:3d:6502"
},
{
"lat": 44.20117807597118,
"lng": 23.70555450810448,
"id": "aaaa3bab:3d:2603"
},
{
"lat": 43.547714841247966,
"lng": 24.56985383484244,
"id": "aaaa3bab:3d:2601"
},
{
"lat": 43.92116991202797,
"lng": 22.82805535024416,
"id": "aaaa3bab:3d:5803"
},
{
"lat": 44.56587414638437,
"lng": 22.970799697228976,
"id": "aaaa3bab:3d:7406"
},
{
"lat": 44.10230727065641,
"lng": 23.701204095342597,
"id": "aaaa3bab:3d:7407"
},
{
"lat": 45.25416535851712,
"lng": 24.434312172789625,
"id": "aaaa3bab:3d:7404"
},
{
"lat": 44.91647619491961,
"lng": 23.678252259828515,
"id": "aaaa3bab:3d:7405"
},
{
"lat": 45.03473433359779,
"lng": 24.07596179597473,
"id": "aaaa3bab:3d:7402"
},
{
"lat": 45.16855171992733,
"lng": 23.435986773864467,
"id": "aaaa3bab:3d:7403"
},
{
"lat": 44.553669079256146,
"lng": 23.05123326220677,
"id": "aaaa3bab:3d:7400"
},
{
"lat": 43.32871087231798,
"lng": 23.325707869122013,
"id": "aaaa3bab:3d:5308"
},
{
"lat": 43.40444516345915,
"lng": 23.485798521785892,
"id": "aaaa3bab:3c:f107"
},
{
"lat": 43.9435337313432,
"lng": 22.968285824722354,
"id": "aaaa3bab:3d:7401"
},
{
"lat": 44.74549949495889,
"lng": 22.832034225254052,
"id": "aaaa3bab:3d:7408"
},
{
"lat": 44.34901730307382,
"lng": 24.33506529636527,
"id": "aaaa3bab:3d:7409"
},
{
"lat": 43.53125851464172,
"lng": 24.763229039168245,
"id": "aaaa3bab:3d:6602"
},
{
"lat": 44.155575603194634,
"lng": 23.250881840942217,
"id": "aaaa3bab:3c:e300"
}
],
"links": [{
"source": 1,
"target": 25
},
{
"source": 1,
"target": 26
},
{
"source": 1,
"target": 27
},
{
"source": 1,
"target": 28
},
{
"source": 1,
"target": 29
},
{
"source": 1,
"target": 30
},
{
"source": 1,
"target": 31
},
{
"source": 1,
"target": 34
},
{
"source": 1,
"target": 35
},
{
"source": 1,
"target": 36
},
{
"source": 3,
"target": 5
},
{
"source": 3,
"target": 6
},
{
"source": 4,
"target": 15
},
{
"source": 4,
"target": 9
},
{
"source": 5,
"target": 19
},
{
"source": 5,
"target": 23
},
{
"source": 6,
"target": 18
},
{
"source": 6,
"target": 20
},
{
"source": 7,
"target": 22
},
{
"source": 8,
"target": 37
},
{
"source": 8,
"target": 3
},
{
"source": 10,
"target": 11
},
{
"source": 17,
"target": 21
}, {
"source": 18,
"target": 13
}, {
"source": 18,
"target": 14
}, {
"source": 19,
"target": 33
}, {
"source": 19,
"target": 38
}, {
"source": 23,
"target": 2
}, {
"source": 25,
"target": 10
}, {
"source": 28,
"target": 4
}, {
"source": 28,
"target": 17
}, {
"source": 29,
"target": 32
}, {
"source": 32,
"target": 25
}, {
"source": 34,
"target": 24
}, {
"source": 35,
"target": 8
}, {
"source": 35,
"target": 16
}, {
"source": 37,
"target": 7
}, {
"source": 37,
"target": 12
}
],
"multigraph": false
}
return jsonobj
}
html,
body,
#map {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
<script src="https://unpkg.com/d3#7.0.4/dist/d3.min.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?&libraries=geometry"></script>
<body>
<div id="map"></div>
</body>
Related
[
[{
"dimensions": {
"source": "linkedin.com",
"pagePath": "/",
"date": 20170906
},
"metrics": {
"visits": 1
}
}, {
"dimensions": {
"source": "linkedin.com",
"pagePath": "/",
"date": 20170921
},
"metrics": {
"visits": 2
}
}, {
"dimensions": {
"source": "linkedin.com",
"pagePath": "/",
"date": 20170929
},
" metrics": {
"visits": 1
}
}, {
"dimensions": {
"source": "linkedin.com",
"pagePath": "/",
"date": 20171004
},
"metrics": {
"visits": 1
}
}, {
"dimensions": {
"source": "linkedin.com",
"pagePath": "/",
"date": 20171010
},
"metrics": {
"visits": 1
}
}, {
"dimensions": {
"source": "linkedin.com",
"pagePath": " /",
"date": 20171018
},
"metrics": {
"visits": 1
}
}, {
"dimensions": {
"source": "linkedin.com",
"pagePath": "/",
"date": 20171020
},
"metrics": {
"visits": 1
}
}, {
"dimensions": {
"source": "linkedin.com",
"pagePath": "/",
"date": 20171030
},
"metrics": {
"visits": 1
}
}, {
"dimensions": {
"source": "linke din.com",
"pagePath": "/",
"date": 20171112
},
"metrics": {
"visits": 1
}
}, {
"dimensions": {
"source": "linkedin.com",
"pagePath": "/",
"date": 20171123
},
"metrics": {
"visits": 1
}
}, {
"dimensions": {
"source": "linkedin.com",
"pagePath": "/",
"date": 20180106
},
"metrics": {
"visits": 1
}
}, {
"dimensi ons": {
"source": "linkedin.com",
"pagePath": "/",
"date": 20180119
},
"metrics": {
"visits": 1
}
}, {
"dimensions": {
"source": "linkedin.com",
"pagePath": "/",
"date": 20180120
},
"metrics": {
"visits": 1
}
}, {
"dimensions": {
"source": "linkedin.com",
"pagePath": "/",
"date": 20180124
},
"metrics": {
" visits": 1
}
}, {
"dimensions": {
"source": "linkedin.com",
"pagePath": "/",
"date": 20180126
},
"metrics": {
"visits": 1
}
}, {
"dimensions": {
"source": "linkedin.com",
"pagePath": "/",
"date": 20180209
},
"metrics": {
"visits": 1
}
}, {
"dimensions": {
"source": "linkedin.com",
"pagePath": "/",
"date": 20180212
},
"metrics": {
"visits": 1
}
}, {
"dimensions": {
"source": "linkedin.com",
"pagePath": "/",
"date": 20180216
},
"metrics": {
"visits": 1
}
}, {
"dimensions": {
"source": "linkedin.com",
"pagePath": "/",
"date": 20180217
},
"metrics": {
"visits": 1
}
}, {
"dimensions": {
"source": "linkedin.com",
"p agePath": "/",
"date": 20180223
},
"metrics": {
"visits": 1
}
}, {
"dimensions": {
"source": "linkedin.com",
"pagePath": "/",
"date": 20180308
},
"metrics": {
"visits": 1
}
}, {
"dimensions": {
"source": "lnkd.in",
"pagePath": "/",
"date": 20171007
},
"metrics": {
"visits": 1
}
}, {
"dimensions": {
"source": " lnkd.in",
"pagePath": "/",
"date": 20171120
},
"metrics": {
"visits": 1
}
}]
]
i get 10k+ lines of json like this, from multiple sources and pages as you can see in the made-up example above how it looks like.
the result i would like to have is like this :
{
"dimensions": {
"source": "linkedin.com",
"pagePath": "/"
},
"metrics": [{
"visits": 1,
"date": 20171205
}, {
"visits": 10,
"date": 20171205
}, {
"visits": 8,
"date": 20171205
}, {
"visits": 6,
"date": 20171205
}, {
"visits": 5,
"date": 20171205
}, {
"visits": 3,
"date": 20171205
}]
}
what is the best way to achieve this ? i looked at a lot of examples but somehow i still dont get it. my problem is that when i use something like JSON.Parse() to convert 1 object. i get multiple objects with 1 array of 1 metric(visit+date) instead of 1 object with 1 array of multiple metrics(visit+date)
You can use Array.filter method
var result = jsonObject.filter(item => {
return item.dimensions.source == 'linkedin.com' //any condtion you can give
})
Like, the other poster suggested (I've tweaked the answer a little bit), You can try the Array.filter like this,
var filtered_result = jsonObj.filter(function (item, index) {
return item[index].dimensions.source == 'linkedin.com'
});
Hope this helps!
I am fetching my data from Google Directions and because the way the JSON response was set up, like this:
Object {
"geocoded_waypoints": Array [
Object {
"geocoder_status": "OK",
"place_id": "ChIJ2YDFT9zweUgRgt5Tws6n3hs",
"types": Array [
"premise",
],
},
Object {
"geocoder_status": "OK",
"place_id": "ChIJW2PCihXxeUgRRWgA9kOzpjY",
"types": Array [
"establishment",
"food",
"lodging",
"point_of_interest",
"restaurant",
],
},
],
"routes": Array [
Object {
"bounds": Object {
"northeast": Object {
"lat": 52.9242042,
"lng": -1.4751044,
},
"southwest": Object {
"lat": 52.900484,
"lng": -1.4801554,
},
},
"copyrights": "Map data ©2018 Google",
"legs": Array [
Object {
"distance": Object {
"text": "3.1 km",
"value": 3137,
},
"duration": Object {
"text": "38 mins",
"value": 2309,
},
"end_address": "Full St, Derby DE1 3AF, UK",
"end_location": Object {
"lat": 52.9242042,
"lng": -1.4756769,
},
"start_address": "50 Chatham St, Derby DE23 8TH, UK",
"start_location": Object {
"lat": 52.900484,
"lng": -1.4801443,
},
"steps": Array [
Object {
"distance": Object {
"text": "3.1 km",
"value": 3137,
},
"duration": Object {
"text": "38 mins",
"value": 2309,
},
"end_location": Object {
"lat": 52.9242042,
"lng": -1.4756769,
},
"html_instructions": "Walk to Full St, Derby DE1 3AF, UK",
"polyline": Object {
"points": "_c{aIza`Hm#BEyHC_AyAk#iCw#KCGAGAMBSFE#iBz#C#E#QDe#TMFUHWJm#TIm#k#_Em#yDuE`BIDw#SEPCJGPINILEDGFWTcBbAwB~#WJ]PIDOHWNMJ?##??#?#?#A??#?#?#A??#?#A??#A?A??#AAA?A??AA??A?AA??AI#EBGBOHo#XGBIDKBGBG#G#G#OBu#JG#KBID?A?AA??AA??AA?A?A??#A??#A??#?#?#]UGG}AiAcAu#g#a#kAw#c#Wu#_#c#U}#e#y#g#WQKGGEEAWGc#Iq#EW?k#?M#g#FoBPq#J[HSDIBGB]Ja#RcAdAYX{AdAq#Ry#Hu#BU?yA#g#AYAAIAG?CAECGACCGEEEECCEAICK?C#A#A?C#C#EDCBABCDABABCDGYgBkBwBsBw#y#GCGAYe#cAqACCAECE?C?G#Kc#NC#gBj#MDKDaBdAq#^GHCFCFCNCFEHEFwCZi##MAMCIIMOSa#Yq#Oc#Qk#CEKc#EQWuAG?ICCIAKAKKFIDIFE#OPONMLa#h#?#",
},
"start_location": Object {
"lat": 52.900484,
"lng": -1.4801443,
},
"steps": Array [
Object {
"distance": Object {
"text": "26 m",
"value": 26,
},
"duration": Object {
"text": "1 min",
"value": 17,
},
"end_location": Object {
"lat": 52.9007148,
"lng": -1.4801554,
},
"html_instructions": "Head <b>north</b> on <b>Chatham St</b> toward <b>Randolph Rd</b>",
"polyline": Object {
"points": "_c{aIza`Hm#B",
},
"start_location": Object {
"lat": 52.900484,
"lng": -1.4801443,
},
"travel_mode": "WALKING",
},
Object {
"distance": Object {
"text": "0.1 km",
"value": 126,
},
"duration": Object {
"text": "1 min",
"value": 88,
},
"end_location": Object {
"lat": 52.9007561,
"lng": -1.4782714,
},
"html_instructions": "Turn <b>right</b> onto <b>Randolph Rd</b>",
"maneuver": "turn-right",
"polyline": Object {
"points": "md{aI~a`HEyHC_A",
},
"start_location": Object {
"lat": 52.9007148,
"lng": -1.4801554,
},
"travel_mode": "WALKING",
},
Object {
"distance": Object {
"text": "0.3 km",
"value": 332,
},
"duration": Object {
"text": "4 mins",
"value": 223,
},
"end_location": Object {
"lat": 52.90362829999999,
"lng": -1.4785171,
},
"html_instructions": "Turn <b>left</b> onto <b>St Thomas Rd</b>",
"maneuver": "turn-left",
"polyline": Object {
"points": "wd{aIdv_HyAk#iCw#KCGAGAMBSFE#iBz#C#E#QDe#TMFUHWJm#T",
},
"start_location": Object {
"lat": 52.9007561,
"lng": -1.4782714,
},
"travel_mode": "WALKING",
},
Object {
"distance": Object {
"text": "0.2 km",
"value": 153,
},
"duration": Object {
"text": "2 mins",
"value": 104,
},
"end_location": Object {
"lat": 52.90412999999999,
"lng": -1.4763973,
},
"html_instructions": "Turn <b>right</b> to stay on <b>St Thomas Rd</b>",
"maneuver": "turn-right",
"polyline": Object {
"points": "uv{aIvw_HIm#k#_Em#yD",
},
"start_location": Object {
"lat": 52.90362829999999,
"lng": -1.4785171,
},
"travel_mode": "WALKING",
},
Object {
"distance": Object {
"text": "0.1 km",
"value": 129,
},
"duration": Object {
"text": "2 mins",
"value": 103,
},
"end_location": Object {
"lat": 52.9052488,
"lng": -1.4769249,
},
"html_instructions": "Turn <b>left</b> onto <b>Portland St</b>",
"maneuver": "turn-left",
"polyline": Object {
"points": "yy{aInj_HuE`BID",
},
"start_location": Object {
"lat": 52.90412999999999,
"lng": -1.4763973,
},
"travel_mode": "WALKING",
},
Object {
"distance": Object {
"text": "32 m",
"value": 32,
},
"duration": Object {
"text": "1 min",
"value": 24,
},
"end_location": Object {
"lat": 52.9055291,
"lng": -1.4768217,
},
"html_instructions": "Slight <b>right</b> at <b>Goodale St</b>",
"maneuver": "turn-slight-right",
"polyline": Object {
"points": "y`|aIvm_Hw#S",
},
"start_location": Object {
"lat": 52.9052488,
"lng": -1.4769249,
},
"travel_mode": "WALKING",
},
Object {
"distance": Object {
"text": "0.4 km",
"value": 418,
},
"duration": Object {
"text": "5 mins",
"value": 325,
},
"end_location": Object {
"lat": 52.9089381,
"lng": -1.4789711,
},
"html_instructions": "Turn <b>left</b> onto <b>Pear Tree Rd</b><div style=\"font-size:0.9em\">Go through 1 roundabout</div>",
"maneuver": "turn-left",
"polyline": Object {
"points": "qb|aIbm_HEPCJGPINILEDGFWTcBbAwB~#WJ]PIDOHWNMJ?##??#?#?#A??#?#?#A??#?#A??#A?A??#AAA?A??AA??A?AA??AI#EBGBOHo#XGBIDKBGBG#G#G#OBu#JG#KBID",
},
"start_location": Object {
"lat": 52.9055291,
"lng": -1.4768217,
},
"travel_mode": "WALKING",
},
Object {
"distance": Object {
"text": "1.0 km",
"value": 984,
},
"duration": Object {
"text": "13 mins",
"value": 760,
},
"end_location": Object {
"lat": 52.9173185,
"lng": -1.4781371,
},
"html_instructions": "At the roundabout, take the <b>1st</b> exit onto <b>Normanton Rd</b>",
"maneuver": "roundabout-left",
"polyline": Object {
"points": "{w|aIpz_H?A?AA??AA??AA?A?A??#A??#A??#?#?#]UGG}AiAcAu#g#a#kAw#c#Wu#_#c#U}#e#y#g#WQKGGEEAWGc#Iq#EW?k#?M#g#FoBPq#J[HSDIBGB]Ja#RcAdAYX{AdAq#Ry#Hu#BU?yA#g#AYA",
},
"start_location": Object {
"lat": 52.9089381,
"lng": -1.4789711,
},
"travel_mode": "WALKING",
},
Object {
"distance": Object {
"text": "0.3 km",
"value": 272,
},
"duration": Object {
"text": "3 mins",
"value": 207,
},
"end_location": Object {
"lat": 52.9193378,
"lng": -1.4764257,
},
"html_instructions": "At the roundabout, take the <b>3rd</b> exit onto <b>Babington Ln</b>",
"maneuver": "roundabout-left",
"polyline": Object {
"points": "gl~aIju_HAIAG?CAECGACCGEEEECCEAICK?C#A#A?C#C#EDCBABCDABABCDGYgBkBwBsBw#y#GCGA",
},
"start_location": Object {
"lat": 52.9173185,
"lng": -1.4781371,
},
"travel_mode": "WALKING",
},
Object {
"distance": Object {
"text": "82 m",
"value": 82,
},
"duration": Object {
"text": "1 min",
"value": 54,
},
"end_location": Object {
"lat": 52.91985400000001,
"lng": -1.4756332,
},
"html_instructions": "Slight <b>right</b> to stay on <b>Babington Ln</b>",
"maneuver": "turn-slight-right",
"polyline": Object {
"points": "{x~aItj_HYe#cAqACCAECE?C?G#K",
},
"start_location": Object {
"lat": 52.9193378,
"lng": -1.4764257,
},
"travel_mode": "WALKING",
},
Object {
"distance": Object {
"text": "0.2 km",
"value": 217,
},
"duration": Object {
"text": "2 mins",
"value": 144,
},
"end_location": Object {
"lat": 52.9216227,
"lng": -1.4768472,
},
"html_instructions": "Turn <b>left</b> onto <b>St Peters St</b>",
"maneuver": "turn-left",
"polyline": Object {
"points": "a|~aIte_Hc#NC#gBj#MDKDaBdAq#^GHCFCFCNCFEHEF",
},
"start_location": Object {
"lat": 52.91985400000001,
"lng": -1.4756332,
},
"travel_mode": "WALKING",
},
Object {
"distance": Object {
"text": "0.3 km",
"value": 266,
},
"duration": Object {
"text": "3 mins",
"value": 186,
},
"end_location": Object {
"lat": 52.9234756,
"lng": -1.4752851,
},
"html_instructions": "Slight <b>right</b> onto <b>Corn Market</b>",
"maneuver": "turn-slight-right",
"polyline": Object {
"points": "cg_bIhm_HwCZi##MAMCIIMOSa#Yq#Oc#Qk#CEKc#EQWuA",
},
"start_location": Object {
"lat": 52.9216227,
"lng": -1.4768472,
},
"travel_mode": "WALKING",
},
Object {
"distance": Object {
"text": "0.1 km",
"value": 100,
},
"duration": Object {
"text": "1 min",
"value": 74,
},
"end_location": Object {
"lat": 52.9242042,
"lng": -1.4756769,
},
"html_instructions": "At the roundabout, take the <b>2nd</b> exit onto <b>Full St</b><div style=\"font-size:0.9em\">Destination will be on the right</div>",
"maneuver": "roundabout-left",
"polyline": Object {
"points": "wr_bIpc_HG?ICCIAKAKKFIDIFE#OPONMLa#h#?#",
},
"start_location": Object {
"lat": 52.9234756,
"lng": -1.4752851,
},
"travel_mode": "WALKING",
},
],
"travel_mode": "WALKING",
},
],
"traffic_speed_entry": Array [],
"via_waypoint": Array [],
},
],
"overview_polyline": Object {
"points": "_c{aIza`Hm#BEyHC_AyAk#uC{#OCa#JoB|#IBw#ZiBr#u#mFm#yDuE`BIDw#SI\\Q`#OR_#\\cBbAwB~#u#\\YNe#\\#??BA#?DCBE#EEAAg#RmAf#yB^IBAAAACACBA#?#e#[uGyE{DsBeBgA]IuAOcA?u#HoBPq#Jo#NQF]Ja#R}A~A{AdAq#RoBLoB#aACCQGUSWOEO#KDORCFCDGY_F_F_A}#GAYe#gAuAEW#Kc#NkBl#YJsCdBKPGVIPEFwCZw#?WMa#q#i#uAg#gBWuAG?ICCICWULOH_#`#o#x#",
},
"summary": "Normanton Rd",
"warnings": Array [
"Walking directions are in beta. Use caution – This route may be missing sidewalks or pedestrian paths.",
],
"waypoint_order": Array [],
},
],
"status": "OK",
}
I am trying to get data from transit details which has the latitudes and longitude values which i am trying to acquire to be able to view this on in the map using MapView in React native and i believe i am successful of on doing this as this is my attempt on fetching the data setting the state:
fetch('https://maps.googleapis.com/maps/api/directions/json?origin=' + latitude + ',' + longitude + '&destination=' + goingto + '&mode=transit&transit_mode=bus&key=' + apiDirectionskey)
.then((resdirections) => resdirections.json())
.then((responseJson4) => {
//const departureloc1 = responseJson4.routes[0].legs[0].steps[1];
//console.log(departureloc1)
const departureloc = responseJson4.routes[0].legs[0].steps[1].transit_details.departure_stop.location;
console.log(departureloc);
const stopmarkers = Object.keys(departureloc).map(value => {
// noinspection JSAnnotator
if (typeof departureloc['lat'] === undefined && typeof departureloc['lng'] === undefined) {
console.log('null');
} else {
return {
latlng: {
latitude: departureloc['lat'],
longitude: departureloc['lng']
}
}
}
this.setState({ departureloc: stopmarkers });
});
})
}
printing the fetch call above will result in this JSON result
Object {
"lat": 52.90736,
"lng": -1.453532,
}
The next section is my render function below which has a mapview in my attempt to set the markers.
<View style={styles.container}>
<Mainlogo/>
<TextInput style={styles.boxInput} underlineColorAndroid='rgba(0,0,0,0)' placeholder="Going To?"
underlineColorAndroid='transparent'
onChangeText={(dest) => this.setState({goingto : dest})}
/>
<TouchableOpacity style={styles.button} onPress={this.fetchDirections.bind(this)}>
<Text style={styles.textButton}> Go {this.props.type}</Text>
</TouchableOpacity>
{this.state.departureloc.map(marker => (
<MapView.Marker
coordinate={marker.latlng}
image={require('../img/busstop1.png')}
/>
))}
</MapView> : <Text style={{fontWeight: 'bold', color: '#546e7a', textAlign: 'center'}}>Please wait.. loading</Text>}
</View>
I am unsuccessful at this stage and instead i cannot get around with this error.
I am very new to this and anyone that can guide and give me pointers on the correct way would be greatly appreciated thanks
At render time, this.state.departureloc is unset, likely because render() gets called before your API calls return. Try setting this.state.departureloc = [] in your constructor(props) function.
Also it's not good to iterate within your JSX rendering. Consider extracting that rendering to before your return statement and storing it in a variable.
Looking at this example from https://codepen.io/anon/pen/WzdOqz
Is there a way so that I can control the maximum length of the red line (path)?
For example I would only like to keep 20 points of the red line shown everytime (meaning while the marker moves, the red line shrinks starting from the beginning)
var map;
function initialize() {
var markLAT = coords[50].lat;
var markLNG = coords[50].lng;
map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(markLAT, markLNG),
zoom: 11,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
autoRefresh();
}
google.maps.event.addDomListener(window, 'load', initialize);
var icon = new google.maps.MarkerImage("https://maps.google.com/mapfiles/ms/micons/blue.png");
function moveMarker(map, marker, lat, lon) {
marker.setPosition(new google.maps.LatLng(lat, lon));
}
function autoRefresh() {
var i, route, marker;
route = new google.maps.Polyline({
path: [],
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2,
editable: false,
map: map
});
marker = new google.maps.Marker({
map: map,
icon: icon
});
for (i = 0; i < coords.length; i++) {
setTimeout(function(coords) {
route.getPath().push(new google.maps.LatLng(coords.lat, coords.lng));
moveMarker(map, marker, coords.lat, coords.lng);
}, 20 * i, coords[i]);
}
}
var coords = [{
"lat": 8.893260000000001,
"lng": 76.61427
},
{
"lat": 8.894430000000002,
"lng": 76.61418
},
{
"lat": 8.89484,
"lng": 76.61416000000001
},
{
"lat": 8.894860000000001,
"lng": 76.61383000000001
},
{
"lat": 8.89488,
"lng": 76.61375000000001
},
{
"lat": 8.89563,
"lng": 76.61370000000001
},
{
"lat": 8.896270000000001,
"lng": 76.61366000000001
},
{
"lat": 8.896700000000001,
"lng": 76.61364
},
{
"lat": 8.89671,
"lng": 76.61351
},
{
"lat": 8.896740000000001,
"lng": 76.61312000000001
},
{
"lat": 8.89675,
"lng": 76.61307000000001
},
{
"lat": 8.89677,
"lng": 76.61305
},
{
"lat": 8.89729,
"lng": 76.61303000000001
},
{
"lat": 8.89755,
"lng": 76.61311
},
{
"lat": 8.898050000000001,
"lng": 76.61311
},
{
"lat": 8.898280000000002,
"lng": 76.61321000000001
},
{
"lat": 8.899180000000001,
"lng": 76.61289000000001
},
{
"lat": 8.900350000000001,
"lng": 76.61396
},
{
"lat": 8.900860000000002,
"lng": 76.61467
},
{
"lat": 8.90127,
"lng": 76.61581000000001
},
{
"lat": 8.901710000000001,
"lng": 76.61796000000001
},
{
"lat": 8.902190000000001,
"lng": 76.61932
},
{
"lat": 8.902370000000001,
"lng": 76.62017
},
{
"lat": 8.902370000000001,
"lng": 76.62088
},
{
"lat": 8.902460000000001,
"lng": 76.6212
},
{
"lat": 8.90313,
"lng": 76.62217000000001
},
{
"lat": 8.903450000000001,
"lng": 76.62240000000001
},
{
"lat": 8.903970000000001,
"lng": 76.62272
},
{
"lat": 8.90409,
"lng": 76.62280000000001
},
{
"lat": 8.904,
"lng": 76.62288000000001
},
{
"lat": 8.90342,
"lng": 76.6233
},
{
"lat": 8.902560000000001,
"lng": 76.62386000000001
},
{
"lat": 8.90033,
"lng": 76.62522000000001
},
{
"lat": 8.89601,
"lng": 76.62777000000001
},
{
"lat": 8.88676,
"lng": 76.63327000000001
},
{
"lat": 8.884450000000001,
"lng": 76.63461000000001
},
{
"lat": 8.882610000000001,
"lng": 76.63582000000001
},
{
"lat": 8.88089,
"lng": 76.63711
},
{
"lat": 8.87918,
"lng": 76.63862
},
{
"lat": 8.87785,
"lng": 76.63936000000001
},
{
"lat": 8.875760000000001,
"lng": 76.63996
},
{
"lat": 8.87273,
"lng": 76.64141000000001
},
{
"lat": 8.87067,
"lng": 76.64251
},
{
"lat": 8.869280000000002,
"lng": 76.64336
},
{
"lat": 8.86805,
"lng": 76.6447
},
{
"lat": 8.86782,
"lng": 76.6451
},
{
"lat": 8.86677,
"lng": 76.64822000000001
},
{
"lat": 8.86645,
"lng": 76.64933
},
{
"lat": 8.866200000000001,
"lng": 76.65092
},
{
"lat": 8.86546,
"lng": 76.6533
},
{
"lat": 8.86508,
"lng": 76.65451
},
{
"lat": 8.86495,
"lng": 76.65667
},
{
"lat": 8.864880000000001,
"lng": 76.65962
},
{
"lat": 8.86519,
"lng": 76.66080000000001
},
{
"lat": 8.866240000000001,
"lng": 76.66343
},
{
"lat": 8.86646,
"lng": 76.66454
},
{
"lat": 8.866200000000001,
"lng": 76.66933
},
{
"lat": 8.86569,
"lng": 76.67323
},
{
"lat": 8.86522,
"lng": 76.67823
},
{
"lat": 8.863840000000001,
"lng": 76.68872
},
{
"lat": 8.86359,
"lng": 76.6907
},
{
"lat": 8.86364,
"lng": 76.69282000000001
},
{
"lat": 8.86317,
"lng": 76.69574
},
{
"lat": 8.863420000000001,
"lng": 76.69850000000001
},
{
"lat": 8.8634,
"lng": 76.69958000000001
},
{
"lat": 8.863050000000001,
"lng": 76.70048000000001
},
{
"lat": 8.862350000000001,
"lng": 76.70149
},
{
"lat": 8.862020000000001,
"lng": 76.70239000000001
},
{
"lat": 8.86176,
"lng": 76.70448
},
{
"lat": 8.86218,
"lng": 76.70703
},
{
"lat": 8.863180000000002,
"lng": 76.70957
},
{
"lat": 8.8636,
"lng": 76.71115
},
{
"lat": 8.86382,
"lng": 76.71257
},
{
"lat": 8.86383,
"lng": 76.71368000000001
},
{
"lat": 8.86354,
"lng": 76.71509
},
{
"lat": 8.863240000000001,
"lng": 76.71595
},
{
"lat": 8.86259,
"lng": 76.71731000000001
}
];
#map {
width: 99, 9%;
position: relative;
height: 500px;
border: 1px solid black;
margin-top: 30px;
}
<div class="container">
<div class="row">
<div class="col-xs-12">
<div id="map"></div>
</div>
</div>
</div>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCC-S80K6c6OmI9nUVxR3PjIxj-PKybFeY"></script>
Remove the points from the path that are more than the specified number of points before the marker's location. Your example only contains 77 points. Below is an example that limits the polyline to 20 points after the marker.
code snippet:
var map;
function initialize() {
var markLAT = coords[50].lat;
var markLNG = coords[50].lng;
map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(markLAT, markLNG),
zoom: 11,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
autoRefresh();
}
google.maps.event.addDomListener(window, 'load', initialize);
var icon = new google.maps.MarkerImage("https://maps.google.com/mapfiles/ms/micons/blue.png");
function moveMarker(map, marker, lat, lon) {
marker.setPosition(new google.maps.LatLng(lat, lon));
}
function autoRefresh() {
var i, route, marker;
route = new google.maps.Polyline({
path: [],
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2,
editable: false,
map: map
});
marker = new google.maps.Marker({
map: map,
icon: icon
});
console.log("coords.length=" + coords.length);
for (i = 0; i < coords.length; i++) {
setTimeout(function(coords) {
route.getPath().push(new google.maps.LatLng(coords.lat, coords.lng));
if (route.getPath().getLength() > 20)
for (var i = 0; i < route.getPath().getLength() - 20; i++) {
route.getPath().removeAt(0);
}
moveMarker(map, marker, coords.lat, coords.lng);
}, 200 * i, coords[i]);
}
}
var coords = [{
"lat": 8.893260000000001,
"lng": 76.61427
},
{
"lat": 8.894430000000002,
"lng": 76.61418
},
{
"lat": 8.89484,
"lng": 76.61416000000001
},
{
"lat": 8.894860000000001,
"lng": 76.61383000000001
},
{
"lat": 8.89488,
"lng": 76.61375000000001
},
{
"lat": 8.89563,
"lng": 76.61370000000001
},
{
"lat": 8.896270000000001,
"lng": 76.61366000000001
},
{
"lat": 8.896700000000001,
"lng": 76.61364
},
{
"lat": 8.89671,
"lng": 76.61351
},
{
"lat": 8.896740000000001,
"lng": 76.61312000000001
},
{
"lat": 8.89675,
"lng": 76.61307000000001
},
{
"lat": 8.89677,
"lng": 76.61305
},
{
"lat": 8.89729,
"lng": 76.61303000000001
},
{
"lat": 8.89755,
"lng": 76.61311
},
{
"lat": 8.898050000000001,
"lng": 76.61311
},
{
"lat": 8.898280000000002,
"lng": 76.61321000000001
},
{
"lat": 8.899180000000001,
"lng": 76.61289000000001
},
{
"lat": 8.900350000000001,
"lng": 76.61396
},
{
"lat": 8.900860000000002,
"lng": 76.61467
},
{
"lat": 8.90127,
"lng": 76.61581000000001
},
{
"lat": 8.901710000000001,
"lng": 76.61796000000001
},
{
"lat": 8.902190000000001,
"lng": 76.61932
},
{
"lat": 8.902370000000001,
"lng": 76.62017
},
{
"lat": 8.902370000000001,
"lng": 76.62088
},
{
"lat": 8.902460000000001,
"lng": 76.6212
},
{
"lat": 8.90313,
"lng": 76.62217000000001
},
{
"lat": 8.903450000000001,
"lng": 76.62240000000001
},
{
"lat": 8.903970000000001,
"lng": 76.62272
},
{
"lat": 8.90409,
"lng": 76.62280000000001
},
{
"lat": 8.904,
"lng": 76.62288000000001
},
{
"lat": 8.90342,
"lng": 76.6233
},
{
"lat": 8.902560000000001,
"lng": 76.62386000000001
},
{
"lat": 8.90033,
"lng": 76.62522000000001
},
{
"lat": 8.89601,
"lng": 76.62777000000001
},
{
"lat": 8.88676,
"lng": 76.63327000000001
},
{
"lat": 8.884450000000001,
"lng": 76.63461000000001
},
{
"lat": 8.882610000000001,
"lng": 76.63582000000001
},
{
"lat": 8.88089,
"lng": 76.63711
},
{
"lat": 8.87918,
"lng": 76.63862
},
{
"lat": 8.87785,
"lng": 76.63936000000001
},
{
"lat": 8.875760000000001,
"lng": 76.63996
},
{
"lat": 8.87273,
"lng": 76.64141000000001
},
{
"lat": 8.87067,
"lng": 76.64251
},
{
"lat": 8.869280000000002,
"lng": 76.64336
},
{
"lat": 8.86805,
"lng": 76.6447
},
{
"lat": 8.86782,
"lng": 76.6451
},
{
"lat": 8.86677,
"lng": 76.64822000000001
},
{
"lat": 8.86645,
"lng": 76.64933
},
{
"lat": 8.866200000000001,
"lng": 76.65092
},
{
"lat": 8.86546,
"lng": 76.6533
},
{
"lat": 8.86508,
"lng": 76.65451
},
{
"lat": 8.86495,
"lng": 76.65667
},
{
"lat": 8.864880000000001,
"lng": 76.65962
},
{
"lat": 8.86519,
"lng": 76.66080000000001
},
{
"lat": 8.866240000000001,
"lng": 76.66343
},
{
"lat": 8.86646,
"lng": 76.66454
},
{
"lat": 8.866200000000001,
"lng": 76.66933
},
{
"lat": 8.86569,
"lng": 76.67323
},
{
"lat": 8.86522,
"lng": 76.67823
},
{
"lat": 8.863840000000001,
"lng": 76.68872
},
{
"lat": 8.86359,
"lng": 76.6907
},
{
"lat": 8.86364,
"lng": 76.69282000000001
},
{
"lat": 8.86317,
"lng": 76.69574
},
{
"lat": 8.863420000000001,
"lng": 76.69850000000001
},
{
"lat": 8.8634,
"lng": 76.69958000000001
},
{
"lat": 8.863050000000001,
"lng": 76.70048000000001
},
{
"lat": 8.862350000000001,
"lng": 76.70149
},
{
"lat": 8.862020000000001,
"lng": 76.70239000000001
},
{
"lat": 8.86176,
"lng": 76.70448
},
{
"lat": 8.86218,
"lng": 76.70703
},
{
"lat": 8.863180000000002,
"lng": 76.70957
},
{
"lat": 8.8636,
"lng": 76.71115
},
{
"lat": 8.86382,
"lng": 76.71257
},
{
"lat": 8.86383,
"lng": 76.71368000000001
},
{
"lat": 8.86354,
"lng": 76.71509
},
{
"lat": 8.863240000000001,
"lng": 76.71595
},
{
"lat": 8.86259,
"lng": 76.71731000000001
}
];
#map {
width: 99, 9%;
position: relative;
height: 500px;
border: 1px solid black;
margin-top: 30px;
}
<div class="container">
<div class="row">
<div class="col-xs-12">
<div id="map"></div>
</div>
</div>
</div>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCC-S80K6c6OmI9nUVxR3PjIxj-PKybFeY"></script>
Here's my code:
var jsonStr = (JSON.stringify(data, ['flightPositions', 'flightId', 'positions', 'lat', 'lon', 'date'], 4));
alert(jsonStr);
var jsonObj = JSON.parse(jsonStr);
alert(jsonObj.flightPositions);
},
The output from JSON.stringify seems to be correct. I've run it through JSLint and gives me no error:
{
"flightPositions": [
{
"flightId": 621650693,
"positions": [
{
"lat": 40.4272,
"lon": -74.3395,
"date": "2015-10-31T13:31:55.000Z"
},
{
"lat": 40.6833,
"lon": -74.1667,
"date": "2015-10-31T13:32:00.000Z"
},
{
"lat": 40.6133,
"lon": -74.0122,
"date": "2015-10-31T13:32:55.000Z"
},
{
"lat": 40.7538,
"lon": -74.0989,
"date": "2015-10-31T13:33:55.000Z"
},
{
"lat": 40.8125,
"lon": -74.1044,
"date": "2015-10-31T13:34:55.000Z"
},
{
"lat": 40.8147,
"lon": -74.1832,
"date": "2015-10-31T13:35:55.000Z"
},
{
"lat": 40.7624,
"lon": -74.2316,
"date": "2015-10-31T13:36:55.000Z"
},
{
"lat": 40.6829,
"lon": -74.2523,
"date": "2015-10-31T13:37:55.000Z"
}
]
},
{
"flightId": 621650363,
"positions": [
{
"lat": 40.7859,
"lon": -74.0978,
"date": "2015-10-31T13:29:21.000Z"
},
{
"lat": 40.7738,
"lon": -74.143,
"date": "2015-10-31T13:30:21.000Z"
},
{
"lat": 40.7169,
"lon": -74.1948,
"date": "2015-10-31T13:31:21.000Z"
},
{
"lat": 40.6602,
"lon": -74.2524,
"date": "2015-10-31T13:32:21.000Z"
},
{
"lat": 40.5888,
"lon": -74.3109,
"date": "2015-10-31T13:33:21.000Z"
},
{
"lat": 40.5004,
"lon": -74.3464,
"date": "2015-10-31T13:34:21.000Z"
},
{
"lat": 40.4213,
"lon": -74.3074,
"date": "2015-10-31T13:35:21.000Z"
},
{
"lat": 40.3152,
"lon": -74.2497,
"date": "2015-10-31T13:36:21.000Z"
},
{
"lat": 40.2224,
"lon": -74.1577,
"date": "2015-10-31T13:37:21.000Z"
},
{
"lat": 40.163,
"lon": -74.0203,
"date": "2015-10-31T13:38:21.000Z"
}
]
},
{
"flightId": 621650734,
"positions": [
{
"lat": 40.6742,
"lon": -74.1728,
"date": "2015-10-31T13:29:15.000Z"
},
{
"lat": 40.6369,
"lon": -74.1976,
"date": "2015-10-31T13:30:15.000Z"
},
{
"lat": 40.7258,
"lon": -74.1342,
"date": "2015-10-31T13:30:22.000Z"
},
{
"lat": 40.8227,
"lon": -74.0561,
"date": "2015-10-31T13:32:15.000Z"
},
{
"lat": 40.7436,
"lon": -74.2853,
"date": "2015-10-31T13:33:15.000Z"
},
{
"lat": 40.6199,
"lon": -74.2754,
"date": "2015-10-31T13:34:15.000Z"
},
{
"lat": 40.5328,
"lon": -74.2924,
"date": "2015-10-31T13:35:15.000Z"
},
{
"lat": 40.4304,
"lon": -74.2721,
"date": "2015-10-31T13:36:15.000Z"
},
{
"lat": 40.3341,
"lon": -74.2384,
"date": "2015-10-31T13:37:15.000Z"
},
{
"lat": 40.2547,
"lon": -74.173,
"date": "2015-10-31T13:38:15.000Z"
}
]
},
{
"flightId": 621633505,
"positions": [
{
"lat": 40.3481,
"lon": -74.7084,
"date": "2015-10-31T13:29:35.000Z"
},
{
"lat": 40.3788,
"lon": -74.6202,
"date": "2015-10-31T13:30:35.000Z"
},
{
"lat": 40.4126,
"lon": -74.5206,
"date": "2015-10-31T13:31:35.000Z"
},
{
"lat": 40.4436,
"lon": -74.4448,
"date": "2015-10-31T13:32:35.000Z"
},
{
"lat": 40.45,
"lon": -74.4333,
"date": "2015-10-31T13:32:35.000Z"
},
{
"lat": 40.481,
"lon": -74.3348,
"date": "2015-10-31T13:33:47.000Z"
},
{
"lat": 40.5225,
"lon": -74.2702,
"date": "2015-10-31T13:34:47.000Z"
},
{
"lat": 40.5691,
"lon": -74.2228,
"date": "2015-10-31T13:35:47.000Z"
},
{
"lat": 40.6234,
"lon": -74.1737,
"date": "2015-10-31T13:36:47.000Z"
},
{
"lat": 40.6804,
"lon": -74.1765,
"date": "2015-10-31T13:37:47.000Z"
}
]
},
{
"flightId": 621650749,
"positions": [
{
"lat": 40.6906,
"lon": -74.2433,
"date": "2015-10-31T13:34:54.000Z"
},
{
"lat": 40.631,
"lon": -74.5142,
"date": "2015-10-31T13:35:54.000Z"
},
{
"lat": 39.6624,
"lon": -74.6148,
"date": "2015-10-31T13:36:54.000Z"
},
{
"lat": 40.6833,
"lon": -74.1667,
"date": "2015-10-31T13:37:00.000Z"
},
{
"lat": 40.7207,
"lon": -74.1416,
"date": "2015-10-31T13:37:54.000Z"
}
]
}
]
}
alert output from JSON.stringify
However, I am getting the following for the JSON.parse:
[object Object],[object Object],[object Object],[object Object],[object Object]
What could be wrong here?
UPDATE
Changed alert to console.log (without concatenating string) and it gives me the following output:
Array [ Object, Object, Object, Object, Object ]
console
Your code to stringify and parse JSON is correct, the behavior you are seeing is due to the alert function. alert automatically tries to convert the parameter to a human-readable string (not a JSON string), in your case it converts every item inside the flightPositions array to string.
To debug JS you should use the console.log method (docs) and the browser developer's tools (use F12 to open the tools).
var jsonStr = (JSON.stringify(data, ['flightPositions', 'flightId', 'positions', 'lat', 'lon', 'date'], 4));
console.log(jsonStr);
var jsonObj = JSON.parse(jsonStr);
console.log(jsonObj.flightPositions);
Using the following Sammy JS view, AmCharts and JSON, I'm trying to load more than one graph into the "insights" view:
this.get('#/insights', function (context) {
context.$element().html("<div id='chartdiv'></div>");
var chart = AmCharts.makeChart("chartdiv", {
"type": "pie",
"theme": "none",
"titles": [{
"text": "Header Text",
"size": 16
}],
"dataProvider": [{
"product": "Product 1",
"value": 23
}, {
"product": "Product 2",
"value": 56
}, {
"product": "Product 3",
"value": 21
}],
"titleField": "product",
"valueField": "value",
"labelRadius": 5,
"startEffect": "bounce",
"startDuration": 2,
"labelRadius": 15,
"radius": "22%",
"innerRadius": "60%",
"balloonText": "[[title]]<br><span style='font-size:14px'><b>[[value]]</b> ([[percents]]%)</span>"
//"labelText": "[[title]]"
});
});
I tried adding another div into:
context.$element().html("<div id='chartdiv'></div><div id='chartdiv2'></div>");
with associated amcharts data:
var chart = AmCharts.makeChart("chartdiv2", {
"type": "pie",
"theme": "none",
"titles": [{
"text": "Header Text",
"size": 16
}],
"dataProvider": [{
"product": "Product 1",
"value": 23
}, {
"product": "Product 2",
"value": 56
}, {
"product": "Product 3",
"value": 21
}],
"titleField": "product",
"valueField": "value",
"labelRadius": 5,
"startEffect": "bounce",
"startDuration": 2,
"labelRadius": 15,
"radius": "22%",
"innerRadius": "60%",
"balloonText": "[[title]]<br><span style='font-size:14px'><b>[[value]]</b> ([[percents]]%)</span>"
//"labelText": "[[title]]"
});
});
I can't seem to get the second graph to display.
I have used multiple piecharts this way, you can refer to my code below:-
/*
This code block i have used in php for dynamic data, you can ignore this block.
This is just to give you an idea about i am using data to populate the chart.
*/
$return['chartdata'] = $dataset;
$return['titlefield'] = $_POST['display_field'];
$return['valuefield'][] = 'count';
$return['valuefield'][] = 'value';
echo json_encode($return);
/*
Ends Here.
*/
/* Below code is what will help you. */
for (var i = 0; i < result.valuefield.length; i++) {
$('#charts_div').append("<div class='col-md-6'><div id='chartdiv"+(i+1)+"' style='width: 640px; height: 400px;'></div></div>");
var chart = new AmCharts.AmPieChart();
chart.pathToImages = gbl_js+"amcharts/images/";
chart.dataProvider = result.chartdata;
chart.titleField = result.titlefield;
chart.valueField = result.valuefield[i];
chart.legend = {
"markerType": "circle",
"position": "bottom",
"marginRight": 80,
"autoMargins": false,
"valueText": ""
};
chart.exportConfig = {
"menuTop": 0,
"menuItems": [{
"icon": gbl_js+'amcharts/images/export.png',
"items": [{
"title": 'JPG',
"format": 'jpg'
}, {
"title": 'PNG',
"format": 'png'
}, {
"title": 'SVG',
"format": 'svg'
}, {
"title": 'PDF',
"format": 'pdf'
}]
}
};
chart.write('chartdiv'+(i+1));
};