【问题标题】:How to get latitude and Longitude based in Textbox value如何根据文本框值获取纬度和经度
【发布时间】:2013-12-22 11:34:34
【问题描述】:

如何根据插入文本框中的值获取纬度和经度。

为了在同一张地图中显示一些 Pin 图。

文本框上的值将根据用户位置填充,因此如果他想分享位置,进入我的网站的人将显示在地图中

我希望当用户在文本框中指定纬度和经度时,该 Pin 将显示在地图中。 目前,这不适用于以下脚本。没有从文本框中获取值。

价值纬度:

<input type="text" name="val-lat" value="User Latitude" />

值经度:

<input type="text" name="val-lng" value="User Longitude" />

下面是我使用的脚本:

$(document).ready(function() {

    //------- Google Maps ---------//

    // Creating a LatLng object containing the coordinate for the center of the map
    var latlng = new google.maps.LatLng('val-lat', 'val-lng');

    // Creating an object literal containing the properties we want to pass to the map  
    var options = {  
        zoom: 15, // This number can be set to define the initial zoom level of the map
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP // This value can be set to define the map type ROADMAP/SATELLITE/HYBRID/TERRAIN
    };  
    // Calling the constructor, thereby initializing the map  
    var map = new google.maps.Map(document.getElementById('map_div'), options);  

    // Define Marker properties
    var image = new google.maps.MarkerImage('images/marker.png',
        // This marker is 129 pixels wide by 42 pixels tall.
        new google.maps.Size(129, 42),
        // The origin for this image is 0,0.
        new google.maps.Point(0,0),
        // The anchor for this image is the base of the flagpole at 18,42.
        new google.maps.Point(18, 42)
    );

    // Add Marker
    var marker1 = new google.maps.Marker({
        position: new google.maps.LatLng('val-lat', 'val-lng'), 
        map: map,       
        icon: image // This path is the custom pin to be shown. Remove this line and the proceeding comma to use default pin
    }); 

    // Add listener for a click on the pin
    google.maps.event.addListener(marker1, 'click', function() {  
        infowindow1.open(map, marker1);  
    });

    // Add information window
    var infowindow1 = new google.maps.InfoWindow({  
        content:  createInfo('Evoluted New Media', 'Ground Floor,<br />35 Lambert Street,<br />Sheffield,<br />South Yorkshire,<br />S3 7BH<br /><a href="http://www.evoluted.net" title="Click to view our website">Our Website</a>')
    }); 

    // Create information window
    function createInfo(title, content) {
        return '<div class="infowindow"><strong>'+ title +'</strong><br />'+content+'</div>';
    } 

});

【问题讨论】:

  • 要求代码的问题必须表明对所解决问题的最低理解。包括尝试过的解决方案、它们为什么不起作用以及预期的结果。
  • @AlbusShin 对不起。因此,当我在文本框中插入一个值时,它不起作用,如果我插入纬度和经度手册,它可以完美地工作,但我只能显示一个位置。我认为问题在于它无法获取文本框内的值

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


【解决方案1】:

这里的问题是您没有获得输入值。试试这个:

<input id="userLat" type="text" name="val-lat" value="User Latitude" />
<input id="userLng" type="text" name="val-lng" value="User Longitude" />

然后

var lat = document.getElementById('userLat').value;
var lng = document.getElementById('userLng').value;

var latlng = new google.maps.LatLng(lat, lng);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-25
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多