将地标的经度和纬度存储到数组中

时间:2014-04-14 13:32:57

标签: javascript google-earth-plugin

因此,我正在使用Google Earth API开展网页工作,其中班车在城镇周围移动并接载乘客。 PASSENGERS是一个预定义的对象数组,由于长度的原因,它是一个单独的javascript文件。这是我的函数populate的代码,它使用地标填充3D地图:

function populate()
{
// mark houses
for (var house in HOUSES)
{
    // plant house on map
    new google.maps.Marker({
        icon: "https://google-maps-icons.googlecode.com/files/home.png",
        map: map,
        position: new google.maps.LatLng(HOUSES[house].lat, HOUSES[house].lng),
        title: house
    });
}

// get current URL, sans any filename
var url = window.location.href.substring(0, (window.location.href.lastIndexOf("/")) + 1);

// scatter passengers
for (var i = 0; i < PASSENGERS.length; i++)
{
    // pick a random building
    var building = BUILDINGS[Math.floor(Math.random() * BUILDINGS.length)];

    // prepare placemark
    var placemark = earth.createPlacemark("");
    placemark.setName(PASSENGERS[i].name + " to " + PASSENGERS[i].house);

    // prepare icon
    var icon = earth.createIcon("");
    icon.setHref(url + "/img/" + PASSENGERS[i].username + ".jpg");

    // prepare style
    var style = earth.createStyle("");
    style.getIconStyle().setIcon(icon);
    style.getIconStyle().setScale(4.0);

    // prepare stylemap
    var styleMap = earth.createStyleMap("");
    styleMap.setNormalStyle(style);
    styleMap.setHighlightStyle(style);

    // associate stylemap with placemark
    placemark.setStyleSelector(styleMap);

    // prepare point
    var point = earth.createPoint("");
    point.setAltitudeMode(earth.ALTITUDE_RELATIVE_TO_GROUND);
    point.setLatitude(building.lat);
    point.setLongitude(building.lng);
    point.setAltitude(0.0);

    // associate placemark with point
    placemark.setGeometry(point);

    // add placemark to Earth
    earth.getFeatures().appendChild(placemark);

    // add marker to map
    var marker = new google.maps.Marker({
        icon: "https://maps.gstatic.com/intl/en_us/mapfiles/ms/micons/man.png",
        map: map,
        position: new google.maps.LatLng(building.lat, building.lng),
        title: PASSENGERS[i].name + " at " + building.name
    });

    //remember passenger's placemark and marker for pick-up's sake
    PASSENGERS[i].lat = placemark.getGeometry.getLatitude();
    PASSENGERS[i].lng = placemark.getGeometry.getLongtitude();
}
}

但是,当我加载页面时,我从控制台收到以下错误:Uncaught TypeError: undefined is not a function关于最后两行代码:
PASSENGERS[i].lat = placemark.getGeometry.getLatitude(); PASSENGERS[i].lng = placemark.getGeometry.getLongtitude()

任何想法如何才能正确获得lat和lng?提前谢谢。

2 个答案:

答案 0 :(得分:0)

尝试

PASSENGERS [i] .lat = placemark.getGeometry()。getLatitude();

PASSENGERS [i] .lng = placemark.getGeometry()。getLongtitude();

答案 1 :(得分:0)

正如其他人所说,getGeometry是一种方法,需要执行括号()

同时查看代码,您可以使用building.latbuilding.lng - 因为这些是用于首先设置地标几何的属性。

实际上,调用更多方法getGeometry()。getLatitude()来获取已有的值是毫无意义的。

这个人在楼里,不是吗?

e.g。

PASSENGERS[i].lat = building.lat;
PASSENGERS[i].lng = building.lng;