我正在尝试根据几个地址放置谷歌地图标记,我已设法根据坐标显示它们,但我没有坐标我只有地址。这是我现在的代码
<div id="map_canvas"></div>
var map;
function initialize() {
var styles = [{"featureType":"administrative","elementType":"all","stylers":[{"visibility":"simplified"}]},{"featureType":"landscape","elementType":"geometry","stylers":[{"visibility":"simplified"},{"color":"#fcfcfc"}]},{"featureType":"poi","elementType":"geometry","stylers":[{"visibility":"simplified"},{"color":"#fcfcfc"}]},{"featureType":"road.highway","elementType":"geometry","stylers":[{"visibility":"simplified"},{"color":"#dddddd"}]},{"featureType":"road.arterial","elementType":"geometry","stylers":[{"visibility":"simplified"},{"color":"#dddddd"}]},{"featureType":"road.local","elementType":"geometry","stylers":[{"visibility":"simplified"},{"color":"#eeeeee"}]},{"featureType":"water","elementType":"geometry","stylers":[{"visibility":"simplified"},{"color":"#dddddd"}]}];
var styledMap = new google.maps.StyledMapType(styles,
{name: "Styled Map"});
// the position of the marker
var zurichCenter = new google.maps.LatLng(47.3768866,8.541694);
var latlng = new google.maps.LatLng(47.4469659,8.5833443);
var latlng1 = new google.maps.LatLng(46.818188,8.227512);
var myOptions = {
zoom: 11,
center: zurichCenter,
mapTypeControlOptions: {
mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'map_style']
}
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var marker = new google.maps.Marker({
map: map,
position: latlng,
icon: 'img/pin-default.png' // location of the pin image, depending of the skin used
});
var marker1 = new google.maps.Marker({
map: map,
position: latlng1,
icon: 'img/pin-default.png' // location of the pin image, depending of the skin used
});
map.mapTypes.set('map_style', styledMap);
map.setMapTypeId('map_style');
}
google.maps.event.addDomListener(window, 'load', initialize);
google.maps.event.addDomListener(window, "resize", function() {
var center = map.getCenter();
google.maps.event.trigger(map, "resize");
map.setCenter(center);
});
答案 0 :(得分:2)
使用Geocoding API,如评论中所述,您可以将地址转换为坐标。
您可以找到有关如何使用API here的示例。
我无法在您的代码中找到地址。
修改强>
这是一个简单的例子(从上面的链接复制):
var geocoder = new google.maps.Geocoder();
var address = "Bergmannstraße 2, Berlin, Germany";
geocoder.geocode({'address': address}, function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
resultsMap.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: resultsMap,
position: results[0].geometry.location
});
}
else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
工作小提琴: