的onclick()获取地理位置是不行的
请告诉我问题:的onclick()获取地理位置是不行的
如果 (我环绕navigator.geolocation.getCurrentPosition(initMap, onError);
蒙山function triger()
然后调用它像: <button id="btn" class="btn btn-default" onclick="triger()">
)
然后
它什么都不做!
其他
它的好!
这里是我的代码:
<script> var map;
var infowindow;
var selecteditem="hospital";
var radiusin=5000;
function triger() {
navigator.geolocation.getCurrentPosition(initMap, onError);
}
function onError(error){
alert("the code is " + error.code + ". \n" + "message: " + error.message);
}
function initMap(position) {
selecteditem= $("#selector").val();
radiusin= $("#radius").val();
alert(selecteditem+"in"+radiusin);
var longitude = position.coords.longitude;
var latitude = position.coords.latitude;
var latLong = new google.maps.LatLng(latitude, longitude);
var pyrmont = latLong;// {lat: -33.867, lng: 151.195};
map = new google.maps.Map(document.getElementById('map'), {
center: pyrmont,
zoom: 14
});
infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
service.nearbySearch({
location: pyrmont,
radius: radiusin,
type: selecteditem
}, 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);
});
}
</script>
和:
<div class="input-group-btn"> <button id="btn" class="btn btn-default" onclick="triger()">
<i class="glyphicon glyphicon-search"></i>
</button>
</div>
回答:
改变
selecteditem= $("#selector").val();
到:
$("#selector").change(function() { slecteditem= $(this).find("option:selected").val(); }
以上是 的onclick()获取地理位置是不行的 的全部内容, 来源链接: utcz.com/qa/260217.html