从Google API placesSearch获取总标记数 [英] Get total marker count from Google API placesSearch
问题描述
在示例小提琴中,如何获取地图上显示的标记总数?
markers.push(marker)
我将每个标记推入数组中, code>
试图获得像这样的标记总数:
$('。marker-count span')。html(markers.length);
不幸的是,markers.length返回0时,它应该至少返回3.
我在这里有示例代码: http://jsfiddle.net/287C7/
如何显示标记的总数?是否有可能将每个标记添加到我的数组?
我需要知道显示的标记数量,以便我可以在用户没有时提醒用户。 p>
谢谢,
如果您不想查看jsfiddle.net上的代码,请在这里:
var map,places,tmpLatLng,markers = [];
var pos = new google.maps.LatLng(51.5033630,-0.1276250);
var mapOptions = {
zoom:8,
center:new google.maps.LatLng(51.5033630,-0.1276250)
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
//创建地图并引用div#map-canvas容器
var markerBounds = new google.maps.LatLngBounds();
var service = new google.maps.places.PlacesService(map);
//获取现有地点(ajax)
//并将它们放在地图上
var request = {
location :pos,
radius:48000,//最大半径
名称:mc donalds
};
function callback(results,status){
if(status == google.maps.places.PlacesServiceStatus.OK){
for(var i = 0; i< results.length; i ++){
createMarker(results [i]);
}
$('#map-canvas')。attr(data-markers,results.length);
$('。marker-count span')。html(markers.length);
} else {
console.log(Places requests failed:+ status);
}
} //结束回调
函数createMarker(place){
var prequest = {
reference:place.reference
};
var tmpLatLng = place.geometry.location;
var marker = new google.maps.Marker({
map:map,
position:place.geometry.location
});
markers.push(marker);
markerBounds.extend(tmpLatLng);
} //结束createMarker
service.nearbySearch(request,callback);
当您运行代码时,placesSearch调用是异步的:
$('。marker-count span')。html(markers.length);
结果尚未从服务器返回。在更新标记数组后,您需要在回调中执行该操作。
函数回调(结果,状态){
if(status == google.maps.places.PlacesServiceStatus.OK){
for(var i = 0; i< results.length; i ++){
createMarker(results [i]) ;
}
$('#map-canvas')。attr(data-markers,results.length);
$('。marker-count span')。html(markers.length);
} else {
console.log(Places requests failed:+ status);
}
} //结束回调
In the example fiddle, how can I get the total number of markers displayed on the map? I'm pushing each of the markers into an array like this:
markers.push(marker)
And attempting to get the total number of markers like this:
$('.marker-count span').html(markers.length);
Unfortunately, "markers.length" is returning 0 when it should be returning at least 3. I have example code here: http://jsfiddle.net/287C7/
How can I display the total number of markers? Is it not possible to add each marker to my array?
I need to know the amount of markers shown so that I can alert the user if there are none.
Thanks,
In case you don't want to view the code on jsfiddle.net, here it is:
var map, places, tmpLatLng, markers = [];
var pos = new google.maps.LatLng(51.5033630,-0.1276250);
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(51.5033630,-0.1276250)
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
// create the map and reference the div#map-canvas container
var markerBounds = new google.maps.LatLngBounds();
var service = new google.maps.places.PlacesService(map);
// fetch the existing places (ajax)
// and put them on the map
var request = {
location: pos,
radius: 48000, // Max radius
name: "mc donalds"
};
function callback(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
createMarker(results[i]);
}
$('#map-canvas').attr("data-markers",results.length);
$('.marker-count span').html(markers.length);
} else {
console.log("Places request failed: "+status);
}
} // end callback
function createMarker(place) {
var prequest = {
reference: place.reference
};
var tmpLatLng = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
markers.push(marker);
markerBounds.extend( tmpLatLng );
} // end createMarker
service.nearbySearch(request, callback);
the placesSearch call is asynchronous, when you run your code:
$('.marker-count span').html(markers.length);
the result hasn't come back from the server yet. You need to do that in the call back after you update the markers array.
function callback(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
createMarker(results[i]);
}
$('#map-canvas').attr("data-markers",results.length);
$('.marker-count span').html(markers.length);
} else {
console.log("Places request failed: "+status);
}
} // end callback
这篇关于从Google API placesSearch获取总标记数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!