javascript 如何从 mapCenter 获取 lat 和 lng 作为单独的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8942911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:05:45  来源:igfitidea点击:

How to get the lat & lng as separate variables from mapCenter

javascriptgoogle-maps-api-3

提问by Sebastian

I have the map center, and comes as a Floating Point number.

我有地图中心,并以浮点数形式出现。

How can I get it as 2 separated variables like this:

我怎样才能将它作为 2 个单独的变量,如下所示:

var lat = ...

var lng = ...

I have the float:

我有浮动:

var NewMapCenter = map.getCenter();

I tried to pass the float toWords(x)but didn't work

我试图通过浮动toWords(x)但没有用

I tried to subtract the (,)and splitit, but didn't work

我试图减去(,)split,但没有奏效

var latlngStr = (StrNewMapCenter.substr(1, (StrNewMapCenter.length-1))).split(",",2);
var NewMapCenter = parseFloat(latlngStr[0]);

I tried, this but comes as an event, but I need it without needing to click.

我试过了,但这只是一个事件,但我不需要点击就需要它。

google.maps.event.addListener(map, 'click', function(event) {
    var myLatLng = event.latlng;
    var Newlat = position.coords.latitude;
    var Newlng = position.coords.longitude;
}

Thanks Sebastian

谢谢塞巴斯蒂安

回答by Ramesh Kotha

var NewMapCenter = map.getCenter();

will give you LatLng object, so you can call

会给你 LatLng 对象,所以你可以打电话

var latitude = NewMapCenter.lat();
var longitude = NewMapCenter.lng();