2013-05-13 85 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