1

我在 phonegap 上设计一个移动应用程序。在这我试图获得用户的当前位置。但我没有在 Andriod Emulator 上获得地图。我使用了这段代码:

<!DOCTYPE HTML>

<html>

     <head>

<meta name="viewport" content="width=device-width; height=device-height; user-scalable=no" />

<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>PhoneGap Map</title>
<link rel="stylesheet" href="/master.css" type="text/css" media="screen" />
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" charset="utf-8" src="cordova-2.0.0.js"></script>
<script type="text/javascript">

function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}

function onDeviceReady() {
    navigator.geolocation.getCurrentPosition(onSuccess, onError,{alert("error"),"enableHighAccuracy":true,"timeout":3000});

}

//GEOLOCATION
var onSuccess = function(position) {
    alert("Latitude: "  + position.coords.latitude   + "\n" +
          "Longitude:" + position.coords.longitude  + "\n");

    var myLat = position.coords.latitude;
    var myLong = position.coords.longitude;

    //MAP
    var mapOptions = {
        center: new google.maps.LatLng(myLat, myLong),
        zoom: 14,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    var map = new google.maps.Map(document.getElementById("map_canvas"),
                                  mapOptions);

};

// onError Callback receives a PositionError object
//
function onError(error) {
    alert("code: "    + error.code    + "\n" +
          "message: " + error.message + "\n");
}


     </script>

     </head>
     <body onload="onLoad()"> 
     <div>map</div>
     <div id="map_canvas" style="width:100px; height: 100px;" ></div>
     </body>
     </html>

我不知道我在哪里错了。有人可以帮我吗?

4

1 回答 1

0

检查此代码...将cordova-1.8.1.js 更改为您的cordova2.0.js ...其余源代码可在线参考。如果它不起作用。然后你的phonegap配置有问题。

<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
            <meta charset="utf-8">
                <title>Google Maps JavaScript API v3 Example: Info Window Simple</title>
                <link href="/maps/documentation/javascript/examples/default.css" rel="stylesheet">
                    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
                     <script type="text/javascript" charset ="utf-8" src="cordova-1.8.1.js"></script>
                    <script>
                        function initialize() {
                            var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
                            var mapOptions = {
                                zoom: 4,
                                center: myLatlng,
                                mapTypeId: google.maps.MapTypeId.ROADMAP
                            }

                            var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);

                            var contentString = '<div id="content">'+
                            '<div id="siteNotice">'+
                            '</div>'+
                            '<h1 id="firstHeading" class="firstHeading">Uluru</h1>'+
                            '<div id="bodyContent">'+
                            '<p><b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large ' +
                            'sandstone rock formation in the southern part of the '+
                            'Northern Territory, central Australia. It lies 335&#160;km (208&#160;mi) '+
                            'south west of the nearest large town, Alice Springs; 450&#160;km '+
                            '(280&#160;mi) by road. Kata Tjuta and Uluru are the two major '+
                            'features of the Uluru - Kata Tjuta National Park. Uluru is '+
                            'sacred to the Pitjantjatjara and Yankunytjatjara, the '+
                            'Aboriginal people of the area. It has many springs, waterholes, '+
                            'rock caves and ancient paintings. Uluru is listed as a World '+
                            'Heritage Site.</p>'+
                            '<p>Attribution: Uluru, <a href="http://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">'+
                            'http://en.wikipedia.org/w/index.php?title=Uluru</a> '+
                            '(last visited June 22, 2009).</p>'+
                            '</div>'+
                            '</div>';

                            var infowindow = new google.maps.InfoWindow({
                                                                        content: contentString
                                                                        });

                            var marker = new google.maps.Marker({
                                                                position: myLatlng,
                                                                map: map,
                                                                title: 'Uluru (Ayers Rock)'
                                                                });
                            google.maps.event.addListener(marker, 'click', function() {
                                                          infowindow.open(map,marker);
                                                          });
                        }

                        </script>
                    </head>
    <body onload="initialize()">
        <div id="map_canvas"></div>
    </body>
</html>
于 2013-01-03T11:00:33.357 回答