GeoCoding API V2

索引サイト

(参考)
https://developers.google.com/maps/articles/phpsqlsearch_v3
https://developers.google.com/maps/articles/phpsqlgeocode?hl=ja
https://developers.google.com/maps/documentation/javascript/v2/services?hl=ja
https://developers.google.com/maps/articles/phpsqlajax_v3?hl=ja




[1] 地図表示

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
Copyright 2008 Google Inc.
Licensed under the Apache License, Version 2.0:
http://www.apache.org/licenses/LICENSE-2.0
-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=Shift_JIS"/>
<title>Google Maps AJAX + MySQL/PHP Example</title>
<script src="http://maps.google.com/maps?file=api&v=2&key=AIzaSyC8dCBx4D4wbw2j7LHOj6_tyESKG6RfLJ4"
type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
var common_map;
var common_geocoder;

function FCT_load() {
if (GBrowserIsCompatible()) {
common_geocoder = new GClientGeocoder();
common_map = new GMap2(document.getElementById('id_map'));
common_map.addControl(new GSmallMapControl());
common_map.addControl(new GMapTypeControl());
common_map.setCenter(new GLatLng(38, 140), 4);
}
}

function FCT_display_course() {
FCT_RightSidebar();
}

function FCT_search_inputAddressLocations() {
FCT_drawMap();
FCT_RightSidebar();
}

function FCT_drawMap() {
var w1_course = document.getElementById('id_input_course').value;
var w1_searchUrl = 'php_sqlsearch_gen_xml_dv7.php?p_course=' + w1_course;

GDownloadUrl(w1_searchUrl, function(data) {
var w2_xml = GXml.parse(data);
var w2_markers = w2_xml.documentElement.getElementsByTagName('marker');
common_map.clearOverlays();

var w2_sidebar = document.getElementById('id_sidebar');
w2_sidebar.innerHTML = '';
if (w2_markers.length == 0) {
w2_sidebar.innerHTML = 'No results found.';
common_map.setCenter(new GLatLng(40, -100), 4);
return;
}

var w2_bounds = new GLatLngBounds();
for (var i = 0; i < w2_markers.length; i++) {
var w3_name = w2_markers[i].getAttribute('name');
var w3_address = w2_markers[i].getAttribute('address');
var w3_distance = parseFloat(w2_markers[i].getAttribute('distance'));
var w3_point = new GLatLng(parseFloat(w2_markers[i].getAttribute('lat')),parseFloat(w2_markers[i].getAttribute('lng')));
var w3_marker = FCT_createMarker(w3_point, w3_name, w3_address);

common_map.addOverlay(w3_marker);
var w3_sidebarEntry = FCT_LeftSidebar(w3_marker, w3_name, w3_address, w3_distance);
w2_sidebar.appendChild(w3_sidebarEntry);
w2_bounds.extend(w3_point);
}
common_map.setCenter(w2_bounds.getCenter(), common_map.getBoundsZoomLevel(w2_bounds));
});
}

function FCT_createMarker(point, name, p_address) {

var markerIcon1 = new GIcon();
markerIcon1.image = "./marker1.png";
markerIcon1.shadow = "./shadow.png";
markerIcon1.iconSize = new GSize(42, 56);
markerIcon1.shadowSize = new GSize(70, 56);
markerIcon1.iconAnchor = new GPoint(21, 56);

var markerIcon2 = new GIcon(markerIcon1, "./marker2.png");
var markerIcon3 = new GIcon(markerIcon1, "./marker3.png");

var opt1 = {icon:markerIcon1};
var opt2 = {icon:markerIcon2};
var opt3 = {icon:markerIcon3};

var w_marker = new GMarker(point);

var html = '<b>' + name + '</b> <br/>' + p_address;
GEvent.addListener(w_marker, 'click', function() {w_marker.openInfoWindowHtml(html);});
return w_marker;
}

function FCT_LeftSidebar(p_marker, p_name, p_address, p_distance) {
var w_div = document.createElement('div');
var w_html = '<b>' + p_name + '</b><br>' + p_address;
w_div.innerHTML = w_html;
w_div.style.cursor = 'pointer';
w_div.style.marginBottom = '5px';
GEvent.addDomListener(w_div, 'click', function() {GEvent.trigger(p_marker, 'click');});
GEvent.addDomListener(w_div, 'mouseover', function() {w_div.style.backgroundColor = '#eee';});
GEvent.addDomListener(w_div, 'mouseout', function() {w_div.style.backgroundColor = '#fff';});
return w_div;
}

function FCT_RightSidebar() {
var w_sidebar = document.getElementById('id_course');
w_sidebar.innerHTML = '';
var w1_searchUrl = 'php_sqlsearch_course_xml_dv7.php';

GDownloadUrl(w1_searchUrl, function(data) {
var w2_xml = GXml.parse(data);
var w2_courses = w2_xml.documentElement.getElementsByTagName('course');

if (w2_courses.length == 0) {
w2_sidebar.innerHTML = 'No Course';
return;
}
for (var i = 0; i < w2_courses.length; i++) {
var w3_code = w2_courses[i].getAttribute('code');
var w3_name = w2_courses[i].getAttribute('name');

var w_div = document.createElement('div');
var w_html = '<b>' + w3_code + '</b> --> ' + w3_name + '<br>';
w_div.innerHTML = w_html;
w_div.style.cursor = 'pointer';
w_div.style.marginBottom = '5px';

var w_sidebarEntry = w_div;
w_sidebar.appendChild(w_sidebarEntry);
}
});
}

//]]>
</script>
</head>

