Reputation: 510
I have a script built from code behind in asp .net. It works fine but just display first 11 markers on the map out of 200. It throws error saying '0' is null or not an object for the 12th address. Even after I removed 12th address and it still behaves the same. All the physical address from the database is also valid. I have 200 such addresses. I am using google map api v3. Thanks in advance for any help. Here is the method I used to build the script:
private void MapScript(DataSet ds)
{
StringBuilder sb = new StringBuilder();
string itemList = string.Empty;
foreach (DataRow r in ds.Tables[0].Rows)
{
// bypass empty rows
if (r["Address"].ToString().Trim().Length == 0)
continue;
sb = sb.Append("'" + r["Address"].ToString().Trim() + "',");
}
if (sb.Length > 0)
{
itemList = sb.ToString().Substring(0, sb.ToString().Length - 1);
itemList = itemList.Replace("\n", " ");
}
//script
script.Text = @"<script type='text/javascript'>
window.onload = function() {
var temp = [" + itemList + @"];
var latlng = new google.maps.LatLng(32.802955, -96.769923);
var options = {
zoom: 10,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map_canvas'), options);
var geocoder = new google.maps.Geocoder();
for (var i = 0; i < temp.length; i++) {
(function(address) {
geocoder.geocode({
'address': address
}, function(results) {
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
title: address
});
});
})(temp[i]);
}
}
</script> ";
}
Upvotes: 1
Views: 1074
Reputation: 4473
You are getting the error '0' is null or not an object
because you are not checking the status of the request before you try to set the position attribute of the marker. Like @david said the status returned is probably OVER_QUERY_LIMIT
.
This is how I have handled this during testing. Basically, it checks the returned status and if the status is over the limit, it will wait for 200 milliseconds and try again. I have only used this in a test environment since the client had a premier account that was not subject to the limits. 200 milliseconds seems to be the sweet spot right now, but Google has changed the limits on how many requests can be made a second in the past.
Here is a fiddle of it working
function Geocode(address) {
geocoder.geocode({
'address': address
}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
//update finished count so we know which markers are done.
finishedCount++;
UpdateProgress();
var result = results[0].geometry.location;
var marker = new google.maps.Marker({
position: result,
map: map
});
bounds.extend(marker.position);
} else if (status === google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
setTimeout(function(){
//retry
Geocode(address);
}, 200);
}else{
alert("Geocode was not successful for the following reason: "
+ status);
}
});
}
function UpdateProgress(){
var progress = Math.round((finishedCount / citiesInTexas.length) * 100);
$('#progress').text("percent done: " + progress);
if(progress === 100)
map.fitBounds(bounds);
}
Upvotes: 1