1

我已经尽一切努力让我的模拟器(目标 2.2)上的地理定位工作,我使用了 telnet 并发布了“geo fix”,然后是经度和纬度。我已经编辑了我的 Android 清单以授予我权限,但我不确定为什么它不会向我显示以我的位置为中心的谷歌地图。如果我将任何坐标放入地图的 setcentered 函数中,但是当我输入坐标时,它不会显示出来。这是我已经检查了几个小时的代码:

<!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> 

提前谢谢你,我已经尝试解决这个问题了这么多小时,我需要的是谷歌地图出现,谷歌地图的中心是我的位置。

4

2 回答 2

0

这是我的一些快速观察。您似乎没有定义一个 onDeviceReady 函数,该函数将在 PhoneGap 初始化时被调用。下面是 PhoneGap 示例代码的副本,展示了如何使用PhoneGap 文档中的onDeviceReady 。

您是否看到这段代码如何定义了一个名为的函数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()">
于 2012-07-25T15:53:29.703 回答
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();
                         });

使用此代码,它对我有用

于 2012-07-26T11:36:52.890 回答