2014-08-29 37 views
0

我正在嘗試編寫一個快速腳本,以便從Facebook獲取數據,然後僅使用簡單的拉伸多邊形將其繪製在Google地圖實例上。我已經獲得了所有社交資源和ajax工作,但無論我每次調用.getcoordinates()時發生的任何事情,都會收到NPObject錯誤。具體爲「NPObject上的錯誤調用方法」。在我的文檔的第一行中,我調用了.getcoordinates()。我環顧了一下StackO,並且嘗試了其他任何人一直在關於NPObject錯誤的任何事情,都無濟於事。有任何想法嗎?將KML對象寫入Google Earth API的NPObject錯誤

頭JS:

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); 
} 

我打電話的init()對身體的onload

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> 

的onload JS:

$('#form').submit(function(event){ 
    var fdata = { 
     'place'    : $('#place').val(), 
     'dist'    : $('#dist').val(), 
     'quantity'   : $('#number').val(), 
    }; 
    console.log(fdata); 
    $.ajax({ 
     type: "POST", 
     url: "fb_back.php", 
     data: fdata, 
     cache:false, 
    }).done(function(data) { 
      for(var i = 0; i < data.length; i++) { 

       // get variables from json return 
       var name = +(data[i]["name"]); 
       var hgt = +(data[i]["checkins"]); 
       var lat = +(data[i]["lat"]); 
       var lng = +(data[i]["lng"]); 

       drawgraph(name, hgt, lat, lng);// create the polygon 

      } // for loop 
     }),//success 
    event.preventDefault(); 
});//form submit 

PHP後臺越來越阿賈克斯電話:

//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&center=".$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)); 

我真的很抱歉給你們轉儲文字牆我只是出於想法。

回答

0

這裏的問題是Google地球插件在頁面加載時不會立即可用。只要頁面上的JavasCript和Google Earth插件之間的連接無法通信,NPObject就會發生錯誤。當GE插件尚未初始化時可能會發生這種情況,如果插件崩潰也可能發生這種情況。

在上面的代碼中你的情況,你需要給你打電話的google.load

google.setOnLoadCallback後加入這一行(init3d);

var ge = null; 
google.load("earth", "1", {"other_params":"sensor=true"}); 
google.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在頁面上正確(https://developers.google.com/earth/documentation/