2015-11-22 46 views
0

這是我用來在谷歌地圖中的多個地方顯示標記的代碼。 我想補充一點,並在標記周圍添加一個5公里的圓圈。如何在Google Maps API上添加標記圓圈

enter image description here

我試圖加入的代碼塊沒有運氣:

var circle = new google.maps.Circle({ 
center:position, 
radius: radius, 
fillColor: "#0000FF", 
fillOpacity: 0.1, 
map: map, 
strokeColor: "#FFFFFF", 
strokeOpacity: 0.1, 
strokeWeight: 2 
}); 

這是下面的完整代碼,並在這裏參考

var center = "Vilnius, Lithuania"; 
var locations = [ 
    ['Tuskulėnų g. 20, Vilnius 09211, Lithuania', "some info"], 
    ['Stumbrų g. 21, Vilnius 08101, Lithuania', "more information"], 
    ['Kalvarijų g. 55, Vilnius 09317, Lithuania', "blah, blah"], 
    ['Birželio 23-iosios g. 6, Vilnius 03204, Lithuania', "other information"], 
    ['Teatro g. 6, Vilnius 03107, Lithuania', "to be determined"] 
]; 


var geocoder; 
var map; 
var infoWin = new google.maps.InfoWindow(); 

function initialize() { 
    geocoder = new google.maps.Geocoder(); 
    map = new google.maps.Map(
    document.getElementById("map_canvas")); 
    // center and zoom map on "center" address 
    geocoder.geocode({ 
    address: center 
    }, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     map.fitBounds(results[0].geometry.bounds); 
    } else { 
     alert("Geocode was not successful for the following reason: " + status); 
    } 
    }); 
    for (var i = 0; i < locations.length; i++) { 
    codeAddress(locations[i], i); 
    } 

} 

function codeAddress(location, i) { 
    geocoder.geocode({ 
    'address': location[0] 
    }, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     map.setCenter(results[0].geometry.location); 
     var marker = new google.maps.Marker({ 
     map: map, 
     title: "marker " + i, 
     position: results[0].geometry.location 
     }); 
     google.maps.event.addListener(marker, 'click', function(evt) { 
     infoWin.setContent(location[0] + "<br>" + location[1]); 
     infoWin.open(map, this); 
     }) 


//i added this block 
var circle = new google.maps.Circle({ 
center:position, 
radius: radius, 
fillColor: "#0000FF", 
fillOpacity: 0.1, 
map: map, 
strokeColor: "#FFFFFF", 
strokeOpacity: 0.1, 
strokeWeight: 2 
}); 
//end of block addition 

    } else { 
     alert("Geocode was not successful for the following reason: " + status); 
    } 
    }); 
} 

google.maps.event.addDomListener(window, "load", initialize); 

更新的jsfiddle

這是我從控制檯

radius is not defined 
+0

我沒有看到任何企圖加入圈子中發佈的代碼(或爲此事小提琴)。你做了什麼沒有奏效? – geocodezip

+0

@geocodezip你好,我在最後一個'} else {'之前添加了代碼塊,以便在'i'的循環中。 – EnexoOnoma

+0

仍然沒有在發佈的代碼中看到它。只是添加它並適當調整變量適用於我,你是否得到了JavaScript錯誤(如'position'沒有定義,'radius'沒有定義)? – geocodezip

回答

2

你剛纔定義radiusposition變量,如獲得:

var radius = 1000; 
var position = marker.getPosition(); 

而對於塔放大您可以使用map.setZoom();setCenter例如後添加它:

map.setCenter(results[0].geometry.location); 
map.setZoom(5); 

Working fiddle


代碼片段:

var center = "Vilnius, Lithuania"; 
 
var locations = [ 
 
    ['Tuskulėnų g. 20, Vilnius 09211, Lithuania', "some info"], 
 
    ['Stumbrų g. 21, Vilnius 08101, Lithuania', "more information"], 
 
    ['Kalvarijų g. 55, Vilnius 09317, Lithuania', "blah, blah"], 
 
    ['Birželio 23-iosios g. 6, Vilnius 03204, Lithuania', "other information"], 
 
    ['Teatro g. 6, Vilnius 03107, Lithuania', "to be determined"] 
 
]; 
 

 

 
var geocoder; 
 
var map; 
 
var infoWin = new google.maps.InfoWindow(); 
 

 
function codeAddress(location, i) { 
 
    geocoder.geocode({ 
 
    'address': location[0] 
 
    }, function(results, status) { 
 
    if (status == google.maps.GeocoderStatus.OK) { 
 
     map.setCenter(results[0].geometry.location); 
 
     map.setZoom(5); 
 

 
     var marker = new google.maps.Marker({ 
 
     map: map, 
 
     title: "marker " + i, 
 
     position: results[0].geometry.location 
 
     }); 
 
     google.maps.event.addListener(marker, 'click', function(evt) { 
 
     infoWin.setContent(location[0] + "<br>" + location[1]); 
 
     infoWin.open(map, this); 
 
     }) 
 

 
     var radius = 1000; 
 
     var position = marker.getPosition(); 
 

 
     var circle = new google.maps.Circle({ 
 
     center: position, 
 
     radius: radius, 
 
     fillColor: "#0000FF", 
 
     fillOpacity: 0.1, 
 
     map: map, 
 
     strokeColor: "#FFFFFF", 
 
     strokeOpacity: 0.1, 
 
     strokeWeight: 2 
 
     }); 
 

 
    } else { 
 
     alert("Geocode was not successful for the following reason: " + status); 
 
    } 
 
    }); 
 
} 
 

 
google.maps.event.addDomListener(window, "load", function() { 
 
    geocoder = new google.maps.Geocoder(); 
 
    map = new google.maps.Map(
 
    document.getElementById("map_canvas")); 
 
    // center and zoom map on "center" address 
 
    geocoder.geocode({ 
 
    address: center 
 
    }, function(results, status) { 
 
    if (status == google.maps.GeocoderStatus.OK) { 
 
     google.maps.event.addListenerOnce(map, 'bounds_changed', function() { 
 
     map.setZoom(map.getZoom() + 3); 
 
     }); 
 
     map.fitBounds(results[0].geometry.bounds); 
 

 
    } else { 
 
     alert("Geocode was not successful for the following reason: " + status); 
 
    } 
 
    }); 
 

 
    for (var i = 0; i < locations.length; i++) { 
 
    codeAddress(locations[i], i); 
 
    } 
 
});
html, 
 
body, 
 
#map_canvas { 
 
    height: 100%; 
 
    width: 100%; 
 
}
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js"></script> 
 
<div id="map_canvas"></div>

+0

嘿,真的非常感謝你,但我想利用職位名稱,而不是latlng。並圍繞所有的標記,而不是一個通用的一個圓... – EnexoOnoma

+0

不客氣,檢查我更新的答案(也小提琴更新)。 –

+0

我接受了你的答案,非常感謝你,這真的很有幫助。在第22行中,在'map:map'的下面我添加了一個'zoom:5'沒有運氣 – EnexoOnoma

相關問題