简体   繁体   English

使用Google Maps JavaScript API搜索地址

[英]search an address using google maps javascript API

i have created the following function to show and address in JS: 我创建了以下功能来显示和处理JS:

function showMapForm(idDiv, divWidth, divHeight, idLat, idLng) {
    document.getElementById(idDiv).style.width = divWidth+'px';
    document.getElementById(idDiv).style.height = divHeight+'px';
    if (document.getElementById(idLat).value=='' && document.getElementById(idLng).value=='') {
        //Paris
        iniLat = 48.85455;
        iniLng = 2.358627;
    } else {
        iniLat = document.getElementById(idLat).value;
        iniLng = document.getElementById(idLng).value;
    }
    var latlng = new google.maps.LatLng(iniLat, iniLng);
    var myOptions = {
                    zoom: 15,
                    center: latlng,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                    };
    map = new google.maps.Map(document.getElementById(idDiv), myOptions);

    marker = new google.maps.Marker({
                                        position: new google.maps.LatLng(iniLat, iniLng),
                                        map: map
                                        });

    google.maps.event.addListener(map, 'click', function(eve) {
        marker.setPosition(eve.latLng);
        document.getElementById(idLat).value = marker.getPosition().lat();
        document.getElementById(idLng).value = marker.getPosition().lng();
    });
}

as you can see it requires the id of a div, the width and height desired and the id of the hidden fields where I stock the latitude and longitude, if those values aren't present it will use the latitude and longitude of Paris. 如您所见,它需要div的id,所需的宽度和高度以及我存储纬度和经度的隐藏字段的id,如果不存在这些值,它将使用巴黎的纬度和经度。 What i would like to do is to improve this function so it can accept a string value with an address like "Saint Joseph Street No. 45, Paris" and put a marker if it finds the address. 我想做的就是改进此功能,以便它可以接受带有“巴黎圣约瑟夫街45号”之类地址的字符串值,并在找到该地址时放一个标记。

My question is: do you know if there's a way to get a latitude and longitude from a search string using the google maps API? 我的问题是:您是否知道是否可以使用Google Maps API从搜索字符串中获取经度和纬度? (the first occurrence will do it) (第一次出现会做到这一点)

使用地址解析器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM