赞
踩
图1,谷歌地图初始化并实现显示点信息
图2 ,谷歌地图实现两点间路径
图3,高德地图实现搜索显示点,可点击地图取点并获取地址,也可以搜索出来
具体文章
<div style="display: none">
<input id="origin-input" class="controls" type="text"
placeholder="Enter an origin location">
<input id="destination-input" class="controls" type="text"
placeholder="Enter a destination location">
<div id="mode-selector" class="controls" style="display: none">
<input type="radio" name="type" id="changemode-driving" >
<label for="changemode-driving">Driving</label>
</div>
</div>
-地图样式
<div id="map" style="width: 100%;height: 500px;">
<div class="loding" style="background: #eee;height: 500px;width: 100%;text-align: center;line-height: 500px">Loading Google map...</div>
</div>
<script type="text/javascript">
//初始化
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
mapTypeControl: false,
center: {lat: -43.712724, lng: 124.873256},
zoom: 17
});
var infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
var request = {
placeId: '地点id,可以写固定的也可以自动找',
fields: ['name', 'formatted_address', 'place_id', 'geometry']
};
// service.getDetails(request, function(place, status) {
// if (status === google.maps.places.PlacesServiceStatus.OK) {
var marker = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(-43.712724, 124.873256)
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent('<div>' +
"17 Silicon Pl, Tullamarine VIC 3043, Australia" + '</div>');
infowindow.open(map, this);
});
// }
// });
new AutocompleteDirectionsHandler(map);
}
/**
* @constructor
*/
function AutocompleteDirectionsHandler(map) {
this.map = map;
this.originPlaceId = null;
this.destinationPlaceId = null;
this.travelMode = 'WALKING';
this.directionsService = new google.maps.DirectionsService;
this.directionsRenderer = new google.maps.DirectionsRenderer;
this.directionsRenderer.setMap(map);
var originInput = document.getElementById('origin-input');
var destinationInput = document.getElementById('destination-input');
var modeSelector = document.getElementById('mode-selector');
var originAutocomplete = new google.maps.places.Autocomplete(originInput);
// Specify just the place data fields that you need.
originAutocomplete.setFields(['place_id']);
var destinationAutocomplete =new google.maps.places.Autocomplete(destinationInput);
// Specify just the place data fields that you need.
destinationAutocomplete.setFields(['place_id']);
// this.setupClickListener('changemode-walking', 'WALKING');
// this.setupClickListener('changemode-transit', 'TRANSIT');
this.setupClickListener('changemode-driving', 'DRIVING');
this.setupPlaceChangedListener(originAutocomplete, 'ORIG');
this.setupPlaceChangedListener(destinationAutocomplete, 'DEST');
this.map.controls[google.maps.ControlPosition.TOP_LEFT].push(originInput);
this.map.controls[google.maps.ControlPosition.TOP_LEFT].push(destinationInput);
this.map.controls[google.maps.ControlPosition.TOP_LEFT].push(modeSelector);
}
// Sets a listener on a radio button to change the filter type on Places
// Autocomplete.
AutocompleteDirectionsHandler.prototype.setupClickListener = function(
id, mode) {
var radioButton = document.getElementById(id);
var me = this;
radioButton.addEventListener('click', function() {
me.travelMode = mode;
me.route();
});
};
AutocompleteDirectionsHandler.prototype.setupPlaceChangedListener = function(
autocomplete, mode) {
var me = this;
autocomplete.bindTo('bounds', this.map);
autocomplete.addListener('place_changed', function() {
var place = autocomplete.getPlace();
if (!place.place_id) {
window.alert('Please select an option from the dropdown list.');
return;
}
if (mode === 'ORIG') {
me.originPlaceId = place.place_id;
} else {
me.destinationPlaceId = place.place_id;
}
me.route();
});
};
AutocompleteDirectionsHandler.prototype.route = function() {
if (!this.originPlaceId || !this.destinationPlaceId) {
return;
}
var me = this;
this.directionsService.route(
{
origin: {'placeId': this.originPlaceId},
destination: {'placeId': this.destinationPlaceId},
travelMode: this.travelMode
},
function(response, status) {
if (status === 'OK') {
me.directionsRenderer.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
};
function loadScript() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&key=你的钥匙&libraries=places&language=en-ww&' +
'callback=initMap';
$('.loading').remove()
document.body.appendChild(script);
}
window.onload = loadScript;
</script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&key=你的钥匙&libraries=places&language=en-ww"></script>
vue页面里,使用mounted引入initMap()后再引入AutocompleteDirectionsHandler。
国外版是需要地图底图是英文的,搜索结果也是英文的,且海外国家地址都可拿到(高德获取不到国外的地址,逆向地理编码不支持国外经纬度转换),准备拿谷歌实现
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。