1
我正在嘗試合併GOOGLE地理編碼器腳本和「搜索附近」腳本。我似乎無法成功合併它。幫幫我?Google Maps API - 地理編碼和搜索附近
地址解析器腳本就在這裏:
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var mapOptions = {
zoom: 8,
center: latlng
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
}
function codeAddress() {
var address = document.getElementById('address').value;
geocoder.geocode({ 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
而搜索附近的腳本就在這裏:
var map;
var infowindow;
function initialize() {
var pyrmont = new google.maps.LatLng(-33.8665433, 151.1956316);
map = new google.maps.Map(document.getElementById('map-canvas'), {
center: pyrmont,
zoom: 15
});
var request = {
location: pyrmont,
radius: 500,
types: ['store']
};
infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
service.nearbySearch(request, callback);
}
function callback(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
createMarker(results[i]);
}
}
}
function createMarker(place) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(place.name);
infowindow.open(map, this);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
所以我在這裏張貼,因爲我不知道如何將它們合併。我試圖將這些腳本合併在一起,但我無法使它工作。有任何想法嗎?
謝謝。
感謝你的貢獻BNZ,但我似乎無法得到它在Dreamweaver中工作。它在地理編碼的位置沒有問題,但它不會搜索附近... – Tomothy 2014-09-28 06:04:39
你在Dreamweaver中不工作的意思是什麼?我不再喜歡這個工具,你是否包含地方文庫?除非你不會看到附近的地方,否則將需要它們。爲了幫助我,我需要你的代碼。 – devbnz 2014-09-28 11:46:23
答案更新與HTML文件 - 應該在任何平臺上工作>> http://lab.sourcloud.com/stackoverflow/26071099/ – devbnz 2014-09-28 12:31:08