【问题标题】:Getting information of Nearby locations using geolocation使用地理位置获取附近位置的信息
【发布时间】:2014-01-17 15:07:58
【问题描述】:

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

我尝试使用 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();
}

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

【问题讨论】:

  • 到底是什么问题?您是否在浏览器控制台 (Firebug) 中看到任何异常?
  • 没有错误,但没有显示附近的地方
  • 您是否在启用了定位服务的实际手机上进行测试?或者您是在兼容 HTML5 的浏览器中执行此操作,再次启用位置服务?既然您使用的是工作灯,为什么不使用:pic.dhe.ibm.com/infocenter/wrklight/v6r1m0/topic/…
  • 感谢 Zarazthuztra 的回复。我正在 HTML5 浏览器上进行测试。我将通过您的链接

标签: javascript html


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-14
    • 2016-05-08
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    • 2020-11-13
    相关资源
    最近更新 更多