入力内容に応じて、英国の 3 つのクローゼット ストアを検索する JavaScript 郵便番号検索があります。現時点では、3 店舗で問題ありません。
まず、入力に入力された郵便番号にマーカーをドロップします。
次に、3 つの結果が表示されると、それらがマップ上にマークされます。クリックすると、最初から選択した店舗までの道順が表示される、方向と呼ばれるリンクが必要です。
次のコードを試しましたが、うまくいきません...ただし、入力と道順リンクから郵便番号データを取得し、コンソールに表示します。機能させるには、それらを経度と緯度に変換する必要がありますか?
function calcRoute() {
var start = document.getElementById('address').value;
var end = document.getElementById('get-directions').name;
//console.log(start, end)
var request = {
origin:start,
destination:end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
開始マーカー用にこのコードがありますが、これも機能していないようです
function initialize() {
var start_marker = new google.maps.LatLng(document.getElementById('address').value);
directionsDisplay = new google.maps.DirectionsRenderer();
var mapOptions = {
zoom:7,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: start_marker
}
marker = new google.maps.Marker({
map:map,
draggable:false,
animation: google.maps.Animation.DROP,
position: start_marker,
});
map = new google.maps.Map(document.getElementById('map'), mapOptions);
directionsDisplay.setMap(map);
}
この部分は、郵便番号から経度/緯度のデータを取得します。
this.geocode = function(address, callbackFunction) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var result = {};
result.latitude = results[0].geometry.location.lat();
result.longitude = results[0].geometry.location.lng();
callbackFunction(result);
//console.log(result);
//console.log("Geocoding " + geometry.location + " OK");
addMarker(map, results[0].geometry.location);
} else {
alert("Geocode was not successful for the following reason: " + status);
callbackFunction(null);
}
});
addMarker の関数は次のとおりです。
function addMarker(map, location) {
console.log("Setting marker for (location: " + location + ")");
marker = new google.maps.Marker({
map : map,
animation: google.maps.Animation.DROP,
position : location
});
}
どんな助けでも大歓迎です!