我是编码初学者。希望有人能帮助我。我应该如何使用我检测到的用户坐标和标记坐标来显示使用谷歌地图意图的路线?大多数教程,我发现键在里面的坐标或通过键入地址。我想添加一个显示该功能的按钮。
var markers_data = [{
"id":"7",
"place":"Cafeteria 2",
"nearTo":"Front door of Caferia 2",
"lat":"2.31065941",
"lng":"102.31864929",
"type":"oil"},
{"id":"8",
"place":"Cafeteria 1",
"nearTo":"Back door of Caferia 1",
"lat":"2.31391430",
"lng":"102.31797791",
"type":"oil"}];
var geocoder;
var map;
function initialize() {
//Google map option
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (p) {
var LatLng = new google.maps.LatLng(p.coords.latitude, p.coords.longitude);
var mapOptions = {
center: LatLng, // map center
zoom: 10, //zoom level, 0 = earth view to higher value
panControl: true, //enable pan Control
zoomControl: true, //enable zoom control
zoomControlOptions: {
style: google.maps.ZoomControlStyle.SMALL //zoom control size
},
scaleControl: true, // enable scale control
mapTypeId: google.maps.MapTypeId.ROADMAP // google map type
};
var map = new google.maps.Map(document.getElementById("map"), mapOptions);
var marker = new google.maps.Marker({
position: LatLng,
map: map,
icon: 'images/user-marker.png',
content:"<b>You're here! Your location:</b><br />Latitude: " + p.coords.latitude + "<br />Longitude: " + p.coords.longitude
});
google.maps.event.addListener(marker, "click", function (e) {
var infoWindow = new google.maps.InfoWindow();
infoWindow.setContent(marker.content);
infoWindow.open(map, marker);
});
});
} else {
alert('Geo Location feature is not supported in this browser.');
}
update(markers_data)
}
var bounds = new google.maps.LatLngBounds();
function update(markers) {
var infowindow = new google.maps.InfoWindow(),
marker, i;
for (i = 0; i < markers.length; i++) {
var place = markers[i].place;
var nearTo = markers[i].nearTo;
var lat = markers[i].lat;
var lng = markers[i].lng;
var type = markers[i].type;
var html = "<b>Details:</b><br />Latitude: " + lat + "<br />Longitude: " + lng + "<br />Building: " + place + "<br />Near To: " + nearTo + "<br />Type: " + type + "<br />Route:" + "<input type='button' onClick=getDir() value='Go!'>" + "<br />Website: " + "<a href='http://www.go-green.netai.net/web/'>More details</a>" + "<br />Video: " + "<a href='http://www.youtube.com/embed?listType=search&list=go-green'>More videos about go green</a>"
var latlng = new google.maps.LatLng(lat, lng);
bounds.extend(latlng);
var marker = new google.maps.Marker({
position: latlng,
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, place, nearTo, lat, lng, type,html) {
return function() {
infowindow.setContent(html);
infowindow.open(map, marker);
}
})(marker, place, nearTo, lat, lng, type,html));
map.fitBounds(bounds);
}
}
google.maps.event.addDomListener(window, 'load', initialize);
答案 0 :(得分:0)
你真的不需要当前的位置。要通过意图启动Google地图导航,只需致电google.navigation:q=your+marker+location
intent。