新的谷歌地图:不是的LatLng一些,但我认为这是

问题描述 投票:-1回答:2

试图得到这个工作 - https://clinicalthermography.co.nz/test_map.php

例如第一位置日志,

"Object
Lat: -36.768498
Lng: 174.75895"

但错误说LAT不是一个数字。而不是盲目地拨弄,这牛逼想明白为什么它不是和它是什么:-)

var map, marker;

function initMap() {
    geocoder = new google.maps.Geocoder();

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

    markAllLocations ();
}

function markAllLocations () {
    var 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]
    ];

    for (let count = 0; count < locations.length; count++) {
        let myPos = {                     
                Lat : locations[count][2],
                Lng : locations[count][3]
        };
        console.log(
            count + '  ' + locations[count][0] 
        );
        console.log(myPos);

        marker = new google.maps.Marker(
            { map : map,
              position: myPos,  
              title: locations[count][0] 
            }
        );
    }
}
javascript google-maps google-maps-markers
2个回答
0
投票

Marker()需要谷歌地图API传递与positionlng按键,都具有数字值的lat对象。你的情况,你必须拥有myPosLat(大写)的对象Lng,和你得到,因为myPos.lat === undefined错误。


0
投票

有,你有可能现在想通了,在拼写小discrepency这是导致错误您所遇到。正确的拼写是latlng〜所有小写,而不是资本。这就是说,通过使用parseFloat(var)或使用谷歌地图的方法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>
© www.soinside.com 2019 - 2024. All rights reserved.