Я использую API автозаполнения карты Google для перечисления местоположений. По прогнозам результаты показываются с адресом, подробно с иконками местоположения. Но мне нужно различать типы локаций по иконкам. (например, если адрес местоположения - это местоположение аэропорта, это означает, что на нем должен отображаться значок аэропорта соответственно). Как добиться этого сценария ..
<script>
var map;
var marker;
var polygon;
var bounds;
var myLatLng = { lat: 51.507605, lng: -0.1300052 };
var directionsService;
var directionsDisplay
window.onload = initMap;
// This example requires the Places library. Include the libraries=places parameter when you first load the API. For example:
function initMap() {
map = new google.maps.Map(document.getElementById("map"), {
center: myLatLng,
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP,
scaleControl: true
});
//map.setCenter(center);
new AutocompleteDirectionsHandler(map);
bounds = new google.maps.LatLngBounds();
google.maps.event.addListenerOnce(map, 'tilesloaded', function (evt) {
bounds = map.getBounds();
});
marker = new google.maps.Marker({
position: center
});
polygon = new google.maps.Polygon({
path: area,
geodesic: true,
strokeColor: '#FFd000',
strokeOpacity: 1.0,
strokeWeight: 1,
fillColor: '#FFd000',
fillOpacity: 0.10
});
polygon.setMap(map);
directionsService = new google.maps.DirectionsService;
directionsDisplay = new google.maps.DirectionsRenderer;
directionsDisplay.setMap(map);
calculateAndDisplayRoute(directionsService, directionsDisplay);
}
function calculateAndDisplayRoute(directionsService, directionsDisplay) {
//alert($('#<%=tbPickupLoc.ClientID%>').val() + ' | ' + $('#<%=tbDropLoc.ClientID%>').val());
directionsService.route({
origin: $('#<%=tbPickupLoc.ClientID%>').val(),
destination: $('#<%=tbDropLoc.ClientID%>').val(),
travelMode: 'DRIVING'
}, function (response, status) {
if (status === 'OK') {
directionsDisplay.setDirections(response);
}
});
}
/**
* @constructor
*/
function AutocompleteDirectionsHandler(map) {
this.map = map;
this.originPlaceId = null;
this.destinationPlaceId = null;
this.travelMode = 'DRIVING';
var originInput = document.getElementById('<%=tbPickupLoc.ClientID %>');
var destinationInput = document.getElementById('<%=tbDropLoc.ClientID %>');
this.directionsService = new google.maps.DirectionsService;
this.directionsDisplay = new google.maps.DirectionsRenderer;
this.directionsDisplay.setMap(map);
var options = {
//rankBy: google.maps.places.RankBy.PROMINENCE,
componentRestrictions: { country: "GB" }
};
var originAutocomplete = new google.maps.places.Autocomplete(
originInput, options, { placeIdOnly: true });
var destinationAutocomplete = new google.maps.places.Autocomplete(
destinationInput, options, { placeIdOnly: true });
this.setupPlaceChangedListener(originAutocomplete, 'ORIG');
this.setupPlaceChangedListener(destinationAutocomplete, 'DEST');
}
AutocompleteDirectionsHandler.prototype.setupPlaceChangedListener = function (autocomplete, mode) {
var me = this;
autocomplete.bindTo('bounds', this.map);
autocomplete.addListener('place_changed', function () {
marker.setMap(null);
var place = autocomplete.getPlace();
var newBounds = new google.maps.LatLngBounds();
newBounds = bounds;
if (!place.place_id) {
window.alert("Please select an option from the dropdown list.");
return;
}
marker.setPosition(place.geometry.location);
marker.setMap(map);
newBounds.extend(place.geometry.location);
map.fitBounds(newBounds);
if (mode === 'ORIG') {
me.originPlaceId = place.place_id;
$('#<%=hdnPickupLatLong.ClientID%>').val(place.geometry.location);
if (google.maps.geometry.poly.containsLocation(place.geometry.location, polygon)) {
$('#<%=hdnIsPickupOptArea.ClientID%>').val("true");
}
else {
$('#<%=hdnIsPickupOptArea.ClientID%>').val("false");
}
}
else {
me.destinationPlaceId = place.place_id;
$('#<%=hdnDropLatLong.ClientID%>').val(place.geometry.location);
if (google.maps.geometry.poly.containsLocation(place.geometry.location, polygon)) {
$('#<%=hdnIsDropOptArea.ClientID%>').val("true");
}
else {
$('#<%=hdnIsDropOptArea.ClientID%>').val("false");
}
}
me.route();
});
};
AutocompleteDirectionsHandler.prototype.route = function () {
$('#<%=hdnMiles.ClientID%>').val("0");
$('#<%=hdnDuration.ClientID%>').val("0");
$('#<%=lbMiles.ClientID%>').text("Distance is " + $('#<%=hdnMiles.ClientID%>').val() + " miles and Duration is " + $('#<%=hdnDuration.ClientID%>').val() + " mins");
/* Place Service */
var service = new google.maps.places.PlacesService(map);
if (this.originPlaceId != null) {
$('#<%=hdnOriginId.ClientID%>').val(this.originPlaceId);
service.getDetails({
placeId: this.originPlaceId
}, function (place, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
$('#<%=hdnPickupAddr.ClientID%>').val(place.formatted_address);
var _typesArr = place.types;
var _type = _typesArr[0];
//alert(_type + "-" + place);
if (_type == "airport") {
$('#<%=hdnPickupType.ClientID%>').val("A");
$('#<%=hdnPickupAddr.ClientID%>').val("");
}
else if (_type == "lodging") {
$('#<%=hdnPickupType.ClientID%>').val("H");
}
}
});
}
if (this.destinationPlaceId != null) {
$('#<%=hdnDestId.ClientID%>').val(this.destinationPlaceId);
service.getDetails({
placeId: this.destinationPlaceId
}, function (place, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
$('#<%=hdnDropAddr.ClientID%>').val(place.formatted_address);
var _typesArr = place.types;
var _type = _typesArr[0];
if (_type == "airport") {
$('#<%=hdnDropType.ClientID%>').val("A");
$('#<%=hdnDropAddr.ClientID%>').val("");
}
else if (_type == "lodging") {
$('#<%=hdnDropType.ClientID%>').val("H");
}
}
});
}
if (!this.originPlaceId || !this.destinationPlaceId) {
return;
}
var me = this;
/* Direction Service */
this.directionsService.route({
origin: { 'placeId': this.originPlaceId },
destination: { 'placeId': this.destinationPlaceId },
travelMode: this.travelMode
}, function (response, status) {
if (status === 'OK') {
me.directionsDisplay.setDirections(response);
var distance = response.routes[0].legs[0].distance.value;
var distanceinkm = distance / 1000;
var distanceinmiles = (distanceinkm * 0.621371);
distanceinmiles = distanceinmiles.toFixed(2);
var duration = response.routes[0].legs[0].duration.value;
var dvDistance = "";
duration = parseFloat(duration / 60).toFixed(2);
dvDistance += "Distance is " + $('#<%=hdnMiles.ClientID%>').val() + " miles and ";
dvDistance += "Duration is " + $('#<%=hdnDuration.ClientID%>').val() + " mins";
} else {
window.alert('Directions request failed due to ' + status);
}
});
};
var center = new google.maps.LatLng(51.5123443, -0.09098519999997734);
var area = [
{ lat: 51.249669, lng: -0.142219 },
{ lat: 51.273731, lng: 0.146172 },
{ lat: 51.421264, lng: 0.261528 },
{ lat: 51.578564, lng: 0.310967 },
{ lat: 51.691076, lng: 0.126946 },
{ lat: 51.704695, lng: -0.169685 },
{ lat: 51.733621, lng: -0.257576 },
{ lat: 51.588804, lng: -0.534981 },
{ lat: 51.361279, lng: -0.548714 },
{ lat: 51.304649, lng: -0.488289 },
{ lat: 51.249669, lng: -0.142219 }
];
</script>
Наш результат
Ожидаемый результат