Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have one page in which i am fetching user values from database and show on page. I have one google map in the same page which is showing user location according to data i am fetching. Now this map is launch every time when my window loads and when i have result value then everything is working fine. But if i have no values still the map is loading and gives me geocode error. I simple want when i have no result then the map should not load. How to stop it by using C# code.
JavaScript
google.maps.event.addDomListener(window, 'load', first);
function first() 
        {
            if (lat == "" || lon == "") {
                InitializeMap2();
            }
}
function GoogleGeocode() {
            geocoder = new google.maps.Geocoder();
            this.geocode = function (address, callbackFunction) {
                geocoder.geocode({ 'address': address }, function (results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        var result = {};
                        result.latitude = results[0].geometry.location.lat();
                        result.longitude = results[0].geometry.location.lng();
                        callbackFunction(result);
                    } else {
                        alert("Geocode was not successful for the following reason: " + status);
                        callbackFunction(null);
                    }
                });
            };
        }
 function InitializeMap2() {
            var g = new GoogleGeocode();
            var address = pincode;
            g.geocode(address, function(data) {
// i am not writing the whole function.
});
        }

C#
while (reader.Read())
            {
                found = true; 
                lbl_iam.Text = reader["iamdetail"].ToString();
                lbl_adid.Text = reader["ad_id"].ToString();
lat = reader["latitude"].ToString();
                lon = reader["longitude"].ToString();
}
if (!found)
            {
                //here i want to code to stop google map load
            }
Posted
Updated 21-Jul-14 8:10am
v2

1 solution

I did my maps like this to avoid the error code displaying if there is no results/ no entry made in the selected fields. But if there is no result from a search with the fields populated I feel you should at least show the users why the map did not populate.

C#
function codeAddress(mapdir) {
            address = document.getElementById("LocAddress1").value + " " + document.getElementById("LocAddress2").value + " " + document.getElementById("LocTown").value;
            if (geocoder != null) {
                if (address != "   ") {

                    if (mapdir != "   ") {
                        address = mapdir;
                        geocoder.geocode({ 'address': address }, function (results, status) {
                            marker.setMap(null);
                            if (status == google.maps.GeocoderStatus.OK) {
                                document.getElementById("lblGeoLocation").innerHTML = results[0].geometry.location;
                                document.getElementById("StaticMap").value = results[0].geometry.location;
                                map.setCenter(results[0].geometry.location);
                                marker = new google.maps.Marker({
                                    map: map,
                                    position: results[0].geometry.location
                                });
                            } else {
                                if (status == "ZERO_RESULTS") {
                                    alert('Geocode was not successful for the following reason: ' + status);
                                }
                                else {

                                    
                                }
                            }
                        });

                    }


Hope this helps.

If this does not solve the problem please lect me know so that I can remove this as an answer.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900