我只想问一下如何使用谷歌地图获取当前的经度和纬度?因为我正在创建一个地图工具来调整数据库中的地图。如果存在给定的纬度和经度,则只显示地图。对于那件事我没有任何疑问。但如果经度和纬度为NULL或0.我怎样才能获得精确的纬度和经度?
我有这段代码:
for ($i = 0; $i <= $companies_count; $i++) :
$company = $companies[$i];
$company_id = $company['company_id'];
//file information from textfile
$file_id = $addresses_file[$i]['company_id'];
$file_latitude = $addresses_file[$i]['new_lat'];
$file_longitude = $addresses_file[$i]['new_long'];
foreach($addresses_file as $y){
if($company_id == $y['company_id']){
$lat = $y['new_lat'];
$long = $y['new_long'];
}else{
$lat = $additionals[$company_id]['geo_lat'];
$long = $additionals[$company_id]['geo_long'];
if($lat == 0 || $lat == NULL){
//set lat to current position
}
if($long == 0 || $long == NULL){
//set long to current position
}
}
}
endfor;
在我的javascript中:
<script>
var maps = {},
geocoder = null;
function showAddress(address, i) {
if (geocoder) {
geocoder.geocode( { 'address' : address },
function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
//var id = $("[name='selected']").val();
var id = i;
var center = results[0].geometry.location;
document.getElementById("lat_" + id).innerHTML = center.lat().toFixed(5);
document.getElementById("long_" + id).innerHTML = center.lng().toFixed(5);
maps[id].mapObj.setCenter(center);
maps[id].marker.setPosition(center);
$('html, body').animate({
scrollTop: $("tr[rel='" + id + "']").offset().top
}, 1000);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
}
);
}
}
function fn_initialize() {
geocoder = new google.maps.Geocoder();
var hh_map = {
init: function(lat, lon, z_lvl, label, id){
var latlng = new google.maps.LatLng(lat, lon);
var mapOptions = {
zoom: z_lvl,
mapTypeId: google.maps.MapTypeId.ROADMAP,
streetViewControl: false,
overviewMapControl: false,
mapTypeControl: false,
panControl: false,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.SMALL,
position: google.maps.ControlPosition.LEFT_BOTTOM
},
center: latlng,
};
var map = new google.maps.Map(document.getElementById("map_canvas_" + id), mapOptions);
var marker = new google.maps.Marker({
map: map,
position: map.getCenter(),
title: label,
draggable: true
});
// this will update update the lat_{id} and long_{id}
google.maps.event.addListener(marker, 'dragend', function(e) {
var center = marker.getPosition();
document.getElementById("lat_" + id).innerHTML = center.lat().toFixed(5);
document.getElementById("long_" + id).innerHTML = center.lng().toFixed(5);
});
// this will center the marker in the map and update the lat_{id} and long_{id}
/*google.maps.event.addListener(map, 'idle', function(e) {
var center = map.getCenter();
document.getElementById("lat_" + id).innerHTML = center.lat().toFixed(5);
document.getElementById("long_" + id).innerHTML = center.lng().toFixed(5);
marker.setPosition(center);
});*/
maps[id] = {'mapObj' : map, 'marker' : marker};
}
};
$('.map_canvas').each(function(){
if ($(this).data('lat') && $(this).data('long')) {
hh_map.init($(this).data('lat'), $(this).data('long'), 16, $(this).data('label'), $(this).data('company_id'));
}
})
}
function loadScript() {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&callback=fn_initialize";
document.body.appendChild(script);
}
window.onload = loadScript;
$(document).ready(function(){
//update address
$('.save-button').each(function(index, elm){
$(elm).click(function(){
var id = $(this).attr("id"),
val_lat = $('#lat_' + id).html(),
val_long = $('#long_' + id).html();
if (val_lat.length > 0 && val_long.length > 0) {
var x = confirm("Are you sure you want to update this?");
if(x == true){
$('<form action="" method="POST">' +
'<input type="hidden" name="company_id" value="' + id + '">' +
'<input type="hidden" name="new_lat" value="' + val_lat + '">' +
'<input type="hidden" name="new_long" value="' + val_long + '">' +
'</form>').submit();
}else{
return false;
}
} else {
alert('New locations are empty!');
}
return false;
})
})
//revert address
$('.revert-button').each(function(index, elm){
$(elm).click(function(){
var id = $(this).attr("id"),
val_lat = $('#lat_' + id).html(),
val_long = $('#long_' + id).html();
if (val_lat.length > 0 && val_long.length > 0) {
var x = confirm("Are you sure you want to revert this?");
if(x == true){
$('<form action="" method="POST">' +
'<input type="hidden" name="company_id" value="' + id + '">' +
'<input type="hidden" name="new_lat" value="' + val_lat + '">' +
'<input type="hidden" name="new_long" value="' + val_long + '">' +
'</form>').submit();
}else{
return false;
}
} else {
alert('New locations are empty!');
}
return false;
})
})
})
</script>
答案 0 :(得分:4)
使用JavaScript,您可以使用:
var center = map.getCenter();
alert(center.lat() + ', ' + center.lng());
链接重复问题中显示的最佳答案是针对Google Maps V2。