Javascript 谷歌地图v3地图设置中心
我不确定在哪里添加设置中心,因为我的地图目前正以上面的LatLng为中心,我将var设置中心代码放在下面,我的javascript很糟糕,所以我甚至不知道下面到底发生了什么,我只是将代码复制粘贴到下面,所以对于我来说,查看stackexchange上的其他示例将不起作用,如果有人知道我将把设置中心放在哪里,我将不胜感激Javascript 谷歌地图v3地图设置中心,javascript,jquery,Javascript,Jquery,我不确定在哪里添加设置中心,因为我的地图目前正以上面的LatLng为中心,我将var设置中心代码放在下面,我的javascript很糟糕,所以我甚至不知道下面到底发生了什么,我只是将代码复制粘贴到下面,所以对于我来说,查看stackexchange上的其他示例将不起作用,如果有人知道我将把设置中心放在哪里,我将不胜感激 var directionDisplay; var directionsService = new google.maps.DirectionsService(); functi
var directionDisplay;
var directionsService = new google.maps.DirectionsService();
function initialize() {
var latlng = new google.maps.LatLng(-33.8333406,18.6470022);
var setCenter = new google.maps.LatLng(37.4419, -122.1419);
directionsDisplay = new google.maps.DirectionsRenderer();
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false
};
var map = new google.maps.Map(document.getElementById("map"),myOptions);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById("directionsPanel"));
var marker = new google.maps.Marker({
position: latlng,
map: map,
title:"Get Directions"
});
}
function calcRoute() {
var start = document.getElementById("routeStart").value;
var end = "-33.8333406,18.6470022";
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);
} else {
if (status == 'ZERO_RESULTS') {
alert('No route could be found between the origin and destination.');
} else if (status == 'UNKNOWN_ERROR') {
alert('A directions request could not be processed due to a server error. The request may succeed if you try again.');
} else if (status == 'REQUEST_DENIED') {
alert('This webpage is not allowed to use the directions service.');
} else if (status == 'OVER_QUERY_LIMIT') {
alert('The webpage has gone over the requests limit in too short a period of time.');
} else if (status == 'NOT_FOUND') {
alert('At least one of the origin, destination, or waypoints could not be geocoded.');
} else if (status == 'INVALID_REQUEST') {
alert('The DirectionsRequest provided was invalid.');
} else {
alert("There was an unknown error in your request. Requeststatus: \n\n"+status);
}
}
});
}
这一行:
var latlng = new google.maps.LatLng(-33.8333406,18.6470022);
为您提供一个latlng对象,稍后将在“贴图选项”中使用该对象以使贴图居中
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false
};
如果要使用setCenter latlng对象作为地图的中心,请在“地图选项”中对其进行更改
var setCenter = new google.maps.LatLng(37.4419, -122.1419);
var myOptions = {
zoom: 8,
center: setCenter, // setCenter latlng, defined above will be used to center tha map
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false
};