0

我想我会尝试一下谷歌的新地理定位 API,它可以根据 wifi 信号为您提供您的位置。

我使用 jquery 编写了这个简短的 JS 代码

function testmoj() {
console.log("testmojjs");

var wifiP = [
  {
  "macAddress": "00:26:f2:f7:a7:6b",
  "signalToNoiseRatio": -57
  },
  {
  "macAddress": "58:98:35:57:b8:db",
  "signalToNoiseRatio": -68
  },
  {
  "macAddress": "58:98:35:3c:3b:c9",
  "signalToNoiseRatio": -67
  },
  {
  "macAddress": "00:26:f2:f7:a7:6d",
  "signalToNoiseRatio": -80
  },
  {
  "macAddress": "70:56:81:ca:63:6f",
  "signalToNoiseRatio": -83
  },
  {
  "macAddress": "00:22:3f:0f:84:11",
  "signalToNoiseRatio": -85
  },
  {
  "macAddress": "58:98:35:3a:9f:01",
  "signalToNoiseRatio": -83
  },
  {
  "macAddress": "58:98:35:55:6b:4f",
  "signalToNoiseRatio": -94
  },
  {
  "macAddress": "00:24:17:d5:a7:83",
  "signalToNoiseRatio": -84
  },
  {
  "macAddress": "58:98:35:57:c5:fb",
  "signalToNoiseRatio": -91
  }
 ];


$.ajax({
    type: 'post',
    dataType: 'json',
    contentType: 'application/json',
    url: 'https://www.googleapis.com/geolocation/v1/geolocate?key=MyKeyIsNormallyHere:-)',
    data: { "wifiAccessPoints": wifiP },
    success: function (response) {
        console.log("wooooooooooooooooooooooooooooooooooooooooooo");
        console.log(response);
    },
    error: function () {
        console.log("faaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaail");
    }
});
}

但我得到的总是(错误函数运行):

{
"error": {
"errors": [
{
"domain": "global",
"reason": "parseError",
"message": "Parse Error"
}
], 
"code": 400,
"message": "Parse Error"
}
}

我根本看不到我在这里做错了什么,有什么想法吗?

4

2 回答 2

0

我仍然不确定为什么 javascript 代码不起作用,但我将它从服务器端移到了 .net 上,然后它就起作用了。对于任何想在这里使用它的人来说,这是一个howto。这只是一个快速测试代码,里面还有很多废话!

我也有一段时间得到 403,因为我忘记在我的谷歌开发者控制台/api 控制台中启用它。只需仔细按照 google maps geolocation api's page 上的说明进行操作

    public ActionResult FindLocationForWifi()
    {
        Models.WifiAPInformation apInfo = new Models.WifiAPInformation();

        apInfo.wifiAccessPoints.Add(new Models.WifiAP("00:26:f2:f7:a7:6b", -57));
        apInfo.wifiAccessPoints.Add(new Models.WifiAP("58:98:35:57:b8:db", -68));
        apInfo.wifiAccessPoints.Add(new Models.WifiAP("58:98:35:3c:3b:c9", -67));
        apInfo.wifiAccessPoints.Add(new Models.WifiAP("00:26:f2:f7:a7:6d", -80));
        apInfo.wifiAccessPoints.Add(new Models.WifiAP("70:56:81:ca:63:6f", -83));
        apInfo.wifiAccessPoints.Add(new Models.WifiAP("00:22:3f:0f:84:11", -85));
        apInfo.wifiAccessPoints.Add(new Models.WifiAP("58:98:35:3a:9f:01", -83));
        apInfo.wifiAccessPoints.Add(new Models.WifiAP("58:98:35:55:6b:4f", -94));
        apInfo.wifiAccessPoints.Add(new Models.WifiAP("00:24:17:d5:a7:83", -84));
        apInfo.wifiAccessPoints.Add(new Models.WifiAP("58:98:35:57:c5:fb", -91));

        String url = "https://www.googleapis.com/geolocation/v1/geolocate?key=" + BlogConfiguration.Instance.Frontend.ApiKeys.GoogleMaps.Key;

        var json = new JavaScriptSerializer().Serialize(apInfo);
        var bytes = Encoding.Default.GetBytes(json);

        //return Content(json);

        try
        {

            using (var client = new WebClient())
            {
                client.Headers.Add("Content-Type", "application/json");
                var response = client.UploadData(url, "POST", bytes);

                return Content(Encoding.Default.GetString(response));
            }
        }
        catch (WebException wex)
        {
            if (wex.Response != null)
            {
                System.Diagnostics.Debug.WriteLine("Url: " + Url);
                String Resp = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd();
                System.Diagnostics.Debug.WriteLine(Resp);
            }
            else
                throw;
        }

        //return Json(apInfo, JsonRequestBehavior.AllowGet);
        return Content(url);
    }
}

你需要这些课程

namespace Frontend.Models
{
public class WifiAPInformation
{
    public List<WifiAP> wifiAccessPoints; //according to google name standard for location api

    public WifiAPInformation()
    {
        wifiAccessPoints = new List<WifiAP>();
    }
}

public class WifiAP
{
    public String macAddress; //starting small letter because of standard in google maps geolocation api
    public int signalStrength;

    public WifiAP(String mac, int signal)
    {
        macAddress = mac;
        signalStrength = signal;
    }
}
}
于 2012-11-24T23:11:41.963 回答
0

对于测试,使用以下代码创建文件 test.html(将 GoogleAPIKeyHere 替换为您的 Google API 位置密钥):

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
function sendSurroundingWifis() {
    console.log("sendSurroundingWifis");
    var wifis = '{"wifiAccessPoints":[' +
        '{"macAddress":"00:26:f2:f7:a7:6b","signalStrength":-68},' +
        '{"macAddress":"58:98:35:57:b8:db","signalStrength": -88},' +
        '{"macAddress":"58:98:35:3c:3b:c9","signalStrength": -90}]}';
    $.ajax({
        type: 'post',
        dataType: 'json',
        contentType: 'application/json',
        url: 'https://www.googleapis.com/geolocation/v1/geolocate?key=GoogleAPIKeyHere',
        data: wifis,
        success: function (response) {
            console.log("Working!");
            console.log(response);
            $('#content').html('<a href="http://www.google.com/maps/place/'+response.location.lat+','+response.location.lng+'">Show on google maps</a>');
        },
        error: function () {
            console.log("FAIL!");
        }
    });
}
</script>

<button onclick="sendSurroundingWifis()">sendSurroundingWifis</button> <br>
<div id="content">...</div>
于 2017-10-08T19:45:41.670 回答