LatLngBounds来自kml文件中的多边形?

时间:2012-05-04 21:21:25

标签: javascript google-maps-api-3 gis kml polygon

我是谷歌地图和API的新手,试图将地图平移到点击区域。我没有很好的时间在API参考中找到我需要的东西,而且根本不知道如何解决这个问题,所以我没有太多东西,但到目前为止我已经有了。

我想这样做,所以点击任何区域都会平移到并放大以显示该区域的大部分区域,我相信这是panToBounds的意图。

大多数功能都来自点击文字名称并让地图加载显示正确的位置,而不是点击地图本身

function initialize_neighbourhoodmap(neighbourhood_name) {
    var latlng = new google.maps.LatLng(42.949442,-81.228125);

    var zoom = 11;
    var neighbourhood_center = get_neighbourhood_center();

    var myOptions = {
      zoom: zoom,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById("neighbourhood_map"),
        myOptions);

    var neighbourhoods = new google.maps.KmlLayer('http://maps.google.com/maps/ms?authuser=0&ie=UTF8&hl=en&oe=UTF8&vps=3&msa=0&output=kml&msid=217366496682144933685.0004bf3be50d3cd7ad1c2', {suppressInfoWindows: true, preserveViewport:true});
    neighbourhoods.setMap(map);

    google.maps.event.addListener(neighbourhoods, 'click', function(KmlFeatureData) {
        var neighbourhood_name = KmlFeatureData.featureData.name;
    load_neighbourhood(neighbourhood_name, neighbourhoods);
    window.location = "#neighbourhood-neighbourhood "+neighbourhood_name;
    });

    if(typeof neighbourhood_name !== 'undefined'){
        load_neighbourhood(neighbourhood_name, neighbourhood_center);
    }
}

function load_neighbourhood (neighbourhood_name, neighbourhoods) {
    var neighbourhoodbounds = nonexistentgetboundsfunction(neighbourhood_name);
    map.panToBounds(neighbourhood_center[neighbourhood_name][0]);
    $('#neighbourhood h2').html('neighbourhood '+neighbourhood_name);
}

1 个答案:

答案 0 :(得分:2)

当然你想要其他功能吗?在我看来,UI变得尴尬。好吧,这是:http://jsfiddle.net/g343k/3/

我遇到了一个奇怪的“bug”,或者有些人可能称之为“feature”,它与KML多边形相关联。单击某个区域时,地图会平移到该区域的中心;但是,当单击外部planning_district区域时,地图将平移到框的中心,而不是单击的位置(Sean Mickey在FireFox 12中测试并且平移到单击)。除了将规划区细分为几十个较小的盒子之外,我无法找到解决方法:(现在我已经编写了代码来忽略对规划区的点击。

google.maps.event.addListener(neighbourhoods, 'click', function(KmlFeatureData) {
        var neighbourhood_name = KmlFeatureData.featureData.name;
    load_neighbourhood(neighbourhood_name, neighbourhoods);
    window.location = "#neighbourhood-neighbourhood "+neighbourhood_name;
    // ADDED
    // Consider what to do when this outer area is clicked
    if(KmlFeatureData.featureData.name!="planning_districts_2008") {
      map.panTo(KmlFeatureData.latLng);
    }

    });

    // LEFT OUT
    //if(typeof neighbourhood_name !== 'undefined'){
    //    load_neighbourhood(neighbourhood_name, neighbourhood_center);
    //}

    // ADDED
    google.maps.event.addListener(map, 'click', function(event) {
      map.panTo(event.latLng);
    });