2012-07-25 105 views
1

我已經嘗試了一切,以獲取地理位置在我的模擬器(目標2.2)上工作,我已經使用telnet併發布了「地理修復」,其次是經度和緯度。我編輯了我的Android清單給我的權限,但我不知道爲什麼它不會顯示以我的位置爲中心的谷歌地圖。如果我將任何座標放入地圖的setcentered函數中,但是當我放入座標時,它不會顯示出來。 這是我一直在研究了幾個小時的代碼:地理位置Phonegap Android不工作

<!DOCTYPE html "-//W3C//DTD XHTML 1.0 Strict// 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="content-type" content="text/html; charset=utf-8"/> 
<title>Google Maps JavaScript API Example</title> 
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=AIzaSyBrnId8UIeAeDTMr2sVi-g86izgdb2KhVo&sensor=true" 
type="text/javascript"></script> 
<script type="text/javascript"> 
// Wait for PhoneGap to load 
// PhoneGap is ready 
document.addEventListener("deviceready", onDeviceReady, false); 
navigator.geolocation.getCurrentPosition(geolocationSuccess, [maximumAge: 3000, timeout:   5000, enableHighAccuracy: 
<!--position.coords.latitude--> 
initialize() 
(GBrowserIsCompatible()) 
map = new GMap2(document.getElementById("map_canvas")); 
map.setCenter(new GLatLng(position.coords.latitude,position.coords.longitude), 13) 
map.setUIToDefault(); 
// Add 10 markers to the map at random locations 
var bounds = map.getBounds(); 
var southWest = bounds.getSouthWest(); 
var northEast = bounds.getNorthEast(); 
var lngSpan = northEast.lng() - southWest.lng(); 
var latSpan = northEast.lat() - southWest.lat(); 
} 
}  
</script> 
</head> 
<body onload="initialize()" onunload="GUnload()"> 
<div id="map_canvas" style="width: 500px; height: 300px"></div> 
</body> 
</html> 

預先感謝您,我一直在試圖解決這個問題這麼多小時,我需要的是谷歌地圖出現和谷歌的中心地圖是我的位置。

回答

0

這裏有幾個我的快速觀察。您似乎沒有定義在PhoneGap初始化時將會調用的onDeviceReady函數。以下是PhoneGap示例代碼的副本,顯示如何使用onDeviceReady從the PhoneGap documentation

您是否看到該代碼如何定義了一個名爲onDeviceReady的函數,其中的註釋表示PhoneGap方法可以安全地調用。

您也未看到加載PhoneGap JavaScript <script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script>

<!DOCTYPE html> 
<html> 
    <head> 
    <title>PhoneGap Device Ready Example</title> 

    <script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script> 
    <script type="text/javascript" charset="utf-8"> 

    // Call onDeviceReady when PhoneGap is loaded. 
    // 
    // At this point, the document has loaded but phonegap-1.0.0.js has not. 
    // When PhoneGap is loaded and talking with the native device, 
    // it will call the event `deviceready`. 
    // 
    document.addEventListener("deviceready", onDeviceReady, false); 

    // PhoneGap is loaded and it is now safe to make calls PhoneGap methods 
    // 
    function onDeviceReady() { 
     // Now safe to use the PhoneGap API 
    } 

    </script> 
    </head> 
    <body> 
    </body> 
</html> 

而且,尋找更多的在你的代碼,你沒有使用正確GBrowserIsCompatible()。您需要在if語句中使用此函數調用。

如下我會改變你的代碼:

<script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script> 
<script type="text/javascript"> 
    // Wait for PhoneGap to load 
    // PhoneGap is ready 
    document.addEventListener("deviceready", onDeviceReady, false); 
    <!--position.coords.latitude--> 
    function onDeviceReady() { 
     if (GBrowserIsCompatible()) { 
      navigator.geolocation.getCurrentPosition(geolocationSuccess, [maximumAge: 3000, timeout: 5000, enableHighAccuracy: // some code is missing here! 
      map = new GMap2(document.getElementById("map_canvas")); 
      map.setCenter(new GLatLng(position.coords.latitude,position.coords.longitude), 13) 
      map.setUIToDefault(); 
      // Add 10 markers to the map at random locations 
      var bounds = map.getBounds(); 
      var southWest = bounds.getSouthWest(); 
      var northEast = bounds.getNorthEast(); 
      var lngSpan = northEast.lng() - southWest.lng(); 
      var latSpan = northEast.lat() - southWest.lat(); 
     } else { 
      // add code here to display error that device is not compatible 
     } 
    }  
</script> 
</head> 
    <body onunload="GUnload()"> 
+0

而且,腳本的HTML標籤加載PhoneGap的JavaScript需要使用正確的JavaScript文件的名稱。該名稱取決於您使用的PhoneGap版本。 – HeatfanJohn 2012-07-25 18:30:04

0
/*enter code here*/ function handle_errors(error) 
    { 
     switch(error.code) 
     { 
      case error.PERMISSION_DENIED: alert("user did not share geolocation data"); 
      break; 

      case error.POSITION_UNAVAILABLE: alert("could not detect current position"); 
      break; 

      case error.TIMEOUT: alert("retrieving position timedout"); 
      break; 

      default: alert("unknown error"); 
      break; 
     } 
    } 

    function handle_geolocation_query(position) { 
     var text = "Latitude: " + position.coords.latitude + "<br/>" + 
     "Longitude: " + position.coords.longitude + "<br/>" + 
     "Accuracy: " + position.coords.accuracy + "m<br/>" + 
     "Time: " + new Date(position.timestamp); 
     //$("#info").html(text); 

     // alert(text); 

     var image_url =''; 
     image_url = "http://maps.google.com/maps/api/staticmap?path=color:0x0000ff|weight:5|40.737102,-73.990318|40.749825,-73.987963&zoom=14&size=260x300&sensor=true"; 


     $("#info").slideUp('slow'); 
     $("#map").slideDown('slow'); 

     $('#map').html('<img src="'+image_url+'" />'); 

    } 

/*enter code here*/<a class="request_appoint_ment" href="javascript:void(0);" data-transition="none" id="btnInit">Mark Vehicle Location</a> 


/*enter code here*/<div id="info" style="display:none;"><img src="images/ajax-loader.gif" style="padding:50px 0 50px 100px;" /></div> 
        <div id="map" style="display:none; padding-bottom:20px;"></div> 


/*enter code here*/$('#btnInit').live('click',function(event){ 
         $("#map").slideUp('slow'); 
         $("#info").slideDown('slow'); 
         $('#markmaptype').val(1); 
         initiate_watchlocation(); 
         }); 

使用此代碼,它是爲我工作

+0

你錯過了它的一大塊 – DdD 2012-09-25 06:48:34

+0

不明白它的好友.. – Shyantanu 2012-09-25 09:33:17