我正在创建一个google maps应用程序,我如何使用JavaScript/HTML5创建一个按钮,在地图上的任何地方随机投下一个标记呢?
我测试过这个代码,它能用。但你可能会检查一下LatLng范围。我不确定我是否做对了。
//Your markers' array
const markers = [];
//Image for pin
const image = {
url: "./your/imagge.png",
};
//Addlistener for your button
$("#rand_marker").click(function () {
//Create LatLng object
const latLng = new google.maps.LatLng(
Math.random() * 180 - 90,
Math.random() * 360 - 180
);
//create Marker object
const new_marker = new google.maps.Marker({
position: latLng,
icon: image,
});
//Add Marker object to your array
markers.push(new_marker);
//Add marker to map (you should have the Map object)
new_marker.setMap(map);
});