Javascript 新谷歌地图:LatLng不是一个数字,但我认为它是 试着让它工作-

Javascript 新谷歌地图:LatLng不是一个数字,但我认为它是 试着让它工作-,javascript,google-maps,google-maps-markers,Javascript,Google Maps,Google Maps Markers,e、 g.第一个位置记录为 "Object Lat: -36.768498 Lng: 174.75895" 但错误表明lat不是一个数字。与其盲目地摆弄,这个新手想了解为什么它不是,它是什么:-) var映射,标记; 函数initMap(){ geocoder=新的google.maps.geocoder(); var centerOfNZ={lat:-40.8,液化天然气:173.0}; map=new google.maps.map(document.getElementById('map

e、 g.第一个位置记录为

"Object
Lat: -36.768498
Lng: 174.75895"
但错误表明lat不是一个数字。与其盲目地摆弄,这个新手想了解为什么它不是,它是什么:-)
var映射,标记;
函数initMap(){
geocoder=新的google.maps.geocoder();
var centerOfNZ={lat:-40.8,液化天然气:173.0};
map=new google.maps.map(document.getElementById('map'){
缩放:5.9,
中心:centerOfNZ
});
标记所有位置();
}
函数markAllLocations(){
变量位置=[
[Auckland',Milford东海岸路50号Milford健康诊所,-36.768498174.75895],
[“基督城”,“乔治,基督城公园露台50号”,-43.525776172.628387],
[《达尼丁》、《乔治上的极光》,达尼丁乔治街678号,-45.87625170.502548],
['Hamilton','Medcom综合健康诊所,32 O'Neill St,Claudelands,Hamilton',-37.781234175.288198],
[Hawke's Bay',“医生绿草地,纳皮尔绿草地肯尼迪路524号”,-39.5229328176.8685695],
['Invercargill','The Quest服务式公寓,10 Dee St,Cnr Dee and Tay Streets,Invercargill',-46.4131866,168.3537731],
['Nelson','Delorenzos公寓,特拉法加街43-55号,纳尔逊伍德',-41.267575173.287417],
[‘新普利茅斯’,‘新普利茅斯考特尼和利奇街北卡罗来纳州普利茅斯优质酒店’,-39.061173,174.06889],
[Palmerston North'、“Palmerston North Fitzherbert大道101号康沃尔汽车旅馆”、-40.3596103、175.6141897],
[Queenstown',“Queenstown杜克街5号一层”,-45.03135700168.65935960],
['Tauranga','1416A Cameron Rd Greerton,Tauranga',-37.729565176.129608],
['Wanaka','C/-Janice Cleghorn,143 Hunter Cres,Wanaka',-44.697945169.167267],
[《惠灵顿》、《索顿探索》,61-63索顿码头,索顿,惠灵顿,-41.2760045174.7813852],
['Whangarei','Whangarei酒店,9 Riverside Drive,Riverside,Whangarei',-35.723466174.327632]
];
for(让count=0;count
谷歌地图API
标记()
需要传递一个带有
lng
lat
键的
位置
对象,这两个键都有数字值。在你的例子中,你有一个对象
myPos
,它有
Lat
Lng
(大写),你得到了一个错误,因为
myPos.Lat==未定义的

,正如你现在可能已经发现的那样,拼写上有一个小小的差异,导致了你所经历的错误。正确的拼写是
lat
lng
~都是小写,而不是大写。也就是说,当使用
parseFloat(var)
或使用google地图方法
new google.maps.LatLng(_LAT,_LNG)

因为我有一点空闲时间,所以我稍微玩弄了一下原始代码,您将在下面看到——希望它能有所帮助

<!DOCTYPE html>
<html>
    <head>
        <title>Google Maps: New Zealand - Clinical Thermography</title>
        <meta charset="utf-8" />
        <style>
            body,
            html { height:100%;margin:0;padding:0;box-sizing:border-box; }
            #map { width:90%;height:90vh; margin:auto;float:none; }
            /* hide infowindow & pre-format content */
            #info{display:none; padding:1rem;margin:0.25rem;border:1px solid pink;background:white;}
            #info div{margin:0.5rem auto;}
            #info div:before{content:attr(data-label)":";text-transform:capitalize;font-weight:bold;color:black;margin:0 0.5rem 0 0;}
            #info div:after{content:attr(data-value);color:pink;}
        </style>
        <script>
            let map;
            let marker;
            let infoWindow;
            let geocoder;
            let content;

            function initMap() {
                const centerOfNZ = {
                        lat: -40.8,
                        lng: 173.0
                    };
                map = new google.maps.Map( document.getElementById('map'), {
                    zoom: 5.9,
                    center: centerOfNZ
                });

                /* The Geocoder is not used */
                //geocoder = new google.maps.Geocoder();


                infoWindow = new google.maps.InfoWindow({ maxWidth:350, disableAutoPan:false });
                infoWindow.setContent( document.getElementById('info') );

                markAllLocations();
            }

            function markAllLocations () {
                let locations = [
                    ['Auckland',            'Milford Health Clinic, 50 East Coast Road, Milford', -36.768498, 174.75895],
                    ['Christchurch',        'The George, 50 Park Terrace, Christchurch', -43.525776, 172.628387],
                    ['Dunedin',             'Aurora on George, 678 George St,   Dunedin', -45.876251, 170.502548],
                    ['Hamilton ',           'Medcom Integrative Health Clinic, 32 O\'Neill St, Claudelands,  Hamilton', -37.781234, 175.288198],
                    ['Hawke\'s Bay',        'The Doctors Greenmeadows, 524 Kennedy Rd,  Greenmeadows,  Napier', -39.5229328, 176.8685695],
                    ['Invercargill',        'The Quest Serviced Apartments, 10 Dee St,  Cnr Dee and Tay Streets,  Invercargill', -46.4131866, 168.3537731],
                    ['Nelson',              'Delorenzos Apartments, 43\-55 Trafalgar St,  The Wood,  Nelson', -41.267575, 173.287417],
                    ['New Plymouth',        'Quality Hotel Plymouth, Cnr of Courtney and Leach St,  New Plymouth', -39.061173, 174.06889],
                    ['Palmerston North',    'Cornwall Motor Lodge, 101 Fitzherbert Avenue,  Palmerston North', -40.3596103, 175.6141897],
                    ['Queenstown',          'Level One, 5 Duke St,  Queenstown', -45.03135700, 168.65935960],
                    ['Tauranga',            '1416A Cameron Rd Greerton,  Tauranga', -37.729565, 176.129608],
                    ['Wanaka',              'C/- Janice Cleghorne, 143 Hunter Cres,  Wanaka', -44.697945,169.167267],
                    ['Wellington',          'The Quest on Thorndon, 61\-63 Thorndon Quay,  Thorndon,  Wellington', -41.2760045, 174.7813852],
                    ['Whangarei',           'Distinction Whangarei Hotel, 9 Riverside Drive,  Riverside,  Whangarei', -35.723466, 174.327632]
                ];

                /* easier to use `forEach` as the source is an array */
                locations.forEach( location => {
                    /*
                        use `parseFloat` to cast the string to a number/float
                        Alternatively, use `new google.maps.LatLng(location[2],location[3])` 
                        which will do it all nicely for you
                    */
                    marker = new google.maps.Marker({ 
                        map : map,
                        title: location[0],
                        description:location[1],
                        position: {
                            lat: parseFloat( location[2] ),
                            lng: parseFloat( location[3] )
                        }
                    });
                    /* show info for each marker when clicked */
                    google.maps.event.addListener( marker, 'click', function(e){
                        /* get a reference to the infowindow content */
                        content=infoWindow.getContent();
                        /* ensure the content is visible */
                        content.style.display='block';
                        /* add details from marker to the content */
                        content.querySelector('[data-label="name"]').dataset.value=this.title;
                        content.querySelector('[data-label="description"]').dataset.value=this.description;
                        content.querySelector('[data-label="co-ordinates"]').dataset.value=[this.position.lat(),this.position.lng()].join(', ');
                        /* display content */
                        infoWindow.setPosition( e.latLng );
                        infoWindow.open( map );
                    });
                });

            }
        </script>
        <script src='//maps.googleapis.com/maps/api/js?key=APIKEY&callback=initMap' async defer></script>
    </head>
    <body>
      <div id='map'></div>
      <!-- use a hidden element as the content for the infoWindow content -->
      <div id='info'>
        <div data-label='name' data-value=''></div>
        <div data-label='description' data-value=''></div>
        <div data-label='co-ordinates' data-value=''></div>
      </div>
    </body>
</html>

谷歌地图:新西兰-临床热成像
身体,
html{高度:100%;边距:0;填充:0;框大小:边框框;}
#地图{宽度:90%;高度:90vh;边距:自动;浮动:无;}
/*隐藏信息窗口和预格式化内容*/
#信息{显示:无;填充:1rem;边距:0.25rem;边框:1px纯粉色;背景:白色;}
#信息分区{边距:0.5rem自动;}
#info div:before{content:attr(数据标签)”:“文本转换:大写;字体大小:粗体;颜色:黑色;边距:0 0.5rem 0;}
#info div:after{content:attr(数据值);color:pink;}
让地图;
让标记;
让信息窗口;
让地理编码器;
让内容;
函数initMap(){
常数Z={
纬度:-40.8,
液化天然气:173.0
};
map=new google.maps.map(document.getElementById('map'){
缩放:5.9,
中心:centerOfNZ
});
/*没有使用地理编码器*/
//geocoder=新的google.maps.geocoder();
infoWindow=new google.maps.infoWindow({maxWidth:350,disableAutoPan:false});
setContent(document.getElementById('info');
标记所有位置();
}
函数markAllLocations(){
让位置=[
['Auckland','Milford东海岸路50号Milford健康诊所',-36.768498174.75895],
[“基督城”,“乔治,基督城公园露台50号”,-43.525776172.628387],
[《达尼丁》、《乔治上的极光》,达尼丁乔治街678号,-45.87625170.502548],
['Hamilton','Medcom综合健康诊所,32 O'Neill St,Claudelands,Hamilton',-37.781234175.288198],
[Hawke's Bay',“医生绿草地,纳皮尔绿草地肯尼迪路524号”,-39.5229328176.8685695],
['Invercargill','The Quest服务式公寓,10 Dee St,Cnr Dee and Tay Streets,Invercargill',-46.4131866,168.3537731],
['Nelson','Delorenzos A
<!DOCTYPE html>
<html>
    <head>
        <title>Google Maps: New Zealand - Clinical Thermography</title>
        <meta charset="utf-8" />
        <style>
            body,
            html { height:100%;margin:0;padding:0;box-sizing:border-box; }
            #map { width:90%;height:90vh; margin:auto;float:none; }
            /* hide infowindow & pre-format content */
            #info{display:none; padding:1rem;margin:0.25rem;border:1px solid pink;background:white;}
            #info div{margin:0.5rem auto;}
            #info div:before{content:attr(data-label)":";text-transform:capitalize;font-weight:bold;color:black;margin:0 0.5rem 0 0;}
            #info div:after{content:attr(data-value);color:pink;}
        </style>
        <script>
            let map;
            let marker;
            let infoWindow;
            let geocoder;
            let content;

            function initMap() {
                const centerOfNZ = {
                        lat: -40.8,
                        lng: 173.0
                    };
                map = new google.maps.Map( document.getElementById('map'), {
                    zoom: 5.9,
                    center: centerOfNZ
                });

                /* The Geocoder is not used */
                //geocoder = new google.maps.Geocoder();


                infoWindow = new google.maps.InfoWindow({ maxWidth:350, disableAutoPan:false });
                infoWindow.setContent( document.getElementById('info') );

                markAllLocations();
            }

            function markAllLocations () {
                let locations = [
                    ['Auckland',            'Milford Health Clinic, 50 East Coast Road, Milford', -36.768498, 174.75895],
                    ['Christchurch',        'The George, 50 Park Terrace, Christchurch', -43.525776, 172.628387],
                    ['Dunedin',             'Aurora on George, 678 George St,   Dunedin', -45.876251, 170.502548],
                    ['Hamilton ',           'Medcom Integrative Health Clinic, 32 O\'Neill St, Claudelands,  Hamilton', -37.781234, 175.288198],
                    ['Hawke\'s Bay',        'The Doctors Greenmeadows, 524 Kennedy Rd,  Greenmeadows,  Napier', -39.5229328, 176.8685695],
                    ['Invercargill',        'The Quest Serviced Apartments, 10 Dee St,  Cnr Dee and Tay Streets,  Invercargill', -46.4131866, 168.3537731],
                    ['Nelson',              'Delorenzos Apartments, 43\-55 Trafalgar St,  The Wood,  Nelson', -41.267575, 173.287417],
                    ['New Plymouth',        'Quality Hotel Plymouth, Cnr of Courtney and Leach St,  New Plymouth', -39.061173, 174.06889],
                    ['Palmerston North',    'Cornwall Motor Lodge, 101 Fitzherbert Avenue,  Palmerston North', -40.3596103, 175.6141897],
                    ['Queenstown',          'Level One, 5 Duke St,  Queenstown', -45.03135700, 168.65935960],
                    ['Tauranga',            '1416A Cameron Rd Greerton,  Tauranga', -37.729565, 176.129608],
                    ['Wanaka',              'C/- Janice Cleghorne, 143 Hunter Cres,  Wanaka', -44.697945,169.167267],
                    ['Wellington',          'The Quest on Thorndon, 61\-63 Thorndon Quay,  Thorndon,  Wellington', -41.2760045, 174.7813852],
                    ['Whangarei',           'Distinction Whangarei Hotel, 9 Riverside Drive,  Riverside,  Whangarei', -35.723466, 174.327632]
                ];

                /* easier to use `forEach` as the source is an array */
                locations.forEach( location => {
                    /*
                        use `parseFloat` to cast the string to a number/float
                        Alternatively, use `new google.maps.LatLng(location[2],location[3])` 
                        which will do it all nicely for you
                    */
                    marker = new google.maps.Marker({ 
                        map : map,
                        title: location[0],
                        description:location[1],
                        position: {
                            lat: parseFloat( location[2] ),
                            lng: parseFloat( location[3] )
                        }
                    });
                    /* show info for each marker when clicked */
                    google.maps.event.addListener( marker, 'click', function(e){
                        /* get a reference to the infowindow content */
                        content=infoWindow.getContent();
                        /* ensure the content is visible */
                        content.style.display='block';
                        /* add details from marker to the content */
                        content.querySelector('[data-label="name"]').dataset.value=this.title;
                        content.querySelector('[data-label="description"]').dataset.value=this.description;
                        content.querySelector('[data-label="co-ordinates"]').dataset.value=[this.position.lat(),this.position.lng()].join(', ');
                        /* display content */
                        infoWindow.setPosition( e.latLng );
                        infoWindow.open( map );
                    });
                });

            }
        </script>
        <script src='//maps.googleapis.com/maps/api/js?key=APIKEY&callback=initMap' async defer></script>
    </head>
    <body>
      <div id='map'></div>
      <!-- use a hidden element as the content for the infoWindow content -->
      <div id='info'>
        <div data-label='name' data-value=''></div>
        <div data-label='description' data-value=''></div>
        <div data-label='co-ordinates' data-value=''></div>
      </div>
    </body>
</html>