0

Wi-Fi信号に基づいて位置情報を提供できるGoogleの新しいジオロケーションAPIを試してみようと思いました.

私は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に移動してから機能しました。ここでそれを使用したい人のためのハウツーです。これは、まだたくさんのがらくたが残っている簡単なテストコードです!

また、Google Developer Console / APIコンソールで403を有効にするのを忘れていたため、しばらくの間403を入手しました。グーグルマップジオロケーションAPIのページの指示に注意深く従ってください

    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 に答える