Solved

Google map markers within give radius e.g. 30 miles from SQL server table using classic asp

Posted on 2012-04-04
4
922 Views
Last Modified: 2012-04-09
Hi All,

I'd like to take a dynamically created dataset from a MSSQL database table using classic asp and plot them on a "google maps" map - ideally within a given range of say 30 miles.

For exmaple, I have a MSSQL tables called LocalStores, that have the following data fields;

1. Store Name
2. Store Type
3. Store Latitude
4. Store Longitude

I've signed up for my Google Key, but cannot work out to extract my MSSQL data using Classic ASP and to plot markers onto a Google maps - I also want to customise the markers based on store type.

I'm totally confused with all the options - I also need the maps to work on Windows/Android and Apple devices - with my example code below does not seem to, it only displays on Windows and not iPads etc.

Any help would be greatly and very much appreciated.

Regards

JamWales

My Code  so far;

========================


default.asp

<%@  language="VBScript" %>
<%Option Explicit%>
<!--#include file="Controls/GoogleMaps.asp" -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Google Maps Searcher</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <%
        ' Initialize Google Maps object, which encapsulates all functionality
        Dim googleMaps : Set googleMaps = New ASP2_Controls_GoogleMapsSearcher
'        googleMaps.APIKey = "xxxxx"
             googleMaps.APIKey = xxxxx"
    %>
</head>
<body>
    <%
        ' Render UI
        googleMaps.Render      
            
            Response.Write "<br><br>Map Drawn - can you see it?<br><br>"

        ' Dispose
       
    %>
</body>
</html>

========================
GoogleMaps.asp

<%
Class ASP2_Controls_GoogleMapsSearcher

    ' ///// Member variables//////

    Dim m_apiKey ' As String
    Dim m_ID ' As String
    Dim m_customFieldLabel ' As String
    Dim m_customAttendees ' As String
    Dim m_attributes ' As String

    ' ///// Properties variables//////

    Public Property Get APIKey
        APIKey = m_apiKey
    End Property

    Public Property Let APIKey(value)
        m_apiKey = value
    End Property

    ' ///// Construciton variables//////

    ' Constructor
    Private Sub Class_Initialize
    End Sub

    ' Destructor
    Private Sub Class_Terminate
    End Sub

    ' ///// Public Methods variables//////

    ' Renders the control UI
    Public Sub Render()
%>
<div id="mapsearcher" style="left: 10px; position: relative;">
    <form id="frmsearcher" method="post" action="">
    Area or Zip Code:
    <input type="text" size="30" name="address" id="address" class="required email">&nbsp;&nbsp;
    Radius:
    <input type="text" size="4" name="radius" value='30' id="radius">&nbsp;
    <input id="optMiles" name="group0" type="radio" title="Miles" checked="checked" />Miles&nbsp;
    <input id="optKm" name="group0" type="radio" title="Kilometers" />Km<br />
    <span id="errMsg" style="font-style: oblique; color: Red"></span>
    <input type="button" value="Geocode" onclick="codeAddress();">
    </form>
</div>
<div id="map" style="width: 1000px; height: 600px; top: 10px; left: 10px; position: relative;
    visibility: hidden;">
</div>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=<%=Me.APIKey %>&sensor=false"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<script type="text/javascript">
    var geocoder;
    var map;
    var cCircle;
    var addr = "";
    var rad = "";

    $(document).ready(function () {
        $("#address").keydown(clearErr);
    });

    function mapLoader() {
        geocoder = new google.maps.Geocoder();
        var latlng = new google.maps.LatLng(-34.397, 150.644);
        var myOptions = {
            zoom: 9,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        map = new google.maps.Map(document.getElementById("map"), myOptions);
    }

    function codeAddress() {
        addr = $("#address").val();
        rad = $("#radius").val();

        if (map == null) {
            mapLoader();
        }

        if (validate()) {
            geocoder.geocode({ 'address': addr }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    map.setCenter(results[0].geometry.location);
                    map.setZoom(9);
                    var marker = new google.maps.Marker({
                        map: map,
                        position: results[0].geometry.location,
                        title: addr
                    });
                    makeRadius();
                    $("#map").css("visibility", "visible");
                } else {
                    $("#errMsg").html("Geocode was not successful for the following reason: " + status + "<br />");
                }
            });
        }
    }

    function makeRadius() {

        if (cCircle != null) {
            cCircle.setMap(null);
        }

        if ($('#optMiles').attr('checked') == 'checked') {
            try {
                var rVal;
                rVal = Math.round(1.61 * rad);
                rad = rVal * 1000;
            }
            catch (e) {
                $("#errMsg").html(e.Message + "<br />");
            }
        }
        else {
            rad = rad * 1000;
        }

        var populationOptions = {
            strokeColor: "#FF0000",
            strokeOpacity: 0.8,
            strokeWeight: 2,
            fillColor: "#FF0000",
            fillOpacity: 0.35,
            map: map,
            center: map.center,
            radius: rad
        };
        cCircle = new google.maps.Circle(populationOptions);
    }

    function validate() {

        var regLet = /\d+|\w+/i;
        var regRad = /^(\d+)+$/i;

        if (!regLet.test(addr)) {
            $("#errMsg").html("*Plase type correct Area or Zip code<br />");
            return false;
        }

        if (rad == "") {
            rad = "30";
            return true;
        }

        if (!regRad.test(rad)) {
            $("#errMsg").html("*Plase type correct Radius value<br />");
            return false;
        }

        return true;
    }

    function clearErr() {
        $("#errMsg").html("");
    }

</script>
<%
   End Sub
End Class
%>


========================
0
Comment
Question by:Jamie
  • 2
  • 2
4 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 37813895
replace :
    $(document).ready(function () {
        $("#address").keydown(clearErr);
    });

Open in new window

by :
    $(document).ready(function () {
    mapLoader();
        $("#address").keydown(clearErr);
    });

Open in new window

0
 

Author Comment

by:Jamie
ID: 37814072
Hi leakim971,

Many thanks for your reply - the maps are displaying on iPhones etc now - thank you very much - rocking!

Do you know how I would place multiple markers from my SQL database onto the map - I can write the classic asp sql calls, just cannot work out how to feed them into the Google map?

Regards

JamWales
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 37814769
Hi JamWales,

Check this thread, hope it will help you :
http://www.experts-exchange.com/Programming/Languages/.NET/Q_27226736.html?cid=1133#a36272263

Regards
0
 

Author Closing Comment

by:Jamie
ID: 37822649
Hi leakim971 - thank you for all your help, much appreciated. Regards JamWales
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now