2015-05-21 63 views
0

在我的應用程序中,我將GeoJSON文件加載到Google地圖中。該文件正確加載沒有問題,但我希望應用其他樣式,這是多段線對象本地的而不是Feature對象。我想要做的是將線串設計爲包含多個圖標和一條虛線。我已經閱讀了關於GeoJSON和虛線的post,它可以工作,但我不希望多段線是單個實體;我想要渲染的多段線位於一個對象(數據層)中。我正在努力實現的是什麼?有沒有可用的解決方法?將折線添加到Google Maps數據層

**更新**

我用從geocodezip代碼並修改爲工廠工作,具有兩種類型:標記和折線。

function LayerFactory() { 
this.entities = []; 
this.labelLayerName = ""; 
} 

LayerFactory.prototype.layerType = PolylineLayer; 
LayerFactory.prototype = new google.maps.MVCObject(); 
LayerFactory.prototype.changed = function (key) { 
    if (this.entities) { 
     for (var i = 0; i < this.entities.length; i++) { 
      this.entities[i].overlay.set(key, this.get(key)); 
     } 
    } 
}; 
LayerFactory.prototype.addEntity = function (entity) { 
    this.entities.push(entity); 

    if (this.layerType === PolylineLayer) { 
     for (var i = 0; i < entity.overlays.length; i++) { 
      var overlay = entity.overlays[i]; 
      //add events here 
     } 
    } 
    else if (this.layerType === MarkerLayer) { 
     //add events here 
    } 
}; 
LayerFactory.prototype.setMap = function (map) { this.set('map', map); }; 
LayerFactory.prototype.getMap = function() { return this.get('map'); }; 
LayerFactory.prototype.createLayer = function (options) { 
    this.labelLayerName = options.labelLayerName; 

    switch (options.layerType) { 
     case "polyline": 
      //set options 
      break; 
     case "marker": 
      //set options 
      break; 
     case "label": 
      //set options 
      break; 
    } 

    return new this.layerType(options); 

}; 

初始化圖層時,我包含標籤的圖層名稱,以便根據可見性單獨切換每個圖層。

myLayer = new LayerFactory(); 
myLayer.createLayer({ map: gmap, layerType: "marker", labelLayerName: "MyLabels" }); 

現在,切換層的時候,我只是拉所需要的層,並設置地圖爲null/GMAP:

yourMapLayer.setMap(/* gmap OR null => show/hide */); 

我希望這有助於人誰經歷過,我遇到的問題。祝你好運。

回答

0

geoxml3中的一些代碼包含多條多段線的MVCObject(它需要像Polygon這樣的paths數組)。不知道它是否以虛線工作。

/** 
* A MultiGeometry object that will allow multiple polylines in a MultiGeometry 
* containing LineStrings to be treated as a single object 
* 
* @param {MutiGeometryOptions} anonymous object. Available properties: 
* map: The map on which to attach the MultiGeometry 
* paths: the individual polylines 
* polylineOptions: options to use when constructing all the polylines 
* 
* @constructor 
*/ 
// only if Google Maps API included 
if (!!window.google && !! google.maps) { 
function MultiGeometry(multiGeometryOptions) { 
    function createPolyline(polylineOptions, mg) { 
    var polyline = new google.maps.Polyline(polylineOptions); 
    google.maps.event.addListener(polyline,'click', function(evt) { google.maps.event.trigger(mg,'click',evt);}); 
    google.maps.event.addListener(polyline,'dblclick', function(evt) { google.maps.event.trigger(mg, 'dblclick', evt);}); 
    google.maps.event.addListener(polyline,'mousedown', function(evt) { google.maps.event.trigger(mg, 'mousedown', evt);}); 
    google.maps.event.addListener(polyline,'mousemove', function(evt) { google.maps.event.trigger(mg, 'mousemove', evt);}); 
    google.maps.event.addListener(polyline,'mouseout', function(evt) { google.maps.event.trigger(mg, 'mouseout', evt);}); 
    google.maps.event.addListener(polyline,'mouseover', function(evt) { google.maps.event.trigger(mg, 'mouseover', evt);}); 
    google.maps.event.addListener(polyline,'mouseup', function(evt) { google.maps.event.trigger(mg, 'mouseup', evt);}); 
    google.maps.event.addListener(polyline,'rightclick', function(evt) { google.maps.event.trigger(mg, 'rightclick', evt);}); 
    return polyline; 
    } 
    this.setValues(multiGeometryOptions); 
    this.polylines = []; 

    for (i=0; i<this.paths.length;i++) { 
    var polylineOptions = multiGeometryOptions; 
    polylineOptions.path = this.paths[i]; 
    var polyline = createPolyline(polylineOptions,this); 
    // Bind the polyline properties to the MultiGeometry properties 
    this.polylines.push(polyline); 
    } 
} 
MultiGeometry.prototype = new google.maps.MVCObject(); 
MultiGeometry.prototype.changed = function(key) { 
    // alert(key+" changed"); 
    if (this.polylines) { 
     for (var i=0; i<this.polylines.length; i++) { 
      this.polylines[i].set(key,this.get(key)); 
     } 
    } 
}; 
MultiGeometry.prototype.setMap = function(map) { this.set('map',map); }; 
MultiGeometry.prototype.getMap = function() { return this.get('map'); }; 
} 
+0

經過進一步調查,多面體對象只是將多義線分組?我希望將它們作爲單個實體,但在一個容器內。基本上,我需要與數據層相同的行爲,但使用折線而不是要素。 – kryptonkal

+0

geocodezip,我能夠使用基類MVCObject並以與MultiGeometry對象類似的方式使用它,但是有單獨的多段線嗎? – kryptonkal

+0

我不確定這是什麼意思。如果您要發佈一個表明您的問題的[最小,完整,測試和可讀示例](http://stackoverflow.com/help/mcve),我可能會將其應用於您的代碼。 – geocodezip

相關問題