获取位置以在控制台中显示

时间:2016-05-22 20:04:00

标签: javascript geolocation

我正在尝试将latitued和经度打印到控制台,但是我的代码没有显示任何内容。它似乎适用于JSfiddle,但不适用于我的桌面。

public static async Task<byte[]> GetPostContentAsync(string url, string data)
{
    var content = new MemoryStream();
    var cookies = new CookieContainer();

    HttpWebRequest webReq;

    webReq = (HttpWebRequest)WebRequest.Create(url);

    webReq.CookieContainer = cookies;
    webReq.Method = "POST";
    webReq.ContentType = "application/x-www-form-urlencoded";

    Stream requestStream = await webReq.GetRequestStreamAsync();

    using (var writer = new StreamWriter(requestStream))
    {
        await writer.WriteAsync(data);
    }

    using (var responseStream = await webReq.GetResponseAsync())
    {
        using (Stream response = responseStream.GetResponseStream())
        {
            await response.CopyToAsync(content);
        }
    }

    return content.ToArray();
}

修改:deprectated since PHP 5.3.0

1 个答案:

答案 0 :(得分:0)

像@ M4tini说的那样,工作正常。问题是,为什么不为你这样做?

如果授予,拒绝或提示,您可以找到新的权限api(仅适用于firefox和blink)

您还可以使用错误回调来确定是否有任何问题获得位置

navigator.permission.query({name: 'geolocation'}).then(permission => {
    alert(permission.state)
})

navigator.geolocation.getCurrentPosition(success, error);

function error(err) {
    console.warn('ERROR(' + err.code + '): ' + err.message)
}