开发者

jQuery Mobile + jQuery UI google maps + Fusion Markers

this has been baking my noodle for months, I'll be honest, I am a designer not a programmer so this type of scripting is a bit harder than the average jquery/javascript that I'm used too.

I can't find any basic documentation on how to implement it, apart from this but it's not massively intuitive - http://jquery-ui-map.googlecode.com/svn/trunk/demos/jquery-mobile-example.html

I've tried making a JSfiddle, but I can't even get it to work (now working thanks to Tsar)

I've built a jQuery mobile app and I'm desperate to get the geo-location functionality working with fusion table markers (from my fusion table) and to be allowed to click on the fusion table markers to reveal a info window. This info window will be constructed in fusion tables.

I need the geo-location to center the map on the devices current location - if the geo-location is not available or denied by device user, then the map needs to be centered on these co-ordinates 52.450939, -1.721002.

What would be the next level is to have the the jQuery mobile UI pop-up window but this is not critical, just the standard bubble on the map is fine.

A updated JSFiddle... http://jsfiddle.net/twGHC/30/

My fusion table number is: 1260763

Default location is: (开发者_如何学Pythononly if Geo location is not available) 52.450939, -1.721002

Zoom level: 13

Any advice would be awesome, please feel free to JSfiddle it. Thanks in advance.


Here's a working solution, which detects user's location, drops a marker on it and plots the map with your Fusion Markers. As per Google Maps v3 API documentation:

$(function() {
  var position = new google.maps.LatLng(52.450939, -1.721002);

  getCurrentPosition = function(callback) {
    // Try W3C Geolocation (Preferred)
    if(navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(
        function(pos) {
          position = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
            callback(position);
          }, callback(position));         
    } // Try Google Gears Geolocation
    else if (google.gears) {
      var geo = google.gears.factory.create('beta.geolocation');
      geo.getCurrentPosition(
        function(pos) {
          position = new google.maps.LatLng(pos.latitude,pos.longitude);
          callback(position);
        }, callback(position));          
    } // Browser doesn't support Geolocation
    else {
      // Drop the user off in Coventry =)
      callback(position);
    }
  };
     // call the above function
  getCurrentPosition(InitMap);
});

function InitMap(pos) {
  map = new google.maps.Map(document.getElementById('map_canvas'), {
    center: pos,
    zoom: 14,
    mapTypeId: 'roadmap'
  });

  var marker = new google.maps.Marker({
    position: pos, 
    animation: google.maps.Animation.DROP,
    map: map, 
    title: "You are here, mate!"
  });   

var layer = new google.maps.FusionTablesLayer({
  query: {
    select: 'Geocodable address',
    from: '1260763'
    },
  });

  layer.setMap(map);
};

When user denies tracking of his location, exception is caught in getCurrentPosition, however, 2nd optional parameter in this function is an exception handler, so what I did is simply passed in callback(position) so that default location sets on the map. If you don't want to do it, move out the map initializer code from InitMap into separate function and call it instead, when exceptions are caught, to display a blank map.

See it in action here: http://jsfiddle.net/twGHC/36/

P.S. In case your next question is "how to add a balloon pop-up on marker click", here's what you need to do.


Here is how to do it with jquery-ui-map: http://jsfiddle.net/rweCf/1/ http://jsfiddle.net/rweCf/1/embedded/result/

If you just want to change within a certain radius of the client position this is how you would do it http://jsfiddle.net/Ywknf/1/ (client location is the initial point so everyone can see the results)

Here is the code if the url isnt working or the trunk code changed

$(function() {

  $('#map_canvas').gmap({'center': '52.450939, -1.721002', 'zoom': 10, 'disableDefaultUI': true, 'mapTypeId': 'terrain'}).bind('init', function(event, map) { 
        $('#map_canvas').gmap('getCurrentPosition', function(results, status) {
            if ( status === 'OK' ) {
                var position = new google.maps.LatLng(results.coords.latitude, results.coords.longitude);
                var marker = $('#map_canvas').gmap('get', 'markers > client' );
                if ( !marker ) {
                    $('#map_canvas').gmap('addMarker', { 'id': 'client', 'position': position, 'bounds': true });
                } else {
                    marker.setPosition(position);
                    map.panTo(position);
                }
            } else if ( status === 'NOT_SUPPORTED' ) {
                $('#map_canvas').gmap('addMarker', { 'id': 'client', 'position': $('#map_canvas').gmap('get', 'map').getCenter(), 'bounds': true });
            }
        });
        $('#map_canvas').gmap('loadFusion', { 'query': { 'select': 'Geocodable address', 'from': 1260763 } } );
    });  

});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