在Google Map v3上偏移x,y位置的D3标记

时间:2013-05-09 17:45:46

标签: javascript google-maps google-maps-api-3 d3.js google-maps-markers

我正在尝试使用Google地图上的d3.js在同一纬度/长度覆盖多个标记。我试图显示的数据是天气数据,所以我希望标记一次显示温度,露点,相对湿度等。我需要让标记偏离给每个标记的纬度/长点。我的地图位于http://alert.fcd.maricopa.gov/alert/Google/v3/mobile_test.html点击数据集,然后点击天气数据。现在只有Dewpoint会显示,我相信temp在下面,因为我的javascript文件没有出现任何错误 我希望标记显示的图片位于Image

function updateWxImage() {
    var reqstamp = Number(new Date());
    var rptDate = " ";
    var strDataset = "";
    var item = $('#date')[0];
    item.style.background = "#ffff00";
    item.style.color = "#000000";
    item.innerHTML = "<strong>...updating dataset...</strong>";
    ClearOverlays();
    var width = 36;
    var height = 12;

    // Start D3 Code
    var fill = d3.scale.linear()
        .domain([0.0, 150.0])
        .range(["black", "black"]);

    // Load the station data. When the data comes back, create an overlay.
    // Calls currain.php which converts xml to json
    d3.json("./php/currentwx.php", function(jsonData) {
        overlayD3 = new google.maps.OverlayView();


        var data = jsonData.data;

        // Add the container when the overlay is added to the map.
        overlayD3.onAdd = function() {
            var layer = d3.select(this.getPanes().floatPane).append("div")
                .attr("class", "stations")
                .attr("id", "stations");

            // Draw each marker as a separate SVG element.
            // We could use a single SVG, but what size would it have
            overlayD3.draw = function() {
                var projection = this.getProjection();

                padding = 12;

                var marker1 = layer.selectAll("svg")
                    .data(d3.entries(data))
                    .each(transform) // update existing markers
                    .enter().append("svg:svg")
                    .each(transform)
                    .attr("class", "marker")
                    .attr("id", "marker")
                    .attr("width", width)
                    .attr("height", height)
                    .attr("title",  function(d) { return d.value.tempid + ' - ' + d.value.name; })
                    .on("click", function(d) { addTempInfoWindow(d); });


                    // Add a rectangle.
                    marker1.append("svg:rect")
                        .attr("x", 0)
                        .attr("y", 0)
                        .attr("rx", 4)
                        .attr("ry", 4)
                        .attr("width", padding*3)
                        .attr("height", padding)
                        .attr("title",  function(d) { return d.value.tempid + ' - ' + d.value.name; })
                        .style("fill", function(d) { return fill(d.value.temp); }) 
                        .style("display", function(d) { if (d.value.temp == 0 || d.value.temp < -90) { return "none"; } else { return "inline"; }});


                    // Add a label.
                    marker1.append("svg:text")
                        .attr("x", padding*1.5)
                        .attr("y", padding*0.85)
                        .attr("text-anchor", "middle")
                        .attr("font-weight", "bold")
                        .attr("font-size", function(d) { if (d.value.temp == 0 || d.value.temp < -90) { return padding*1.05; } else { return padding; }})
                        .style("fill", function(d) { 
                            if (d.value.temp < -90) {
                                return "red"; 

                            } else {
                                return "red"; 

                            }
                        })
                        .text(function(d) { 
                            if (d.value.temp < -90) {
                                 return "M";
                            } else {
                                return d.value.temp; 
                            }
                        });

                function transform(d) {
                    d = new google.maps.LatLng(d.value.latitude, d.value.longitude);
                    d = projection.fromLatLngToDivPixel(d);
                    return d3.select(this)
                        .style("left", d.x + "px")
                        .style("top", d.y + "px");
                }
            };

            overlayD3.onRemove = function() {
                var marker = d3.select("#marker").remove();
                var layer = d3.select("#stations").remove();

            };

        };
        overlayD3.setMap(map);


        overlayD3.onAdd = function() {
            var layer = d3.select(this.getPanes().floatPane).append("div")
                .attr("class", "stations")
                .attr("id", "stations");

            // Draw each marker as a separate SVG element.
            // We could use a single SVG, but what size would it have
            overlayD3.draw = function() {
                var projection = this.getProjection();

                padding = 12;               

                var marker2 = layer.selectAll("svg")
                    .data(d3.entries(data))
                    .each(transform) // update existing markers
                    .enter().append("svg:svg")
                    .each(transform)
                    .attr("class", "marker2")
                    .attr("id", "marker2")
                    .attr("width", width)
                    .attr("height", height)
                    .attr("title",  function(d) { return d.value.dewpointid + ' - ' + d.value.name; })
                    .on("click", function(d) { addDewpointInfoWindow(d); });


                    // Add a rectangle.
                    marker2.append("svg:rect")
                        .attr("x", 0)
                        .attr("y", 0)
                        .attr("rx", 4)
                        .attr("ry", 4)
                        .attr("width", padding*3)
                        .attr("height", padding)
                        .attr("title",  function(d) { return d.value.dewpointid + ' - ' + d.value.name; })
                        .style("fill", function(d) { return fill(d.value.dewpoint); }) 
                        .style("display", function(d) { if (d.value.dewpoint == 0 || d.value.dewpoint < -90) { return "none"; } else { return "inline"; }});


                    // Add a label.
                    marker2.append("svg:text")
                        .attr("x", padding*1.5)
                        .attr("y", padding*0.85)
                        .attr("text-anchor", "middle")
                        .attr("font-weight", "bold")
                        .attr("font-size", function(d) { if (d.value.dewpoint == 0 || d.value.dewpoint < -90) { return padding*1.05; } else { return padding; }})
                        .style("fill", function(d) { 
                            if (d.value.dewpoint < -90) {
                                return "red"; 

                            } else {
                                return "turquoise"; 

                            }
                        })
                        .text(function(d) { 
                            if (d.value.dewpoint < -90) {
                                 return "M";
                            } else {
                                return d.value.dewpoint; 
                            }
                        });

                function transform(d) {
                    d = new google.maps.LatLng(d.value.latitude, d.value.longitude);
                    d = projection.fromLatLngToDivPixel(d);
                    return d3.select(this)
                        .style("left", d.x + "px")
                        .style("top", d.y + "px");
                }
            };

            overlayD3.onRemove = function() {
                var marker2 = d3.select("#marker2").remove();
                var layer = d3.select("#stations").remove();

            };

        };
        overlayD3.setMap(map);


        // Get the date that the report was created from the meta object in the json file
        var meta = jsonData.meta;
        var rptDate = meta.created;
        item = $('#date')[0];
        item.style.background = "#9CF"; 
        item.style.color = "black";
        $('#date')[0].innerHTML = "Data: " + rptDate + strDataset + "(Weather)";

    });
}

我认为我需要编辑此部分,但我尝试的所有内容都不成功。

            function transform(d) {
                d = new google.maps.LatLng(d.value.latitude, d.value.longitude);
                d = projection.fromLatLngToDivPixel(d);
                return d3.select(this)
                    .style("left", d.x + "px")
                    .style("top", d.y + "px");
            }
        };

0 个答案:

没有答案