我必须使用我的网页中的搜索按钮找到用户在谷歌地图中的位置。但它适用于错误的位置,所以我必须验证位置(纬度和经度)以找到标记。我有尝试了很多但标记位于错误的位置也可以。任何人都可以解决我的问题吗?
这是我的地图代码:
function load() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
var center = new GLatLng(<?php echo $clat;?>,<?php echo $clng;?>);
map.setCenter(center, 15);
geocoder = new GClientGeocoder();
var marker = new GMarker(center, {draggable: false});
map.addOverlay(marker);
document.getElementById("lat").value = center.lat().toFixed(5);
document.getElementById("lng").value = center.lng().toFixed(5);
GEvent.addListener(marker, "dragend", function() {
var point = marker.getPoint();
map.panTo(point);
document.getElementById("lat").value = point.lat().toFixed(5);
document.getElementById("lng").value = point.lng().toFixed(5);
});
}
}
function showAddress(id) {
var address=document.getElementById(id).value;
var n=id.replace("comp_address","");
var map = new GMap2(document.getElementById("map"));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
if (geocoder) {
geocoder.getLatLng(
address,
function(point) {
if (!point) {
document.getElementById('locaterr').innerHTML="Enter valid Location and Search"
document.getElementById("map").style.display="none";
document.getElementById(id).focus();
} else {
document.getElementById("map").style.display="block";
document.getElementById("lat"+n).value = point.lat().toFixed(5);
document.getElementById("lng"+n).value = point.lng().toFixed(5);
map.clearOverlays()
map.setCenter(point, 14);
var marker = new GMarker(point, {draggable: false});
map.addOverlay(marker);
GEvent.addListener(marker, "dragend", function() {
var pt = marker.getPoint();
map.panTo(pt);
document.getElementById("lat"+n).value = pt.lat().toFixed(5);
document.getElementById("lng"+n).value = pt.lng().toFixed(5);
});
}
}
);
}
}
<input type="text" name="locat" id="comp_address0" value="<?php echo $locat;?>" class="canditate_map_inputbox" onKeyUp="specValidate(comp_address0,locaterr);this.value=this.value.replace(/[^a-zA-Z_ ,\.]/g,'');" onChange="resetlocat()" onblur="comp_addre();"/>
<input class="go_play_btn_1" type="button" value="search<?php echo $cons['search'];?>" onclick="showAddress('comp_address0');" /><div class="text_error1"><span id="entlocationntype" style="display:none;color:#C00"> Enter Location And Search</span> </div> <span id="locaterr" style="color:#C00"></span>
<input type="hidden" id="lat0" name="lat" value="<?php //echo $lats;?>">
<input type="hidden" id="lng0" name="lng" value="<?php //echo $lngs;?>">