1

Javascript rookie warning

I'm utilizing this demo on a new project: http://jquery-ui-map.googlecode.com/svn/trunk/demos/jquery-google-maps-mobile.html#directions_map

All is working well, except i need to make a functionality adjustment.

Currently, the app accesses Geolocation right away as the page loads. I need to find a way to change that, so the geolocation function triggers when clicking a link that I'll have say "Use my current location"

I've tried quite a few times, but I admit my rookie javascript syntax is problem.

I'll paste a clean local-ready version of the map (just need to link modernizer). If anyone can help me trigger the geolocation function via a link, instead of automatically on pageload, you'd be a lifesaver.

<!doctype html>
<html lang="en">
        <head>
        <title>jQuery mobile with Google maps - Google maps jQuery plugin</title>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
        <meta name="viewport" content="width=device-width,initial-scale=1" />
        <link type="text/css" rel="stylesheet" href="http://jquery-ui-map.googlecode.com/svn/trunk/demos/css/jquery-mobile-1.0/jquery.mobile.css" />
        <link rel="stylesheet" href="http://jquery-ui-map.googlecode.com/svn/trunk/demos/css/mobile.css" />
        <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&libraries=places"></script>
        <script type="text/javascript" src="../js/modernizr.js"></script>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js" type="text/javascript"></script>
        <script type="text/javascript" src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script>
        <script type="text/javascript" src="demo.js"></script>
        <script type="text/javascript" src="http://jquery-ui-map.googlecode.com/svn/trunk/ui/jquery.ui.map.js"></script>
        <script type="text/javascript" src="http://jquery-ui-map.googlecode.com/svn/trunk/ui/jquery.ui.map.services.js"></script>
        <script type="text/javascript" src="http://jquery-ui-map.googlecode.com/svn/trunk/ui/jquery.ui.map.extensions.js"></script>
        <script type="text/javascript">

            var mobileDemo = { 'center': '40.74843,-73.98566', 'zoom': 18 };            

            ////////////////////////////////////////////////////////////

            $('#directions_map').live('pageinit', function() {
                demo.add('directions_map', function() {
                    $('#map_canvas_1').gmap({'center': mobileDemo.center, 'zoom': mobileDemo.zoom, 'disableDefaultUI':true, 'callback': function() {
                        var self = this;

                        self.addMarker({'position': this.get('map').getCenter() }).click(function() {
                            self.openInfoWindow({ 'content': 'Hello World!' }, this);
                        });

                        self.set('getCurrentPosition', function() {
                            self.refresh();
                            self.getCurrentPosition( function(position, status) {
                                if ( status === 'OK' ) {
                                    var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude)
                                    self.get('map').panTo(latlng);
                                    self.search({ 'location': latlng }, function(results, status) {
                                        if ( status === 'OK' ) {
                                            $('#from').val(results[0].formatted_address);
                                        }
                                    });
                                } else {
                                    alert('Unable to get current position');
                                }
                            });
                        });


                        $('#submit').click(function() {
                            self.displayDirections({ 'origin': $('#from').val(), 'destination': $('#to').val(), 'travelMode': google.maps.DirectionsTravelMode.DRIVING }, { 'panel': document.getElementById('directions')}, function(response, status) {
                                ( status === 'OK' ) ? $('#results').show() : $('#results').hide();
                            });
                            return false;
                        });
                    }});
                }).load('directions_map');
            });

            $('#directions_map').live('pageshow', function() {
                demo.add('directions_map', $('#map_canvas_1').gmap('get', 'getCurrentPosition')).load('directions_map');
            });

        </script>
        </head>
<body>
<div id="directions_map" data-role="page"><div data-role="content">

    <div class="ui-bar-c ui-corner-all ui-shadow" style="padding:1em;">
      <div id="map_canvas_1" style="height:300px;"></div>
          <p>
            <label for="from">From</label>
            <input id="from" class="ui-bar-c" type="text" value="" />
          </p>
          <p>
            <label for="to">To</label>
            <input id="to" class="ui-bar-c" type="text" value="350 5th Ave, New York, NY 10118" />
          </p>
          <a id="submit" href="#" data-role="button" data-icon="search">Get directions</a>
      </div>

    <div id="results" class="ui-listview ui-listview-inset ui-corner-all ui-shadow" style="display:none;">
      <div class="ui-li ui-li-divider ui-btn ui-bar-b ui-corner-top ui-btn-up-undefined">Results</div>
      <div id="directions"></div>
      <div class="ui-li ui-li-divider ui-btn ui-bar-b ui-corner-bottom ui-btn-up-undefined"></div>
    </div>

</div></div>

</body>
</html>
4

0 回答 0