Update Google map by latitude and longitude form

后端 未结 4 1694
小鲜肉
小鲜肉 2021-02-06 19:32

So I am building a HTML page, in which i need a Google Map and a simple form which consists of two fields latitude and longitude.




        
4条回答
  •  名媛妹妹
    2021-02-06 20:26

    For anyone who will come later, suppose you want to update your map latitudes and longitudes from an array(like in case you have a list of cities), you can try this:

    Globaly declare your variables and array

    var latlon;
    var map;
    var coords = {
    'agra': '27.1767, 78.0081',
    'noida': '28.5355, 77.3910',
    'kanpur':'26.4499, 80.3319',
    };
    

    Add a function that will work on your selects on change, so that when you change a city, the map automatically changes.

    function firemapChange(value)
    {
    var c = coords[value].split(',');
    var latlon = new google.maps.LatLng(c[0], c[1]);
    // map = new google.maps.Map(document.getElementById('mapdiv'), {
    //center: {lat: position.coords.latitude, lng: position.coords.longitude},
    // center: latlon,
    //  zoom: 13,
    //  mapTypeId: 'roadmap'
    map.panTo(latlon);
    }
    

    Why i have commented so many lines? It is because i was accidentally creating a new map object. Since my map also had a search box, the search box disappeared and all its functionality was lost. So, it is better to avoid creating a new map, and create a global variable

    finally in your HTML, you do something like this:

    
    
    
      
                            
        
    提交评论

提交回复
热议问题