【问题标题】:Inserting varianble GPS coordinates in google maps在谷歌地图中插入可变 GPS 坐标
【发布时间】:2013-04-17 13:26:18
【问题描述】:

我希望我的脚本从 GPS(手机)获取纬度和经度坐标,然后将它们插入谷歌地图并显示手机的位置。好吧,我能够从 GPS 中检索经纬度坐标,但它不会将它们作为显示的地图的中心插入。我的代码:

<script>
    $(document).ready(function(){
        navigator.geolocation.getCurrentPosition(useposition); 
    });
                function useposition(position){  
            lat = position.coords.latitude;
            lon = position.coords.longitude;   
            $("#location").html('Lat: ' + lat + '<br />Lon: ' + lon);

        }
</script><meta name="viewport" content="initial-scale=1.0, user-scalable=no"<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script><script type="text/javascript">
function initialize() {
    var latlng = new google.maps.LatLng(54,-1);
    var settings = {
        zoom: 15,
        center: latlng,
        mapTypeControl: true,
        mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
        navigationControl: true,
        navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
        mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map(document.getElementById("map"), settings);
var marker = new google.maps.Marker({      position: latlng,      map: map,      title:"Yeah buddy!"  })};

                </script>

如您所见,我获取了 GP 坐标,我想将它们插入到我加载的地图中:&lt;body onload="initialize ()" &gt; &lt;/body&gt; 我只是不知道如何从 lat,lon 生成变量以及如何在 gogle maps 中正确插入它。请帮帮我。

【问题讨论】:

    标签: php google-maps gps


    【解决方案1】:

    我假设“在地图中插入坐标”是指在该位置添加一个标记?

    您将遇到的第一个问题是您的map 变量是初始化函数的局部变量。所以你可以把它变成一个全局变量。然后在你的 useposition 函数中你可以简单地创建一个新的标记。

    <script>
        var map;
    
        $(document).ready(function(){
            navigator.geolocation.getCurrentPosition(useposition); 
        });
    
        function useposition(position){  
                lat = position.coords.latitude;
                lon = position.coords.longitude;   
                $("#location").html('Lat: ' + lat + '<br />Lon: ' + lon);
                var marker = new google.maps.Marker({      
                    position: new google.maps.LatLng(lat,lon),      
                    map: map,      
                    title:"Your Mobile"  });
            }
    
    function initialize() {
        var latlng = new google.maps.LatLng(54,-1);
        var settings = {
            zoom: 15,
            center: latlng,
            mapTypeControl: true,
            mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
            navigationControl: true,
            navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
            mapTypeId: google.maps.MapTypeId.ROADMAP 
        }; 
    
        map = new google.maps.Map(document.getElementById("map"), settings);
        var marker = new google.maps.Marker({      position: latlng,      map: map,      title:"Yeah buddy!"  })
    };
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-05
      • 2012-10-18
      相关资源
      最近更新 更多