2017-02-24 44 views
0

當我在PC上縮放地圖時,圓圈渲染效果良好,並且功能齊全。但是,當我嘗試在移動設備(iOS)上查看地圖時,圓形渲染效果很好,但如果我放大或縮小屏幕,它們最終會消失(幾次交互之後)。有些圈子根本不渲染。此外,工具提示不會觸摸。有什麼我失蹤?在尋找解決方案時,我找不到任何可以解釋此行爲的解決方案。當地圖放大時,圈子在手機上消失

在此先感謝您提供任何提示或解決方案。代碼是codepen - http://codepen.io/kvyb/pen/VPqWNR?editors=0010

HTML

<!--D3 --> 
<script src='https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js'></script> 
<!--Leaflet/Mapbox --> 
<script src='https://api.tiles.mapbox.com/mapbox.js/v2.1.9/mapbox.js'></script> 
<link href='https://api.tiles.mapbox.com/mapbox.js/v2.1.9/mapbox.css' rel='stylesheet' /> 

<!--Div to hold the map. Inline styling is horrible I know, it's just for demo purpose... --> 
<div id="map" style="position: absolute; height: 100%; width: 100%;"></div> 

CSS

div.tooltip { 
    position: absolute;   
    text-align: center;   
    width: 60px;      
    height: 28px;     
    padding: 2px;    
    font: 12px sans-serif;  
    background: lightsteelblue; 
    border: 0px;   
    border-radius: 8px;   
    pointer-events: none;   
} 

JS

// Map details 
// Define the div for the tooltip 
var div = d3.select("body").append("div") 
    .attr("class", "tooltip")    
    .style("opacity", 0); 

L.mapbox.accessToken = 'pk.eyJ1Ijoic3RlbmluamEiLCJhIjoiSjg5eTMtcyJ9.g_O2emQF6X9RV69ibEsaIw'; 
var map = L.mapbox.map('map', 'mapbox.streets').setView([53.4072, -2.9821], 14); 

// Sample Data 
var data = [ 
    {"coords": [53.3942, -2.9785]}, 
    {"coords": [53.4082, -2.9837]}, 
    {"coords": [54.4082, -2.9837]}, 
]; 

// Loop through data and create d.LatLng 
data.forEach(function(d) { 
    d.LatLng = new L.LatLng(d.coords[0], d.coords[1]); 
    //blue rings around circles: 
    map.addLayer(L.circle([d.coords[0], d.coords[1]], 0)); 

}); 

// Append <svg> to #map 
var svg = d3.select(map.getPanes().overlayPane).append("svg") 
    .attr("class", "leaflet-zoom-animated") 
    .attr("width", window.innerWidth) 
    .attr("height", window.innerHeight); 

// Append <g> to <svg> 
var g = svg.append("g").attr("class", "leaflet-zoom-hide"); 

// Append <circle> to <g> 
var circles = g.selectAll("circle") 
    .data(data) 
    .enter() 
    .append("circle") 
    .style("fill", "rgba(255, 255, 255, .7)") 
.on("mouseover", function(d) { 
      d3.select(this).style("cursor", "default") 
      div.transition()   
       .duration(200)  
       .style("opacity", .9);  
      div .html("formatTime(d.date) ++ d.close") 
       .style("left", (d3.event.pageX) + "px")  
       .style("top", (d3.event.pageY - 28) + "px");  
      }) 
.on("touchstart",function(d) { 
      d3.select(this).style("cursor", "default") 
      div.transition()   
       .duration(200)  
       .style("opacity", .9);  
      div .html("formatTime(d.date) ++ d.close") 
       .style("left", (d3.event.pageX) + "px")  
       .style("top", (d3.event.pageY - 28) + "px");  
      }) 
    .on("touchend", function(d) {  
       div.transition()   
       .duration(500)  
       .style("opacity", 0); 
     }) 
     .on("mouseout", function(d) {  
       div.transition()   
       .duration(500)  
       .style("opacity", 0); 
     }); 


function update() { 
    translateSVG() 
    circles.style("stroke", "gray") 
    circles.attr("cx", function(d) { return map.latLngToLayerPoint(d.LatLng).x; }) 
    circles.attr("cy", function(d) { return map.latLngToLayerPoint(d.LatLng).y; }) 
    circles.attr("r", "30") 
} 

// Adjust the circles when the map is moved 
function translateSVG() { 
    var viewBoxLeft = document.querySelector("svg.leaflet-zoom-animated").viewBox.animVal.x; 
    var viewBoxTop = document.querySelector("svg.leaflet-zoom-animated").viewBox.animVal.y; 
    // Reszing width and height incase of window resize 
    svg.attr("width", window.innerWidth) 
    svg.attr("height", window.innerHeight) 
     // Adding the ViewBox attribute to our SVG to contain it 
    svg.attr("viewBox", function() { 
     return "" + viewBoxLeft + " " + viewBoxTop + " " + window.innerWidth + " " + window.innerHeight; 
    }); 
    // Adding the style attribute to our SVG to transkate it 
    svg.attr("style", function() { 
     return "transform: translate3d(" + viewBoxLeft + "px, " + viewBoxTop + "px, 0px);"; 
    }); 
} 

// Re-draw on reset, this keeps the markers where they should be on reset/zoom 
map.on("moveend", update); 
update(); 

回答

1

我想你的SVG的是,因爲這些元素的CSS的dissapearing是默認設置到overflow: hidden。你可以嘗試設置溢出覆蓋在你的js代碼爲svg.attr

function translateSVG() { 
... 
svg.attr("style", function() { 
     return "overflow: overlay;" + 
      "transform: translate3d(" + viewBoxLeft + "px, " + viewBoxTop + "px, 0px);"; 
}); 
} 

關於應用程序的移動設備上的行爲,你記得設置的元吧?

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /> 

另一件值得調試的事情是,如果您的移動設備上的地圖甚至會對點擊做出響應。在示例中,使用簡單警報創建功能,以檢查您在地圖上點擊是否觸發了警報功能。

我沒有這個問題,但我看書上說在這種情況下它可能有助於以下屬性添加到div包含地圖:

data-tap-disabled="true" 
+0

謝謝,不知道該點之一,但你的解決方案解決了我的問題! –

+0

@KristianVybiral很高興聽到它的幫助,不客氣:) – d324223

相關問題