我想要从我的位置获取谷歌方向到点击地图上的标记

时间:2013-03-20 17:40:09

标签: javascript google-maps

src="http://www.google.com/jsapi?key=AIzaSyBkplC2WJePplXwHeSeiYCPh1QtHrLE_Oo"
if (navigator.geolocation) {enableHighAccuracy: true,
    navigator.geolocation.watchPosition(function(position){
        var latitude = position.coords.latitude;
        var longitude = position.coords.longitude;
        var coords = new google.maps.LatLng(latitude, longitude);
        var mapOptions = {
            zoom: 13,
            center: coords,
            mapTypeControl: true,
            navigationControlOptions: {
                style: google.maps.NavigationControlStyle.SMALL
            },
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById("mapContainer"), mapOptions);
        //mylocation as a marker on the map
        var marker1 = new google.maps.Marker({
            position: coords,
            draggable: false,
            map: map,
            title: "Your current location!"
        });
        //the circle radius on the marker 
        var myCity = new google.maps.Circle({
            center:coords,
            radius:2000,
            strokeColor:"#0000FF",
            strokeOpacity:0.5,
            strokeWeight:1,
            fillColor:"#0000FF",
            fillOpacity:0.1
        });

        myCity.setMap(map);
    });
    enableHighAccuracy: true;
}else {
    alert("Geolocation API is not supported in your browser.");
}

1 个答案:

答案 0 :(得分:0)

此工作示例HERE可能会回答您的问题。尝试点击给定地图上的任何标记,它将为您提供当前位置的方向。