次の手順に従いマップ上のアドレスをプロットする:
ステップ1:GoogleマップAPIスクリプトファイルをインクルード
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_KEY"></script>
ステップ2:
マップ要素を作成します
<div id="map" style="height: 550px; width: 100%;"></div>
ステップ3:マップ要素を宣言し、住所をジオコーディングしてプロットマーカーにするコード
<script type="text/javascript">
// ====== Create map objects ======
var latlng = new google.maps.LatLng(-34.397, 150.644);
var mapOptions = {
zoom: 10,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var geo = new google.maps.Geocoder();
var map = new google.maps.Map(document.getElementById("map"), mapOptions);
var bounds = new google.maps.LatLngBounds();
// ====== Geocoding ======
function getAddress(search) {
geo.geocode({address:search}, function (results,status)
{
// If that was successful
if (status == google.maps.GeocoderStatus.OK) {
// Lets assume that the first marker is the one we want
var p = results[0].geometry.location;
var lat=p.lat();
var lng=p.lng();
// Output the data
createMarker(search,lat,lng);
}
// ====== Decode the error status ======
else {
alert('Something went wrong. Please Try again.');
}
}
);
}
// ======= Function to create a marker
function createMarker(location,lat,lng) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(lat,lng),
map: map
});
google.maps.event.addListener(marker, 'click', function() {
alert("Your Location : "+location);
});
bounds.extend(marker.position);
}
</script>
ステップ4:データベースからアドレスを取得し、指定されたアドレス変数を使用してスクリプト内でジオコード関数getAddress('address from db')
を呼び出します。
<?php
$city = $row["city"];
$street = $row["streetname"];
?>
<script>
getAddress('<?php echo $street.','.$city; ?>');
</script>
このコードを簡単に理解していただければ幸いです。このコードに関する問題がある場合は、そのことを教えてください。
[Google Maps API](https://developers.google.com/maps/) –
のドキュメントを読むことから始めてください。緯度経度または緯度経度のみを生成したいと考えていますか? –
私は何を読みましたか私は都市とstreetnameを緯度経度に変換し、その地点から地図を生成する必要があります。私は全体のプロセスにいくつかの助けが必要です。 – Hazelcraft