谷歌地图和shadowbox
我正在构建一个在shadowbox内加载谷歌地图的javascript函数,使用这段代码非常容易(我使用名为GeoPlugin的插件来获取纬度和经度,因为我现在不知道如何自动获取我的纬度和经度):
function initGeoLocationOnDemand() {
$('a.btnInitGeoLoc').click(function () {
if (GBrowserIsCompatible()) {
Shadowbox.open({
player: "html",
content: "",
height: 300,
width: 500,
options: {
onFinish: function (item) {
var body = document.getElementById(Shadowbox.playerId);
var map = new GMap2(body);
//using geoPlugin to get lat and long
var point = new GLatLng(geoplugin_latitude(), geoplugin_longitude());
map.setCenter(point, 15);
var marker = new GMarker(point);
var addrFields = ['street_address'];
map.addOverlay(marker);
marker.openInfoWindow(document.createTextNode("hello");
// add some simple controls
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
}
}
});
} else {
alert("Your browser is not compatible with Google Maps!");
}
});
Shadowbox.init();
}
现在我想使用 GoogleMaps 反向地理编码 进入googleMaps中的信息窗口标记信息,例如:城市名称,国家名称,街道名称,邮政编码等...
我按照GoogleMaps api编写了这段代码,但它不再工作了...总是使用GeoPlugin:
function initGeoLocationOnDemand() {
$('a.btnInitGeoLoc').click(function () {
if (GBrowserIsCompatible()) {
Shadowbox.open({
player: "html",
content: "",
height: 300,
width: 800,
options: {
onFinish: function (item) {
var body = document.getElementById(Shadowbox.playerId);
var geocoder;
var map = new google.maps.Map(body);
var infowindow = new google.maps.InfoWindow();
var marker;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(geoplugin_latitude(),geoplugin_longitude());
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(body, myOptions);
}
function codeLatLng() {
var input = document.getElementById("latlng").value;
var latlngStr = input.split(",", 2);
var lat = parseFloat(latlngStr[0]);
var lng = parseFloat(latlngStr[1]);
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({ 'latLng': latlng }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
map.setZoom(11);
marker = new google.maps.Marker({
position: latlng,
map: map
});
infowindow.setContent(results[1].formatted_address);
infowindow.open(map, marker);
}
} else {
alert("Geocoder failed due to: " + status);
}
});
}
}
}
});
} else {
alert("Your browser is not compatible with Google Maps!");
}
});
Shadowbox.init();
}
有一个不使用插件即可获取纬度和经度的方法?
有没有办法获取有关国家名称、城市名称、街道名称、邮政编码等信息?
预先感谢您的帮助!
I'm building a javascript function that load google maps inside shadowbox, this is quite easy using this code (I used the plugin calld GeoPlugin to get latitude and longitude, because I don't now how to get my lat and long automatically):
function initGeoLocationOnDemand() {
$('a.btnInitGeoLoc').click(function () {
if (GBrowserIsCompatible()) {
Shadowbox.open({
player: "html",
content: "",
height: 300,
width: 500,
options: {
onFinish: function (item) {
var body = document.getElementById(Shadowbox.playerId);
var map = new GMap2(body);
//using geoPlugin to get lat and long
var point = new GLatLng(geoplugin_latitude(), geoplugin_longitude());
map.setCenter(point, 15);
var marker = new GMarker(point);
var addrFields = ['street_address'];
map.addOverlay(marker);
marker.openInfoWindow(document.createTextNode("hello");
// add some simple controls
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
}
}
});
} else {
alert("Your browser is not compatible with Google Maps!");
}
});
Shadowbox.init();
}
Now I would like tu use GoogleMaps Reverse Geocoding to get inside the information window in googleMaps marker information like: city name, country name, street name, postal code, etc...
I did this code, following GoogleMaps api, but it doesn't work anymore... always using GeoPlugin:
function initGeoLocationOnDemand() {
$('a.btnInitGeoLoc').click(function () {
if (GBrowserIsCompatible()) {
Shadowbox.open({
player: "html",
content: "",
height: 300,
width: 800,
options: {
onFinish: function (item) {
var body = document.getElementById(Shadowbox.playerId);
var geocoder;
var map = new google.maps.Map(body);
var infowindow = new google.maps.InfoWindow();
var marker;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(geoplugin_latitude(),geoplugin_longitude());
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(body, myOptions);
}
function codeLatLng() {
var input = document.getElementById("latlng").value;
var latlngStr = input.split(",", 2);
var lat = parseFloat(latlngStr[0]);
var lng = parseFloat(latlngStr[1]);
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({ 'latLng': latlng }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
map.setZoom(11);
marker = new google.maps.Marker({
position: latlng,
map: map
});
infowindow.setContent(results[1].formatted_address);
infowindow.open(map, marker);
}
} else {
alert("Geocoder failed due to: " + status);
}
});
}
}
}
});
} else {
alert("Your browser is not compatible with Google Maps!");
}
});
Shadowbox.init();
}
There is a way to get latitude and longitude without using plugins?
There is a way to get information about Country name, City name, street name, postal code, etc?
Thanks in advance for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想要什么地方的纬度和经度?客户端机器?
Latitude and longitude of what place do you want? The client machine?
我对 Shadowbox 不熟悉,但在处理 Geocoder 的结果时肯定至少存在一个问题。您应该使用
results[0]
而不是results[1]
来获取 latlng 的第一个(如果有)反向地理编码。I am not familiar with Shadowbox, but there is surely at least one problem in handling the results of the Geocoder. You should use
results[0]
instead ofresults[1]
to get the first (if any) reverse geocode of latlng.