Javascript NPObject将KML对象写入Google Earth API时出错
我正在尝试编写一个快速脚本,从facebook中提取数据,然后使用简单的拉伸多边形在google地图实例上绘制数据。我已经让所有的社交内容和ajax都正常工作了,但是无论我每次调用.getcoordinates()做什么,我都会得到一个NPObject错误。特别是在我的文档中调用.getcoordinates()的第一行中的“在NPObject上调用方法时出错”。我环顾斯塔科四周,尝试了所有其他人提出的与NPObject错误有关的东西,但都没有用。有什么想法吗 标题js:Javascript NPObject将KML对象写入Google Earth API时出错,javascript,php,ajax,kml,google-earth-plugin,Javascript,Php,Ajax,Kml,Google Earth Plugin,我正在尝试编写一个快速脚本,从facebook中提取数据,然后使用简单的拉伸多边形在google地图实例上绘制数据。我已经让所有的社交内容和ajax都正常工作了,但是无论我每次调用.getcoordinates()做什么,我都会得到一个NPObject错误。特别是在我的文档中调用.getcoordinates()的第一行中的“在NPObject上调用方法时出错”。我环顾斯塔科四周,尝试了所有其他人提出的与NPObject错误有关的东西,但都没有用。有什么想法吗 标题js: function dr
function drawgraph(name, z, x, y) {
// Create the placemark.
var polygonPlacemark = ge.createPlacemark('');
// Create the polygon.
var polygon = ge.createPolygon('');
polygon.setAltitudeMode(ge.ALTITUDE_RELATIVE_TO_GROUND);
polygonPlacemark.setGeometry(polygon);
// Add points for the outer shape.
var outer = ge.createLinearRing('');
outer.setAltitudeMode(ge.ALTITUDE_RELATIVE_TO_GROUND);
outer.getCoordinates().pushLatLngAlt(x - 0.005, y - 0.005, z);
outer.getCoordinates().pushLatLngAlt(x + 0.005, y + 0.005, z);
outer.getCoordinates().pushLatLngAlt(x - 0.005, y + 0.005, z);
outer.getCoordinates().pushLatLngAlt(x + 0.005, y - 0.005, z);
polygon.setOuterBoundary(outer);
//Create a style and set width and color of line
polygonPlacemark.setStyleSelector(ge.createStyle(''));
var lineStyle = polygonPlacemark.getStyleSelector().getLineStyle();
lineStyle.setWidth(5);
lineStyle.getColor().set('9900ffff');
// Add the placemark to Earth.
ge.getFeatures().appendChild(polygonPlacemark);
}
var ge = null;
google.load("earth", "1", {"other_params":"sensor=true"});
function init() {
init3d();
}
function initCB(instance) {
ge = instance;
ge.getWindow().setVisibility(true);
ge.getNavigationControl().setVisibility(ge.VISIBILITY_SHOW);
}
function failureCB(errorCode) {
}
function init3d() {
google.earth.createInstance('map3d', initCB, failureCB);
}
我在body onload上调用init()
html前端:
<div id="map3d" style="height: 600px; width:100%; margin:0; "></div>
<div id="form" style="text-align:center; width:100%; margin-top:10px;">
<button id = "getcoord" style="background-color:#707070">Get coordinates of Current view</button>
<form id = "form" style="display:inline;" method="GET">
<input type="text" name="place" id="place" >
<select name="dist" id="dist">
<option value="1609">1 Mile</option>
<option value="3218">2 Miles</option>
<option value="8046">5 Miles</option>
<option value="16090">10 Miles</option>
</select>
<select name="number" id="number">
<option value="10">10</option>
<option value="50">50</option>
<option value="100">100</option>
<option value="1000">1000</option>
<option value="5000">5000</option>
</select>
<button id="submit" type="submit">Submit</button>
</form>
</div>
我真的很抱歉在你们身上扔了一堆文字,我只是没有主意。这里的问题是谷歌地球插件不能立即在页面加载中使用。只要页面上的JavasCript和Google Earth插件之间的连接无法通信,就会出现NPObject错误。这可能发生在GE插件尚未初始化时,也可能发生在插件崩溃时 在上面的代码中,您需要在调用google.load之后添加这一行 setOnLoadCallback(init3d) 并移除init3d();调用页面加载。加载页面后,GE插件将异步加载,您需要使用google.setOnLoadCallback获取实际事件,让您知道GE插件已准备就绪。更新了下面的脚本
function drawgraph(name, z, x, y) {
// Create the placemark.
var polygonPlacemark = ge.createPlacemark('');
// Create the polygon.
var polygon = ge.createPolygon('');
polygon.setAltitudeMode(ge.ALTITUDE_RELATIVE_TO_GROUND);
polygonPlacemark.setGeometry(polygon);
// Add points for the outer shape.
var outer = ge.createLinearRing('');
outer.setAltitudeMode(ge.ALTITUDE_RELATIVE_TO_GROUND);
outer.getCoordinates().pushLatLngAlt(x - 0.005, y - 0.005, z);
outer.getCoordinates().pushLatLngAlt(x + 0.005, y + 0.005, z);
outer.getCoordinates().pushLatLngAlt(x - 0.005, y + 0.005, z);
outer.getCoordinates().pushLatLngAlt(x + 0.005, y - 0.005, z);
polygon.setOuterBoundary(outer);
//Create a style and set width and color of line
polygonPlacemark.setStyleSelector(ge.createStyle(''));
var lineStyle = polygonPlacemark.getStyleSelector().getLineStyle();
lineStyle.setWidth(5);
lineStyle.getColor().set('9900ffff');
// Add the placemark to Earth.
ge.getFeatures().appendChild(polygonPlacemark);
}
var ge = null;
google.load("earth", "1", {
"other_params": "sensor=true"
});
function init() {
}
function initCB(instance) {
ge = instance;
ge.getWindow().setVisibility(true);
ge.getNavigationControl().setVisibility(ge.VISIBILITY_SHOW);
}
function failureCB(errorCode) {}
function init3d() {
google.earth.createInstance('map3d', initCB, failureCB);
}
function drawgraph(name, z, x, y) {
// Create the placemark.
var polygonPlacemark = ge.createPlacemark('');
// Create the polygon.
var polygon = ge.createPolygon('');
polygon.setAltitudeMode(ge.ALTITUDE_RELATIVE_TO_GROUND);
polygonPlacemark.setGeometry(polygon);
// Add points for the outer shape.
var outer = ge.createLinearRing('');
outer.setAltitudeMode(ge.ALTITUDE_RELATIVE_TO_GROUND);
outer.getCoordinates().pushLatLngAlt(x - 0.005, y - 0.005, z);
outer.getCoordinates().pushLatLngAlt(x + 0.005, y + 0.005, z);
outer.getCoordinates().pushLatLngAlt(x - 0.005, y + 0.005, z);
outer.getCoordinates().pushLatLngAlt(x + 0.005, y - 0.005, z);
polygon.setOuterBoundary(outer);
//Create a style and set width and color of line
polygonPlacemark.setStyleSelector(ge.createStyle(''));
var lineStyle = polygonPlacemark.getStyleSelector().getLineStyle();
lineStyle.setWidth(5);
lineStyle.getColor().set('9900ffff');
// Add the placemark to Earth.
ge.getFeatures().appendChild(polygonPlacemark);
}
var ge = null;
google.load("earth", "1", {
"other_params": "sensor=true"
});
function init() {
init3d();
}
function initCB(instance) {
ge = instance;
ge.getWindow().setVisibility(true);
ge.getNavigationControl().setVisibility(ge.VISIBILITY_SHOW);
}
function failureCB(errorCode) {}
function init3d() {
google.earth.createInstance('map3d', initCB, failureCB);
}
// This will waint until GE plugin is actually loaded and ready
google.setOnloadCallback(init3d);
本页面提供了一个很棒的教程,其中介绍了在页面上正确加载GE插件所需的步骤()
//get ajax data
$distance = $_POST["dist"];
$place = $_POST["place"];
$limit = $_POST["quantity"];
// get the lat and long of the sent address to use for the fb request
$geo_return = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json? address=".urlencode($place)."&key=AIzaSyDeEkWgri9GAvDVE4QN9j2IeO4_2Dj61iM");
$geo_returned = json_decode($geo_return);
foreach ($geo_returned->results as $results){
$lat = $results->geometry->location->lat;
$lng = $results->geometry->location->lng;
}
//make the fb request for a token and for the data then decode the data and stick it into the $decoded array
$center = $lat . "," . $lng;
$fb_key = file_get_contents("https://graph.facebook.com/oauth/access_token?client_id=[my id]&client_secret=[my secret]grant_type=client_credentials");
$ini = "https://graph.facebook.com/v2.1/search? limit=".$limit."&offset=0&type=place¢er=".$center."&distance=".$distance."";
$fb_get = $ini . "&" . $fb_key;
$fb_got = file_get_contents($fb_get);
$decoded = json_decode($fb_got, true);
$decoded = $decoded['data'];
//setup the foreach loop, the $data variable is where each entry will be stored
$data = array();
$i = 0;
foreach ($decoded as $value) {
//pull the name and the fb id of the place from the object in this iteration of the loop
$name = $value['name'];
$each_id = $value['id'];
//pull the fb json file for this place using the id
$pull = "https://graph.facebook.com/v2.1/". $each_id . "?" . $fb_key;
$detail_array = file_get_contents($pull);
$decode_detail = json_decode($detail_array);
//pull out the checkins as well as the latitude and longitude of the place
$checkins = $decode_detail->checkins;
$fblat = $decode_detail->location->latitude;
$fblng = $decode_detail->location->longitude;
//make another associative array with the variables for the kml objects in fb_front
$entry = array(
"name" => $name,
"checkins" => $checkins,
"lat" => $fblat,
"lng" => $fblng,
);
//store the entry array and step the index variable
$data[$i]=$entry;
$i++;
}
//dump it back out to fb_front
var_dump(json_encode($data));
var ge = null;
google.load("earth", "1", {"other_params":"sensor=true"});
google.setOnLoadCallback(init3d);
function drawgraph(name, z, x, y) {
// Create the placemark.
var polygonPlacemark = ge.createPlacemark('');
// Create the polygon.
var polygon = ge.createPolygon('');
polygon.setAltitudeMode(ge.ALTITUDE_RELATIVE_TO_GROUND);
polygonPlacemark.setGeometry(polygon);
// Add points for the outer shape.
var outer = ge.createLinearRing('');
outer.setAltitudeMode(ge.ALTITUDE_RELATIVE_TO_GROUND);
outer.getCoordinates().pushLatLngAlt(x - 0.005, y - 0.005, z);
outer.getCoordinates().pushLatLngAlt(x + 0.005, y + 0.005, z);
outer.getCoordinates().pushLatLngAlt(x - 0.005, y + 0.005, z);
outer.getCoordinates().pushLatLngAlt(x + 0.005, y - 0.005, z);
polygon.setOuterBoundary(outer);
//Create a style and set width and color of line
polygonPlacemark.setStyleSelector(ge.createStyle(''));
var lineStyle = polygonPlacemark.getStyleSelector().getLineStyle();
lineStyle.setWidth(5);
lineStyle.getColor().set('9900ffff');
// Add the placemark to Earth.
ge.getFeatures().appendChild(polygonPlacemark);
}
var ge = null;
google.load("earth", "1", {
"other_params": "sensor=true"
});
function init() {
}
function initCB(instance) {
ge = instance;
ge.getWindow().setVisibility(true);
ge.getNavigationControl().setVisibility(ge.VISIBILITY_SHOW);
}
function failureCB(errorCode) {}
function init3d() {
google.earth.createInstance('map3d', initCB, failureCB);
}
function drawgraph(name, z, x, y) {
// Create the placemark.
var polygonPlacemark = ge.createPlacemark('');
// Create the polygon.
var polygon = ge.createPolygon('');
polygon.setAltitudeMode(ge.ALTITUDE_RELATIVE_TO_GROUND);
polygonPlacemark.setGeometry(polygon);
// Add points for the outer shape.
var outer = ge.createLinearRing('');
outer.setAltitudeMode(ge.ALTITUDE_RELATIVE_TO_GROUND);
outer.getCoordinates().pushLatLngAlt(x - 0.005, y - 0.005, z);
outer.getCoordinates().pushLatLngAlt(x + 0.005, y + 0.005, z);
outer.getCoordinates().pushLatLngAlt(x - 0.005, y + 0.005, z);
outer.getCoordinates().pushLatLngAlt(x + 0.005, y - 0.005, z);
polygon.setOuterBoundary(outer);
//Create a style and set width and color of line
polygonPlacemark.setStyleSelector(ge.createStyle(''));
var lineStyle = polygonPlacemark.getStyleSelector().getLineStyle();
lineStyle.setWidth(5);
lineStyle.getColor().set('9900ffff');
// Add the placemark to Earth.
ge.getFeatures().appendChild(polygonPlacemark);
}
var ge = null;
google.load("earth", "1", {
"other_params": "sensor=true"
});
function init() {
init3d();
}
function initCB(instance) {
ge = instance;
ge.getWindow().setVisibility(true);
ge.getNavigationControl().setVisibility(ge.VISIBILITY_SHOW);
}
function failureCB(errorCode) {}
function init3d() {
google.earth.createInstance('map3d', initCB, failureCB);
}
// This will waint until GE plugin is actually loaded and ready
google.setOnloadCallback(init3d);