使用Google Maps API在用户当前位置设置标记?

时间:2012-10-16 21:12:05

标签: javascript api google-maps

我想在以下脚本中的用户当前位置设置标记。

任何人都可以使用正确的代码向我提供实际使用此脚本的代码吗?

我从Google文档中尝试了一些,没有任何运气。

谢谢

    <html>
    <head>
    <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">

    var showPosition = function (position) {
    var userLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
    // Do whatever you want with userLatLng.
    }

    navigator.geolocation.getCurrentPosition(showPosition); 

    var directionDisplay;
    var map;


function initialize() {
    directionsDisplay = new google.maps.DirectionsRenderer();
    var copenhagen = new google.maps.LatLng(55.6771, 12.5704);
    var myOptions = {
        zoom:12,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: copenhagen
    }

    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    directionsDisplay.setMap(map);
    }


    var directionsService = new google.maps.DirectionsService();



    function calcRoute() {
    var start = document.getElementById("start").value;
    var end = document.getElementById("end").value;
    var distanceInput = document.getElementById("distance");

    var request = {
        origin:start,
        destination:end,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };

    directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
            distanceInput.value = response.routes[0].legs[0].distance.value / 1000;
        }
    });
    }


    </script>

    <title>Distance Calculator</title>

    <style type="text/css">

            body {
                font-family:Helvetica, Arial;
            }
            #map_canvas {
                height: 50%;
            }
    </style>
    </head>
    <body>

    <body onload="initialize()">
    <p>Enter your location and desired destination to calculate the distance</p>
        <div>
            <p>
                <label for="start">Start: </label>
                <input type="text" name="start" id="start" /><br />

                <label for="end">End:</label>
                <input type="text" name="end" id="end" /><br />

                <input type="submit" value="Calculate Route" onclick="calcRoute()" />
            </p>
            <p>
                <label for="distance">Distance (km): </label>
                <input type="text" name="distance" id="distance" readonly="true" />
            </p>
        </div>
        <div id="map_canvas"></div>
    </body>
</html>

2 个答案:

答案 0 :(得分:0)

您可以从其IP地址获取用户的位置(近似值)。查看这些资源:

http://www.geoplugin.net/php.gp

http://isithackday.com/hacks/geo/yql-geo-library/

http://www.maxmind.com/app/geolitecity(可下载的数据库)

从上述某个网站获取经度和纬度后,

var location = new google.maps.LatLng(40.779502, -73.967857);
marker = new google.maps.Marker({
        position: location,
        map: map
    });

答案 1 :(得分:0)

您可以像在尝试的那样使用HTML5中的地理定位功能,但是您需要确保在初始化地图之前不要求地理定位。因此,将您的通话移至navigator.geolocation.getCurrentPosition(showPosition);initialize()功能的末尾。

此外,您需要为用户的位置创建一个新标记,并将地图指定给该标记:

var showPosition = function (position) {
    var userLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
    // Do whatever you want with userLatLng.
    var marker = new google.maps.Marker({
        position: userLatLng,
        title: 'Your Location',
        map: map
    });
}