2013-05-13 83 views
1

下面我得到了google/api地址的long/lat。但经度和latitube变量我需要在哪里使用gmap。正在创建map_canvas。使用函数外部的变量

在下一个函数中如何使用这些变量? (其中长/纬度每写现在的数字)

的Jquery:

 var geocoder = new google.maps.Geocoder(); 
    var address = markerInformation = $('#googleMapLocation').val(); 

    geocoder.geocode({ 'address': address}, function(results, status) { 

    if (status == google.maps.GeocoderStatus.OK) { 
    latitude = results[0].geometry.location.lat(); 
    longitude = results[0].geometry.location.lng(); 
    } 
    }); 
    markerInformation = $('#googleMapInformation').val(); 
      $(function() { 
      demo.add(function() { 
       $('#map_canvas').gmap({'center': '57.7973333,12.0502107', 'zoom': 10, 'disableDefaultUI':true, 'callback': function() { 
        var self = this; 
        self.addMarker({'position': this.get('map').getCenter() }).click(function() { 
         self.openInfoWindow({ 'content': markerInformation }, this); 
        }); 
       }}); 
      }).load(); 

     }); 

回答

0

你能做到这一点有两种方式一种是由之前简单地除去保留字var要把他们变成全局变量变量名。

或由于您使用jQuery,你可以调用该函数的函数

0

里面你可以使用jQuery的data method存储和检索任意数据。

0

使用API​​只需添加标记:

if (status == google.maps.GeocoderStatus.OK) { 
     var latitude = results[0].geometry.location.lat(); 
     var longitude = results[0].geometry.location.lng(); 
     $('#map_canvas').gmap('addMarker', { position: latitude + ',' + longitude }); 
    } 
+0

感谢,做工精细!除了它不在该位置?我在西非得到中心 – Kim 2013-05-13 07:52:07

+0

也许需要切换?位置:经度+','+纬度 – 2013-05-13 15:28:38

+0

可能吧!但是它实际上把标记放在正确位置的地图上,但是它没有集中在位置 – Kim 2013-05-13 17:19:15