如何从response.loc设置地理编码: $("#details").html("Location: " + response.loc);
。
作为以下内容的值: var latlng = new google.maps.LatLng(59.6162,16.5528);
var geocoder;
var map;
var infowindow = new google.maps.InfoWindow();
var marker;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(59.6162,16.5528);
var mapOptions = {
zoom: 13,
center: latlng,
mapTypeId: 'hybrid'
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
}
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(15);
marker = new google.maps.Marker({
position: latlng,
map: map
});
infowindow.setContent(results[1].formatted_address);
infowindow.open(map, marker);
} else {
alert('No results found');
}
} else {
alert('Geocoder failed due to: ' + status);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
// get geocode from IP //
$.get("http://ipinfo.io", function (response) {
$("#ip").html("Your IP: " + response.ip);
$("#address").html("Location: " + response.city + ", " + response.region);
$("#details").html("Location: " + response.loc);
}, "jsonp");
答案 0 :(得分:0)
您可以将response.loc作为参数传递给函数codeLatLng。
function codeLatLng(loc) {
var latlngStr = input.split(',', loc);
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(15);
marker = new google.maps.Marker({
position: latlng,
map: map
});
infowindow.setContent(results[1].formatted_address);
infowindow.open(map, marker);
} else {
alert('No results found');
}
} else {
alert('Geocoder failed due to: ' + status);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
// get geocode from IP //
$.get("http://ipinfo.io", function (response) {
$("#ip").html("Your IP: " + response.ip);
$("#address").html("Location: " + response.city + ", " + response.region);
$("#details").html("Location: " + response.loc);
codeLatLng(response.loc);
}, "jsonp");