gpt4 book ai didi

javascript - 谷歌地图设置中心。专注于新位置

转载 作者:行者123 更新时间:2023-11-30 17:40:54 25 4
gpt4 key购买 nike

我正在使用谷歌地图并在 http://www.dentalo.se/search/stockholm 处呈现许多标记它工作正常。我想做的是当有人点击我想使用 map.SetCenter 的按钮并关注该位置时。

enter image description here

我有一个信息按钮

enter image description here

当用户点击它时,我调用了一个 JavaScript 函数并设置了中心。但是没有用。当我点击信息时,它只是变成灰色。您可以自己尝试一下,看看会发生什么 http://www.dentalo.se/search/stockholm .

function showCompany(latlong) {
map.setCenter(latlong);
alert(latlong);
}

这是渲染 map 的jquery代码

var p = $("#map_search");
var position = p.position();
$("#directionsPanel").css({top: position.top, position:'absolute'});

var map;
var addressField;
var geocoder;
$(document).ready(function () {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(ShowPosition);
}
else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
function showError(error) {
switch (error.code) {
case error.PERMISSION_DENIED:
alert("User denied the request for Geolocation.");
break;
case error.POSITION_UNAVAILABLE:
alert("Location information is unavailable.");
break;
case error.TIMEOUT:
alert("The request to get user location timed out.");
break;
case error.UNKNOWN_ERROR:
alert("An unknown error occurred.");
break;
}
}
function ShowPosition(position) {
//begin rest call
$("#latitude").val(position.coords.latitude);
$("#longitude").val(position.coords.longitude);
var from = $("#latitude").val() + "," + $("#longitude").val();
var urlParts = window.location.href.split('/'),
Id = urlParts[(urlParts.length - 1)];
$.ajax({
type: "GET",
cache: true,
async: false,
url: "/RestService/Dentalo.svc/CompaniesByState/" + Id,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
map = new GMaps({
el: 'map_search',
//center: Id,
lat: data[0].State.Latitude ,//position.coords.latitude,
lng: data[0].State.Longitude,
zoom: 14,
zoomControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.DEFAULT,
position: google.maps.ControlPosition.RIGHT_RIGHT
},

panControl: true,
panControlOptions: {
position: google.maps.ControlPosition.RIGHT_TOP
},

zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.LARGE,
position: google.maps.ControlPosition.RIGHT_TOP
},
panControl: true,
scrollwheel: true
});
map.addMarker({
lat: position.coords.latitude,
lng: position.coords.longitude,
title: 'Min position',
icon: 'http://www.dentalo.se/assets/img/map/user_x64.png'
});
var h = "";
$.each(data, function (index, item) {
var to = item.Latitude + "," + item.Longitude;
h += "<div class='row buttons-page'>" +
"<div class='col-md-8 col-sm-4'>" +
"<h4>"+ item.Name + "</h4>" +
"<p style='margin: 1px;'>" + item.Address + ", " + item.County.Name + "</p>" +
"<p style='margin: 1px;'>" + item.Phone + "</p>" +
"<p style='margin: 1px;'>Distans: " + calcRoute(from, to) + "</p>" +
"</div>" +
"<div class='col-md-3 col-sm-9'>" +
"<div class='btn-group'>" +
"<a href='/booking/"+ item.CompanyId +"' class='btn " + bookable(item.Status) + " " + SetDisplayClass(item.Status) +"'>Boka <i class='m-icon-swapright m-icon-white'></i></a>" +
"<button type='button' class='btn default' onClick='showCompany(&apos;" + to + "&apos;);return false;'>Information <i class='icon-info-sign m-icon-white'></i></button>" +
"</div>" +
"</div>" +
"</div>" +
"<hr style='margin: 1px;' />";
map.addMarker({
lat: item.Latitude ,
lng: item.Longitude ,
title: item.Address ,
icon: GetMarkerImage(item.Status),
infoWindow: {
content: '<div style="width: 300px"><h4>' + item.Name + '</h4><br /><p>' + item.Address + ', ' + item.County.Name + '</p><div class="four columns alpha"><a class="btn blue ' + SetDisplayClass(item.Status) + '" href="/booking/' + item.CompanyId + '" ><i class="m-icon-swapright m-icon-white"></i> Boka</a> <a href="#" onClick="showPopUp(&apos;' + item.CompanyId +'&apos;);return false;" class="btn default">Information</a></div></div>',
}
})
});
$("#search_panel").html(h).show();
},
error: function (msg, url, line) {
//alert('error trapped in error: function(msg, url, line)');
alert('msg = ' + msg + ', url = ' + url + ', line = ' + line);
}
});
//end rest call

// Define Gecoder
geocoder = new google.maps.Geocoder();

// Init searchbox
initSearchBox();

// main directions

}

当用户单击“信息” 按钮时,如何设置 SetCenter?

提前致谢。

最佳答案

map.setCenter(latlong);

latlong 必须是一个 google.maps.LatLng 对象,但在您的情况下,您正在将一个字符串传递给您的函数。

<button onclick="showCompany('59.3618356,18.0140273');return false;">Information</button>

所以那行不通。

改为尝试:

<button onclick="showCompany('59.3618356','18.0140273');return false;">Information</button>

function showCompany(lat, lng) {
var position = new google.maps.LatLng(lat, lng);
map.setCenter(position);
alert(lat, lng);
}

关于javascript - 谷歌地图设置中心。专注于新位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21111860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com