LoginSignup
2
0

More than 1 year has passed since last update.

国土地理院APIを使って住所からLatitude、Longitudeに変換する

Last updated at Posted at 2023-03-03

MAP上にマーカーを描くために住所からLatitude、Longitudeに変換したいと思いますが、結構件数があるのでできれば自動化したい。
まずはAPIを組み込めるか検討します。

会社の住所を使ってURLをたたくと

https://msearch.gsi.go.jp/address-search/AddressSearch?q=%E6%84%9B%E7%9F%A5%E7%9C%8C%E5%90%8D%E5%8F%A4%E5%B1%8B%E5%B8%82%E6%9D%B1%E5%8C%BA%E8%B5%A4%E5%A1%9A%E7%94%BA%EF%BC%92%EF%BC%98

このようなJsonが返ってきます。

[{"geometry":{"coordinates":[136.923203,35.183064],"type":"Point"},"type":"Feature","properties":{"addressCode":"","title":"愛知県名古屋市東区赤塚町28番地"}}]

Apexで呼び出しの部分だけをコーディングしてみましょう。

 /**
     * 国土地理院
     */
    @AuraEnabled
    public static Map<String,Object> ins_gio(String ins_body) {
        Map<String,Object>  RetMap = new Map<String,Object>();
        Http http = new Http();
        String path ='https://msearch.gsi.go.jp/address-search/AddressSearch';
        String parameters = '?q=' + ins_body;

        HttpRequest req = new HttpRequest();
        req.setEndpoint(path+parameters);
        req.setMethod('GET');
        //req.setHeader('Content-Type', 'application/json');//POSTには必須みたい
        
        HttpResponse res = http.send(req);
        //JsonをObjectに変換する
        String resBody = res.getBody(); 
        RetMap.put('resBody',resBody);        
        
        
        return RetMap;
    }

Jsonの解析です。JSON.deserializeStrict(resBody,geoData.class);を使って一発で処理できるようにClassを定義します。
"coordinates":[136.923203,35.183064] の定義に少々悩みました...

public static  String create_geo(){
        String retStr ='';
        String resBody ='[{"geometry":{"coordinates":[136.923203,35.183064],"type":"Point"},"type":"Feature","properties":{"addressCode":"","title":"愛知県名古屋市東区赤塚町28番地"}}]';
        resBody = resBody.left(resBody.length() -1);
        resBody = resBody.right(resBody.length() -1);
        geoData g = (geoData)JSON.deserializeStrict(resBody,geoData.class);
        geometry geo = (geometry)g.geometry;
        List<double> coordinates = geo.coordinates;
        system.debug(Logginglevel.INFO,'========= coordinates =======> ' + coordinates);
        return retStr;
    }

    public class geometry {
        public List<double> coordinates;
        public String type;
        
    }
    
    public class properties{
        public String addressCode;
        public String title;
    }
    
    public class geoData {
        public geometry geometry;
        public String type;
        public properties properties;
    }


ずっと、FATAL_ERROR System.JSONException: Malformed JSON: Expected '{' at the beginning of object のエラーではまってました。

なんと返されるJsonの両端が[]になっているのが原因でした。とりあえず[]両端を省く。 もしかしたら、複数件処理できるのかな?List変数かもしれんな。

追記

ListというようにList変数として定義したら両端の[]もそのまま認識できました。やはり複数件処理ができるのではないかと思います。

JSON.deserializeStrict(resBody,List.class);

参考

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0