谷歌地理编码器服务没有使用javascript返回适当的lat和long值

时间:2014-03-27 14:30:36

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

Google地图未显示确切位置。我从我们的客户处获取地址并将其显示在地图上。我交叉检查谷歌拉特和我的长,它没有返回确切的值。这是我的代码,如果我错了,请指导我。

function callMap() {

    var fullAddress = address + "," + city + "," + state + "," + zip;
    var lat_Company = "";
    var lng_Company = "";
    geocoder.geocode({
        'address': fullAddress
    }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            lat_Company = parseFloat(results[0].geometry.location.lat());
            lng_Company = parseFloat(results[0].geometry.location.lng());
            var lCompanyObject = new Object();
            lCompanyObject.Name = companyDetails.CompanyName;
            lCompanyObject.Description = address + "<br/>" + city + ", " + state + "," + zip;
            lCompanyObject.FullAddress = address + ", " + city + ", " + state + "," + zip;
            lCompanyObject.Lat = lat_Company;
            lCompanyObject.Lng = lng_Company;
            displayCompany(city, state, "comp_map", "mapinfowindow", lCompanyObject, "fromSideBar");
        }
    });
    var abpoutsideBarBuilder = '<ul>' +
        '<li><div id="map_wrapper1">' +
        '<div id="comp_map1"></div>' +
        '<div id="mapinfowindow"style="display:none" ><b>#name</b><br>#description</div></div></li></ul>';
    $('.about_map_addr_businesshours').html(abpoutsideBarBuilder);
}

function displayCompany(pCity, pState, pMapDiv, mapinfowindow, lCompany, from) {
    var requestLocation = lCompany.FullAddress;
    geocoder.geocode({
        'address': requestLocation
    }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var lat = parseFloat(results[0].geometry.location.lat());
            var lng = parseFloat(results[0].geometry.location.lng());
            if (lat != null && lng != null) {
                centerLat = lat;
                centerLng = lng;
                initDisplayMap(centerLat, centerLng, pMapDiv, mapinfowindow, lCompany, from);
            }
        } else {
            console.error("Geocode was not successful for the following reason ::" + status);
        }
    });
}

function initDisplayMap(pCenterLat, pCenterLng, pMapDiv, mapinfowindow, pCompany, from) {
    if ($("#" + mapinfowindow).length > 0) {
        var latlng = new google.maps.LatLng(pCenterLat, pCenterLng);
        var myOptions = {
            zoom: 14,
            center: latlng,
            disableDefaultUI: true,
            navigationControl: true,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        var map = new google.maps.Map(document.getElementById(pMapDiv), myOptions);

        var infowindow = null;
        infowindow = new google.maps.InfoWindow({});
        var LatLngList = new Array();


        var marker = pCompany;
        var markerHTML = $('#mapinfowindow').clone().html();

        if (from.indexOf("fromSideBar") != -1) {
            markerHTML = markerHTML.replace("#name", marker.Name);
            markerHTML = markerHTML.replace("#description", marker.Description);
        } else if (from.indexOf("fromAboutBar") != -1) {
            markerHTML = markerHTML.replace("#name", marker.Name);
            markerHTML = markerHTML.replace("#description", marker.Description);
        }

        if (marker.Lat != null && marker.Lng != null) {
            var myLatLng = new google.maps.LatLng(marker.Lat, marker.Lng);
            LatLngList[LatLngList.length] = myLatLng;

            var beachMarker = new google.maps.Marker({
                position: myLatLng,
                map: map,
                clickable: true,
                html: markerHTML
            });

            google.maps.event.addListener(beachMarker, 'click', function () {
                infowindow.setContent(this.html);
                infowindow.open(map, this);
            });

            var bounds = new google.maps.LatLngBounds();
            if (LatLngList.length > 1) {
                for (var i = 0, LtLgLen = LatLngList.length; i < LtLgLen; i++) {
                    //  And increase the bounds to take this point
                    bounds.extend(LatLngList[i]);
                }
                //Fit these bounds to the map 
                map.fitBounds(bounds);
                map.setZoom(map.getBoundsZoomLevel(bounds));
                map.setCenter(beachMarker.getPosition());
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

不要把地理编码器混在一起。

  

Nakedcherry Waxing Boutique 南非Parkhurst第六街

这个字符串的粗体部分显然不是地址组件。

期望得到所需的结果就像我期望获得像

这样的查询的位置
  

<强> Dr.Molle 下,柏林,德国

地理编码器获得的结果是南非第6街Parkhurst,结果是正确的。

当您查找地点时,请使用places-textsearch,给定查询的结果将是:-26.1437060,28.0207660