Javascript 当我将标记移动到我所在的指定位置时,如何在地图中获取placeMarker的纬度和经度以及我的地址 google.maps.event.addDomListener(窗口“加载”,初始化); var_图; var _originMarker,_destinationMarker; var_地理编码器; 函数初始化() { 变量映射选项={ 缩放:5, 中心:新google.maps.LatLng(21.1289956,82.7791754) }; _map=new google.maps.map(document.getElementById('map-canvas'),mapOptions); _geocoder=新的google.maps.geocoder(); _originMarker=createMarker('search-from'); _destinationMarker=createMarker('search-to'); google.maps.event.addListener(_map,'click',函数(mouseEvent) { 如果((\u activeMarker!=null)和(!\u activeMarker.getMap())placeMarker(\u activeMarker,mouseEvent.latLng); }); } 函数createMarker(_autoComplId) { var _autoCompl=document.getElementById(_autoComplId); var\u newmarker=新的google.maps.Marker({ 位置:新google.maps.LatLng(0,0), 真的, map:null, 自动压缩:\u自动压缩 }); google.maps.event.addListener(_newmarker,“dragend”,function(event) { placeMarker(_newmarker,_newmarker.getPosition()); }); var\u autocomplete=新的google.maps.places.autocomplete(\u autoCompl); _setTypes(['geocode']); google.maps.event.addListener(_autocomplete,'place_changed',function() { var_place=_autocomplete.getPlace(); if(_place.geometry==null)返回; 设置中心和缩放(_place.geometry.location,16); placeMarker(_newmarker,_place.geometry.location); }); 返回新标记; } 函数位置标记(_标记,_位置) { _标记器。设置位置(_位置); 更新地址(_标记); } 函数地址(_标记) { _geocoder.geocode({'latLng':_marker.getPosition()},函数(结果,状态) { if(status==google.maps.GeocoderStatus.OK) { 如果(_marker.getMap()==null)_marker.setMap(_map); _marker.autoCompl.value=结果[0]。格式化的\u地址; } }); } 函数设置中心和缩放(_中心,_缩放) { _地图设置中心(_中心); _map.setZoom(_zoom); } var\u activeMarker=null; 函数setActiveMarker(索引) { 开关(索引) { 案例0: _activeMarker=_originMarker; 打破 案例1: _activeMarker=_destinationMarker; } } 这是我用来检索地址的 现在我想把纬度和经度和我的 地址,在上述函数中为RenewAddress(_标记)

Javascript 当我将标记移动到我所在的指定位置时,如何在地图中获取placeMarker的纬度和经度以及我的地址 google.maps.event.addDomListener(窗口“加载”,初始化); var_图; var _originMarker,_destinationMarker; var_地理编码器; 函数初始化() { 变量映射选项={ 缩放:5, 中心:新google.maps.LatLng(21.1289956,82.7791754) }; _map=new google.maps.map(document.getElementById('map-canvas'),mapOptions); _geocoder=新的google.maps.geocoder(); _originMarker=createMarker('search-from'); _destinationMarker=createMarker('search-to'); google.maps.event.addListener(_map,'click',函数(mouseEvent) { 如果((\u activeMarker!=null)和(!\u activeMarker.getMap())placeMarker(\u activeMarker,mouseEvent.latLng); }); } 函数createMarker(_autoComplId) { var _autoCompl=document.getElementById(_autoComplId); var\u newmarker=新的google.maps.Marker({ 位置:新google.maps.LatLng(0,0), 真的, map:null, 自动压缩:\u自动压缩 }); google.maps.event.addListener(_newmarker,“dragend”,function(event) { placeMarker(_newmarker,_newmarker.getPosition()); }); var\u autocomplete=新的google.maps.places.autocomplete(\u autoCompl); _setTypes(['geocode']); google.maps.event.addListener(_autocomplete,'place_changed',function() { var_place=_autocomplete.getPlace(); if(_place.geometry==null)返回; 设置中心和缩放(_place.geometry.location,16); placeMarker(_newmarker,_place.geometry.location); }); 返回新标记; } 函数位置标记(_标记,_位置) { _标记器。设置位置(_位置); 更新地址(_标记); } 函数地址(_标记) { _geocoder.geocode({'latLng':_marker.getPosition()},函数(结果,状态) { if(status==google.maps.GeocoderStatus.OK) { 如果(_marker.getMap()==null)_marker.setMap(_map); _marker.autoCompl.value=结果[0]。格式化的\u地址; } }); } 函数设置中心和缩放(_中心,_缩放) { _地图设置中心(_中心); _map.setZoom(_zoom); } var\u activeMarker=null; 函数setActiveMarker(索引) { 开关(索引) { 案例0: _activeMarker=_originMarker; 打破 案例1: _activeMarker=_destinationMarker; } } 这是我用来检索地址的 现在我想把纬度和经度和我的 地址,在上述函数中为RenewAddress(_标记),javascript,jquery,html,google-maps,google-maps-api-3,Javascript,Jquery,Html,Google Maps,Google Maps Api 3,iam使用\u geocoder.geocode({'latLng':\u marker.getPosition()},函数(结果,状态) 但是我找不到我刚得到地址的结果 自动填充文本框作为输出,但我无法检索 经纬度 我的RenewAddress()函数版本与您的几乎相同,不同之处在于我以前检索lat/lng值并在控制台中显示它们 <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&a

iam使用
\u geocoder.geocode({'latLng':\u marker.getPosition()},函数(结果,状态)

  • 但是我找不到我刚得到地址的结果 自动填充文本框作为输出,但我无法检索 经纬度

我的
RenewAddress()
函数版本与您的几乎相同,不同之处在于我以前检索lat/lng值并在控制台中显示它们

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script>
<script>
google.maps.event.addDomListener(window, 'load', initialize);

var _map;
var _originMarker, _destinationMarker;
var _geocoder;

function initialize()
{
   var mapOptions = {
        zoom: 5,
        center: new google.maps.LatLng(21.1289956,82.7791754)
    };
    _map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

    _geocoder = new google.maps.Geocoder();
    _originMarker=createMarker('search-from');
    _destinationMarker=createMarker('search-to');


    google.maps.event.addListener(_map, 'click', function(mouseEvent)
    {
        if ((_activeMarker != null) && (!_activeMarker.getMap())) placeMarker(_activeMarker, mouseEvent.latLng);
    });
}





function createMarker(_autoComplId)
{
    var _autoCompl = document.getElementById(_autoComplId);
    var _newmarker = new google.maps.Marker({
        position: new google.maps.LatLng(0, 0),
        draggable: true,
        map: null,
        autoCompl: _autoCompl
    });

    google.maps.event.addListener(_newmarker, "dragend", function(event)
    {
        placeMarker(_newmarker, _newmarker.getPosition());
    });

    var _autocomplete = new google.maps.places.Autocomplete(_autoCompl);
    _autocomplete.setTypes(['geocode']);
    google.maps.event.addListener(_autocomplete, 'place_changed', function()
    {
        var _place = _autocomplete.getPlace();
        if (_place.geometry == null) return;
        setCenterAndZoom(_place.geometry.location, 16);
        placeMarker(_newmarker, _place.geometry.location);
    });

    return _newmarker;  
}

function placeMarker(_marker, _location)
{
    _marker.setPosition(_location);
    RenewAddress(_marker);
}

function RenewAddress(_marker)
{
    _geocoder.geocode({'latLng': _marker.getPosition()}, function(results, status)
    {
        if (status == google.maps.GeocoderStatus.OK)
        {
            if (_marker.getMap() == null) _marker.setMap(_map);
            _marker.autoCompl.value = results[0].formatted_address;
        }
    });
}


function setCenterAndZoom(_center, _zoom)
{
    _map.setCenter(_center);
    _map.setZoom(_zoom);
}

var _activeMarker = null;
function setActiveMarker(index)
{
    switch(index)
    {
        case 0:
            _activeMarker = _originMarker;
            break;
        case 1:
            _activeMarker = _destinationMarker;
    }
}

</script>

请仅使用一个标记以及纬度和经度字段查看。将一些内容写入
城镇/城市/国家
字段,例如Odisha。选择它。在显示该标记后,应设置确切地址以及纬度和经度字段。如果移动标记,信息将更改。

此语法的作用是什么,即您已经开始t围绕着你的两个函数吗?
![…]
?sry我错误地引用了它,我删除了它们。你如何“检索地址”?坐标应该是_marker.getPosition(),您是如何使用它来检索坐标的,是什么让您相信它不起作用的?这是我用来检索地址的脚本,但我无法检索纬度和经度。geocodezip说:仅使用函数
中的
var latlng=_marker.getPosition();
RenewAddress()
latlng.lat()和
latlng.lng()
将检索标记纬度和经度。现在的问题是,您想对这些lat/lng值做什么?只是将它们写入控制台除了调试之外并没有什么用处。所以使用标记
getPosition()
您可以通过相同的方式获取其lat/lng值。查看上面的链接,您会知道我在问什么,您想用地址和lat/lng更新哪个字段?最后一个:
联系人地址
?已更新。您必须将其格式化为您喜欢的格式。我不确定为什么要创建两个标记。移动其中任何一个标记都会更改一个标记地址和lat/lng信息。
function RenewAddress(_marker) {
    console.log('RenewAddress');

    var latlng = _marker.getPosition();
    console.log(latlng.lat());
    console.log(latlng.lng());

    _geocoder.geocode({'latLng': _marker.getPosition()}, function(results, status) {

        if (status == google.maps.GeocoderStatus.OK) {
            console.log('status ok');
            if (_marker.getMap() == null) 
                _marker.setMap(_map);
            _marker.autoCompl.value = results[0].formatted_address;
            console.log(results[0].formatted_address);
            console.log(_marker.autoCompl.value);

            var exactAddress = document.getElementById('search-to');
            exactAddress.value = _marker.autoCompl.value + ', lat/lng: ' + latlng.lat() + ':' + latlng.lng();
        } else {
            console.log('error: ' + status);
        }
    });
}