Javascript 使用地理定位获取附近位置的信息

Javascript 使用地理定位获取附近位置的信息,javascript,html,Javascript,Html,我是编程新手,我想开发一个应用程序,显示食品、商场等项目的列表,当用户单击任何项目时,它应该自动获得用户的位置并显示相应搜索的列表,搜索查询是列表项目的名称 我尝试使用navigator.geolocation.watchPosition()获取用户的坐标 这是我用它做的 Javascript代码 var x=document.getElementById("demo"); function getLocation() { if (navigator.geolocation)

我是编程新手,我想开发一个应用程序,显示食品、商场等项目的列表,当用户单击任何项目时,它应该自动获得用户的位置并显示相应搜索的列表,搜索查询是列表项目的名称

我尝试使用navigator.geolocation.watchPosition()获取用户的坐标

这是我用它做的

Javascript代码

var x=document.getElementById("demo");

function getLocation()
{
    if (navigator.geolocation)
    {
        navigator.geolocation.watchPosition(showPosition);
    }
    else{
        x.innerHTML="Geolocation is not supported by this browser.";
    }
}

function showPosition(position)
{
    initialize(position.coords.latitude,position.coords.longitude);
}

/*getting locations nearby using google places api*/

function initialize(lat,lng) {
    var map = new google.maps.Map(document.getElementById('map-canvas'), {
        center: new google.maps.LatLng(lat,lng),
        zoom: 15
    });

    var request = {
        reference: 'CnRkAAAAGnBVNFDeQoOQHzgdOpOqJNV7K9-c5IQrWFUYD9TNhUmz5-aHhfqyKH0zmAcUlkqVCrpaKcV8ZjGQKzB6GXxtzUYcP-muHafGsmW-1CwjTPBCmK43AZpAwW0FRtQDQADj3H2bzwwHVIXlQAiccm7r4xIQmjt_Oqm2FejWpBxLWs3L_RoUbharABi5FMnKnzmRL2TGju6UA4k'
    };

    var infowindow = new google.maps.InfoWindow();
    var service = new google.maps.places.PlacesService(map);

    service.getDetails(request, function(place, status) {
        if (status == google.maps.places.PlacesServiceStatus.OK) {
            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);
            });
        }
    });
}

google.maps.event.addDomListener(window, 'load', initialize);

function wlCommonInit(){
    require([ "layers/core-web-layer", "layers/mobile-ui-layer" ], dojoInit);

    getLocation();
    initialize();
}

我知道这可能是一个重复的问题,但我没有找到任何匹配我的问题在搜索中。如果有对我有用的帖子,请回复。

我找到了。我的钥匙有问题,当我试图在浏览器中获取详细信息时,它说“访问被拒绝”。所以,我更改了google api密钥并尝试了它。现在我在浏览器中得到了结果。

到底是什么问题?您是否在浏览器控制台(Firebug)中看到任何异常?没有错误,但它没有显示附近的位置您是否在启用位置服务的实际手机上测试此功能?或者,您是在兼容HTML5的浏览器中执行此操作的,还是在启用位置服务的情况下?既然你在使用worklight,为什么不使用:谢谢回复Zarazthuztra。我正在HTML5浏览器上测试。我将浏览你的链接