谷歌地图"附近搜索"回调函数没有在phonegap中调用

时间:2014-09-09 06:12:09

标签: javascript google-maps cordova phonegap-build

我已在我的phonegap应用程序中集成G​​oogle地图,以搜索附近的地方(餐厅)。

但回调函数永远不会被调用。

我在javascript中使用了以下代码:

var map;
var infowindow;
var currentLattitude;
var currentLongitude;

//AddEventListener for device ready
document.addEventListener('deviceready', onDeviceReady, false);


// Function gets called when device is ready
function onDeviceReady()
{
    navigator.geolocation.getCurrentPosition(onSuccess,
                                             onError);



}

// onSuccess Callback receives a Position object
var onSuccess = function(position) {

    currentLattitude = position.coords.latitude;
    currentLongitude = position.coords.longitude;
    initialize();
};

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


function initialize() {
    var pyrmont = new google.maps.LatLng(currentLattitude,currentLongitude);

    map = new google.maps.Map(document.getElementById('map'), {
                              center: pyrmont,
                              zoom: 15
                              });

    var request = {
    location: pyrmont,
    radius: '5000',
    types: ['restaurant']
    };

    infowindow = new google.maps.InfoWindow();

    var service = new google.maps.places.PlacesService(map);
    service.nearbySearch(request, callback);

}


function callback(results, status)
{

    if (status == google.maps.places.PlacesServiceStatus.OK)
    {
        for (var i = 0; i < results.length; i++)
        {
            createMarker(results[i]);
        }
    }
}


function createMarker(place)
{
    var placeLoc = place.geometry.location;
    var marker = new google.maps.Marker({
                                        map: map,
                                        position: place.geometry.location
                                        });

    google.maps.event.addListener(marker, 'click', function() {
                                  infowindow.setContent(place.name);
                                  infowindow.open(map, this);
                                  });
}

我的HTML代码是:

<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/custom.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
        <script type="text/javascript" src="js/jquery.min.js"></script>
        <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBlOnbFDz29ydASADhRcKizpcx9VIfBXV8&sensor=true"></script>
        <script type="text/javascript">

        </script>
        <style type="text/css">
            html, body, #map
            {
                height: 100%;
            }
            #map
            {
                width: 100%;
            }
        </style>
        <title>Hello World</title>
    </head>
    <body>
        <div id="map">
        </div>
    </body>
</html>

提前致谢

0 个答案:

没有答案