Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Google Maps API JS-从另一个JS文件访问标记位置_Javascript_Html_Google Maps_Google Maps Api 3_Global - Fatal编程技术网

Javascript Google Maps API JS-从另一个JS文件访问标记位置

Javascript Google Maps API JS-从另一个JS文件访问标记位置,javascript,html,google-maps,google-maps-api-3,global,Javascript,Html,Google Maps,Google Maps Api 3,Global,我需要使用标记放置的位置,以便能够通过另一个API加载天气数据。我让我的地图放置一个加载和放置一个标记以及一个对象“myPosition”,它应该包含标记的纬度和经度。我的问题是使用标记的坐标更新myPosition.lat和.lng。下面是我到目前为止的代码,我能够从另一个文件中的myPosition获取初始化数据,但我不知道如何获取坐标。这是我第一次使用HTML和JS,我通常使用C++或java,所以它对我来说有点改变。p> var myPosition = { lat: 0, lng:

我需要使用标记放置的位置,以便能够通过另一个API加载天气数据。我让我的地图放置一个加载和放置一个标记以及一个对象“myPosition”,它应该包含标记的纬度和经度。我的问题是使用标记的坐标更新myPosition.lat和.lng。下面是我到目前为止的代码,我能够从另一个文件中的myPosition获取初始化数据,但我不知道如何获取坐标。这是我第一次使用HTML和JS,我通常使用C++或java,所以它对我来说有点改变。p>
var myPosition = 
{
lat: 0,
lng: 0
}; 

function initMap()
{
var texas = {lat: 32.7767, lng: -96.7970};
var map = new google.maps.Map(document.getElementById('map'), 
{
    zoom: 10,
    center: texas
});

var marker = new google.maps.Marker({
    position: texas,
    map: map
});

google.maps.event.addListener(map, 'click', function(event)
{
    placeMarker(event.latLng);
});

function placeMarker(location)
{ 
    if (marker == undefined)
    {
        marker = new google.maps.Marker(
        {
            position: location,
            map: map, 
            animation: google.maps.Animation.DROP,
        });
    }
    else
    {
        marker.setPosition(location);
    }

map.setCenter(location);
}
}

听起来,您需要考虑的是,在您的“其他文件”访问坐标时,少一点考虑问题,而多一点考虑通过回调通知您的其他模块已在地图上单击鼠标

例如在
weather.js中

function handleCoords(latlng){
   //...Do whatever you want with latlng
}

// Export function as needed if you're using modules/webpack.
// (don't worry if you don't know what this means)
然后在您的
main.js
中嵌入HTML嵌入js

// import {handleCoords} from './weather'
// Again ignore this if you're not using modules/don't know what this means.

google.maps.event.addListener(map, 'click', function(event){
    placeMarker(event.latLng);
    handleCoords(event.latLng); // This is how you pass the latlng 
                                       // along to weather.js 

});

您的代码片段不完整。myPosition已定义且未使用。我读了两遍你的问题,仍然不清楚你在问什么。这可能是一个语言问题,但请尝试澄清。“另一个文件”是什么意思?我有另一个js文件“weather.js”,我想用“myPosition”。这个想法是点击地图上的一个位置并从该位置加载天气数据。我的问题是我无法从weather.js中的标记获取位置数据