<body onload="FCT_load()" onunload="GUnload()">
<input type="button" onclick="FCT_display_course()" value="ポイント表示"/>
Course: <input type="text" id="id_input_course"/>
<input type="button" onclick="FCT_search_inputAddressLocations()" value="Search Locations"/>
<br/>
<br/>

<div style="width:980px; font-family:Arial,sans-serif; font-size:11px; border:1px solid black">
<table>
<tbody>
<tr>
<td width="200" valign="top"><div id="id_sidebar" style="overflow: auto; height: 420px; font-size: 11px; color: #000"></div></td>
<td > <div id="id_map" style="overflow: hidden; width:660px; height:420px"></div> </td>
<td width="120" valign="top"><div id="id_course" style="overflow: auto; height: 420px; font-size: 11px; color: #000"></div></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>

db-->XML




[2] 位置情報読込(XML)

<?php
//require("php_dbinfo.php");
$username="root";
$password="password";
$database="db_geo";

$p_course = $_GET["p_course"];

$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);

$connection=mysql_connect ('localhost', $username, $password);
if (!$connection) {
die("Not connected : " . mysql_error());
}
mysql_query('SET NAMES utf8',$connection);

$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ("Can\'t use db : " . mysql_error());
}

$query = sprintf("SELECT address, name, lat, lng FROM tb_marker where course = " . $p_course);
$result = mysql_query($query);

if (!$result) {
die("Invalid query: " . mysql_error());
}

header("Content-type: text/xml");
//header("Content-type: text/plain");

while ($row = @mysql_fetch_assoc($result)){
// $w1_address = $row['address'];
// $w2_address = mb_convert_encoding($w1_address,"UTF-8","auto");

$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("name", $row['name']);

$newnode->setAttribute("address", $row['address']);
$newnode->setAttribute("lat", $row['lat']);
$newnode->setAttribute("lng", $row['lng']);
// echo $row['name']."<br>";
}

//echo $feed->saveXML();
//die ("debug end-3-");
echo $dom->saveXML();
?>

付加機能(マップ表示以外のデータ読込)




[3] マスタ表示

<?php
//require("php_dbinfo.php");
$username="root";
$password="password";
$database="db_geo";

$dom = new DOMDocument("1.0");
$node = $dom->createElement("courses");
$parnode = $dom->appendChild($node);

$connection=mysql_connect ('localhost', $username, $password);
if (!$connection) {
die("Not connected : " . mysql_error());
}
mysql_query('SET NAMES utf8',$connection);

$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ("Can\'t use db : " . mysql_error());
}

$query = sprintf("SELECT code, name FROM tb_course order by code");
$result = mysql_query($query);

if (!$result) {
die("Invalid query: " . mysql_error());
}
header("Content-type: text/xml");
//header("Content-type: text/plain");

while ($row = @mysql_fetch_assoc($result)){
$node = $dom->createElement("course");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("code", $row['code']);
$newnode->setAttribute("name", $row['name']);
}
echo $dom->saveXML();
?>

位置情報取込バッチ処理




緯度経度情報取り込み

<?php
//mb_language("uni");
//mb_internal_encoding("utf-8");
//mb_http_input("auto");
//mb_http_output("utf-8");

$username="root";
$password="password";
$database="db_geo";

define("MAPS_HOST", "maps.google.com");
define("KEY", "*** google map key ***");

$connection = mysql_connect("localhost", $username, $password);
if (!$connection) {
die("Not connected : " . mysql_error());
}

mysql_query('SET NAMES utf8',$connection);

$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die("Can\'t use db : " . mysql_error());
}

// Select all the rows in the markers table
$query = "SELECT * FROM tb_marker WHERE lat = 0";
$result = mysql_query($query);
if (!$result) {
die("Invalid query: " . mysql_error());
}

// Initialize delay in geocode speed
$delay = 0;
$base_url = "http://" . MAPS_HOST . "/maps/geo?output=xml" . "&key=" . KEY;

// Iterate through the rows, geocoding each address
while ($row = @mysql_fetch_assoc($result)) {
$geocode_pending = true;

while ($geocode_pending) {
$address = $row["address"];
$code = $row["code"];

$w_address = mb_convert_encoding($address,"shift_jis","auto");
echo $address."--UTF8--変換--Shift_JIS-->".$w_address."<br>";

$request_url = $base_url . "&q=" . urlencode($address);
$xml = simplexml_load_file($request_url) or die("url not loading");

$status = $xml->Response->Status->code;
if (strcmp($status, "200") == 0) {
// Successful geocode
$geocode_pending = false;
$coordinates = $xml->Response->Placemark->Point->coordinates;
$coordinatesSplit = explode(",", $coordinates);
// Format: Longitude, Latitude, Altitude
$lat = $coordinatesSplit[1];
$lng = $coordinatesSplit[0];

$query = sprintf("UPDATE tb_marker" .
" SET lat = '%s', lng = '%s' " .
" WHERE code = '%s' LIMIT 1;",
mysql_real_escape_string($lat),
mysql_real_escape_string($lng),
mysql_real_escape_string($code));
$update_result = mysql_query($query);
if (!$update_result) {
die("Invalid query: " . mysql_error());
}
} else if (strcmp($status, "620") == 0) {
// sent geocodes too fast
$delay += 100000;
} else {
// failure to geocode
$geocode_pending = false;
echo "Address " . $address . " failed to geocoded. ";
echo "Received status " . $status . "<br>";

$query = sprintf("UPDATE tb_marker" .
" SET lat = 0, lng = 0" .
" WHERE code = '%s' LIMIT 1;",
mysql_real_escape_string($code));
$update_result = mysql_query($query);
if (!$update_result) {
die("Invalid query: " . mysql_error());
}
}
usleep($delay);
}
}
?>




AX