希望从其他函数

时间:2015-10-08 07:50:11

标签: javascript google-maps google-maps-api-3 maps

我已经初始化了一个带有信息框的地图,现在当我尝试从另一个函数访问地图变量时(点击链接获取位置捕获我的位置)我得到的地图未定义我明白它之前执行变量可用,但我还没有找到出路。 任何指针/帮助将不胜感激,谢谢你提前

jQuery( document ).ready(function($) {
    var script = document.createElement('script');
    script.src = "http://maps.googleapis.com/maps/api/js?sensor=false";
    // document.body.appendChild(script);
	var map;
	initialize();

    });   
	  function showPosition(){
		  // Try HTML5 geolocation
		  if(navigator.geolocation) {
			navigator.geolocation.getCurrentPosition(function(position) {
			var map;
			var bounds = new google.maps.LatLngBounds();
			
			var pos = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
			//jQuery('#findMe').data('pos',pos);
				
				marker = new google.maps.Marker({   
					map: map,
					position: pos,
					zoom:7

				});
				// infowindow = new google.maps.InfoWindow({
					// map: map,
					// position: pos,
					// content: 'You Are Here'
				 // });
				 map.setCenter(pos);
				 
			}, function() {
			  handleNoGeolocation(true);
			});
		  } else {
			// Browser doesn't support Geolocation
			handleNoGeolocation(false);
		  }
		  return false;
    }
   function initialize() {
    var map;
    var bounds = new google.maps.LatLngBounds();
    var mapOptions = {
        mapTypeId: 'roadmap',
		zoom: 8
    };
	
	
    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
    map.setTilt(45);
      
    var markers = [['',35.2152241,-80.84801900000002,''],
['',35.1995636,-80.82525329999999,''],
['',35.1585635,-80.79616499999997,''],
['',35.201245,-80.86516999999998,''],
['',35.19604558956503,-80.84856033325195,''],
['',35.197692,-80.82809299999997,''],
['',35.0658382,-80.76834389999999,''],
['',35.1786109,-80.851946,' '],
['',35.1304041,-80.8324773,''],
['',35.201245,-80.86516999999998,'  '],
['',35.2136068,-80.79125290000002,' '],
];
 
var infoWindowContent = [['<div class="info_content">test content<\/div>'],
['<div class="info_content">test content<\/div>'],
['<div class="info_content">test content<\/div>'],
['<div class="info_content">test content<\/div>'],
['<div class="info_content">test content<\/div>'],
['<div class="info_content">test content<\/div>'],
['<div class="info_content">test content<\/div>'],
['<div class="info_content">test content<\/div>'],
['<div class="info_content">test content<\/div>'],
['<div class="info_content">test content<\/div>'],
];
    var infoWindow = new google.maps.InfoWindow(), marker, i;

    for( i = 0; i < markers.length; i++ ) {
        var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
        var itype = markers[i][3];
        bounds.extend(position);
        marker = new google.maps.Marker({
            position: position,
            map: map,
            title: markers[i][0]
        });
          
        google.maps.event.addListener(marker, 'click', (function(marker, i) {
            return function() {
                infoWindow.setContent(infoWindowContent[i][0]);
                infoWindow.open(map, marker);
            }
        })(marker, i));

        map.fitBounds(bounds);
    }
    }
		
#map_canvas{
width:100%;height:400px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.0&sensor=false"></script>

<a href='#' class='detectLoc' onclick="return showPosition()">Get My lcoation</a>
    <div id="map_canvas" class="mapping"></div>

1 个答案:

答案 0 :(得分:0)

您需要有一个全局变量来存储“地图”。此外,您还在每个函数中声明了变量映射。这将影响全局变量,并且将始终使用本地变量,从而导致未定义。在下面的代码中,我在函数 showPosition() initialize()

中添加了一个全局映射变量和注释的map变量重新声明。
var map;    // global map variable

jQuery( document ).ready(function($) {
        var script = document.createElement('script');
        script.src = "http://maps.googleapis.com/maps/api/js?sensor=false";
        // document.body.appendChild(script);
        var map;
        initialize();

        });   
          function showPosition(){
              // Try HTML5 geolocation
              if(navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(function(position) {
                //var map;
                var bounds = new google.maps.LatLngBounds();

                var pos = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
                //jQuery('#findMe').data('pos',pos);

                    marker = new google.maps.Marker({   
                        map: map,
                        position: pos,
                        zoom:7

                    });
                    // infowindow = new google.maps.InfoWindow({
                        // map: map,
                        // position: pos,
                        // content: 'You Are Here'
                     // });
                     map.setCenter(pos);

                }, function() {
                  handleNoGeolocation(true);
                });
              } else {
                // Browser doesn't support Geolocation
                handleNoGeolocation(false);
              }
              return false;
        }
       function initialize() {
        //var map;
        var bounds = new google.maps.LatLngBounds();
        var mapOptions = {
            mapTypeId: 'roadmap',
            zoom: 8
        };


        map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
        map.setTilt(45);

        var markers = [['',35.2152241,-80.84801900000002,''],
    ['',35.1995636,-80.82525329999999,''],
    ['',35.1585635,-80.79616499999997,''],
    ['',35.201245,-80.86516999999998,''],
    ['',35.19604558956503,-80.84856033325195,''],
    ['',35.197692,-80.82809299999997,''],
    ['',35.0658382,-80.76834389999999,''],
    ['',35.1786109,-80.851946,' '],
    ['',35.1304041,-80.8324773,''],
    ['',35.201245,-80.86516999999998,'  '],
    ['',35.2136068,-80.79125290000002,' '],
    ];

    var infoWindowContent = [['<div class="info_content">test content<\/div>'],
    ['<div class="info_content">test content<\/div>'],
    ['<div class="info_content">test content<\/div>'],
    ['<div class="info_content">test content<\/div>'],
    ['<div class="info_content">test content<\/div>'],
    ['<div class="info_content">test content<\/div>'],
    ['<div class="info_content">test content<\/div>'],
    ['<div class="info_content">test content<\/div>'],
    ['<div class="info_content">test content<\/div>'],
    ['<div class="info_content">test content<\/div>'],
    ];
        var infoWindow = new google.maps.InfoWindow(), marker, i;

        for( i = 0; i < markers.length; i++ ) {
            var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
            var itype = markers[i][3];
            bounds.extend(position);
            marker = new google.maps.Marker({
                position: position,
                map: map,
                title: markers[i][0]
            });

            google.maps.event.addListener(marker, 'click', (function(marker, i) {
                return function() {
                    infoWindow.setContent(infoWindowContent[i][0]);
                    infoWindow.open(map, marker);
                }
            })(marker, i));

            map.fitBounds(bounds);
        }
        }