カテゴリー
SugiBlog Webデザイナー・プログラマーのためのお役立ちTips

Geocoder APIを利用した座標変換

Geocoder APIを使えば住所から座標を取得することができます。
今回はJavaScriptではなくHTTPリクエストで座標を取得する方法をご紹介します。

取得するデータはJSON形式で受け取るようにしています。
使用する言語はPHPです。

まずはリクエストするURLとAPIキーを宣言

$googleapis = "https://maps.googleapis.com/maps/api/geocode/json";
$api_key    = "YOUR API KEY";

続いてパラメーターを追加していきます。
検索する住所を追加

$googleapis .= "?address=" . $address;

国を指定(同じ名前の地名が中国等にあるとそちらの座標になってしまうことがあります)

$googleapis .= "&components=country:JP";

APIキーを設定します

$googleapis .= "&key=" . $api_key;

file_get_contents関数を使ってデータを取得します。

$data = file_get_contents($googleapis);

返ってきたJSONデータを解析して座標を取り出します。

$json = json_decode($data);

$lat = $json->results[0]->geometry->location->lat;
$lng = $json->results[0]->geometry->location->lng;

if( $json->status == "OK" ) {
    echo $lat . "," . $lng;
}

詳細な仕様は公式ドキュメントを参照ください。
https://developers.google.com/maps/documentation/geocoding/intro#GeocodingRequests

1,146 views

GoogleMapにGeoJsonデータを読み込んで表示する

GoogleMapに図形等を描画するのに非常に便利な方法がありました。

//GoogleMapインスタンスを生成(v3)
map = new google.maps.Map(document.getElementById("map"), { center: new google.maps.LatLng(35.6585809608813, 139.74543258547783) } );

// GeoJsonを読み込む
map.data.loadGeoJson('sample.geojson');

// スタイルを設定
map.data.setStyle(function(feature) {

    var myStyle = {
        fillColor: "#9AD2AE",
        fillOpacity: 0.5,
        strokeColor: "#1EB26A",
        strokeOpacity: 0.8,
        strokeWeight: 2,
        clickable: true,
        visible: true,
        zIndex: 1
    };

    return (myStyle);

});

sample.geojsonの内容

{
"type": "FeatureCollection",
"name": "sample",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "name": "サンプル1" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 139.74472479682004, 35.659171090464596 ], [ 139.74610447883606, 35.659171090464596 ], [ 139.74610447883606, 35.65809388391181 ], [ 139.74472479682004, 35.65809388391181 ], [ 139.74472479682004, 35.659171090464596 ] ] ] } }
]
}

今回はポリゴンのみですが、他にポイントやポリラインもGeoJsonで読み込み可能です。
GeoJsonは統一された規格なので汎用性があり便利です。

3,091 views

GoogleMaps 数字アイコンを表示する

targetPoint = new google.maps.LatLng(lat, lng);

markerOptions.map = map;
markerOptions.position = targetPoint;
markerOptions.icon = makeNumIcon(1, 'ff7e73');

var marker = new google.maps.Marker(markerOptions);

//ラベル位置の調整(x, y)
marker.icon.labelOrigin = new google.maps.Point(9, 0);

//ラベルを設定
marker.setOptions({label:{text: '1',color: '#000',fontSize: '20px'}});

marker.setMap(map);


// 数字アイコン作成
//{
    makeNumIcon = function(n, color)
    {
        var icon_color = color || 'ff7e73'; //カラーが指定されなかった場合はデフォルトのカラー

        //文字ありのピン
        //var icon = new google.maps.MarkerImage('//chart.apis.google.com/chart?chst=d_map_pin_letter&chld='+ n + '|' + icon_color + '|000000');

        //文字ありのピン(フォントサイズ等指定)
        //var icon = new google.maps.MarkerImage('//chart.apis.google.com/chart?chst=d_map_spin&chld=0.8|0|' + icon_color + '|16|b|'+ n);

        //文字なしのピン
        var icon = new google.maps.MarkerImage('//chart.apis.google.com/chart?chst=d_map_spin&chld=0.5|0|' + icon_color + '|16|b|');

        return icon;
    }
//}
2,930 views

図形を描画して内部の面積を計測する

iOSアプリ上のGoogleMapで図形を描画し、そのパス内部の面積を計測することができます。

まずは図形を描画します。

// Create a rectangular path
let rect = GMSMutablePath()
rect.add(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.0))
rect.add(CLLocationCoordinate2D(latitude: 37.45, longitude: -122.0))
rect.add(CLLocationCoordinate2D(latitude: 37.45, longitude: -122.2))
rect.add(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.2))

// Create the polygon, and assign it to the map.
let polygon = GMSPolygon(path: rect)
polygon.fillColor = UIColor(red: 0.25, green: 0, blue: 0, alpha: 0.05);
polygon.strokeColor = .black
polygon.strokeWidth = 2
polygon.map = mapView

面積を計測

let a: Double = GMSGeometryArea(polygon.path!)

JavaScriptのAPIではパスが閉じている必要がありましたが、
こちらは閉じていなくても閉じているものと仮定してくれるようです。

ただ、実際に使ってみた結果、JavaScript APIのほうで測った面積と24m²ほど差がありました。

JavaScriptでの計測はこちら→GoogleMapで面積計測(v3)

Xcode: 9.4.1
Swift: 3.3
OS: High Sierra 10.13.6

1,527 views

描画ツールライブラリを使う GoogleMap v3

描画ツールを使うとユーザーが地図上に図形を描画することができます。
実際に使用するクラスは「DrawingManager」になります。

このツールは独立したAPIなので、別途使用することを明記しなくてはなりません。
API読み込みの際にパラメータを追加します。

GoogleMapAPI version 3.26

<script type="text/javascript"
  src="https://maps.googleapis.com/maps/api/js?key=[YOUR_API_KEY]&libraries=drawing">
</script>

実装方法は以下のようになります。

var drawingManager = new google.maps.drawing.DrawingManager();
drawingManager.setMap(map);

様々なオプションを指定して実装する

drawingManager = new google.maps.drawing.DrawingManager({
    drawingMode: google.maps.drawing.OverlayType.MARKER, //CIRCLE, MARKER, POLYGON, POLYLINE, RECTANGLE
    drawingControl: true,
    drawingControlOptions: {
        position: google.maps.ControlPosition.TOP_CENTER,
        drawingModes: ['polygon', 'rectangle'] //['marker', 'polyline', 'rectangle', 'circle', 'polygon']
    },
    polygonOptions: {
        fillColor: '#999999',
        fillOpacity: 0.5,
        strokeWeight: 3,
        clickable: false,
        editable: true,
        zIndex: 1
    },
    rectangleOptions: {
        fillColor: '#999999',
        fillOpacity: 0.5,
        strokeWeight: 3,
        clickable: false,
        editable: true,
        zIndex: 1
    }
});
drawingManager.setMap(map);

続きを読む…»

3,097 views