当我在v2上使用上面的代码时,它正在工作,但是当我在V3地图中使用它时,我无法跟踪位置;纬度和经度甚至没有返回。
谁能告诉我为什么会这样?
function load()
{
if (GBrowserIsCompatible())
{
var map = new GMap2(document.getElementById("map"));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
var center = new GLatLng(48.89364,2.33739);
map.setCenter(center, 15);
geocoder = new GClientGeocoder();
var marker = new GMarker(center);
map.addOverlay(marker);
document.getElementById("lat").innerHTML = center.lat().toFixed(5);
document.getElementById("lng").innerHTML = center.lng().toFixed(5);
}
}
function showAddress(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)
{
alert(address + " not found");
}
else
{
document.getElementById("lat").innerHTML = point.lat().toFixed(5);
document.getElementById("lng").innerHTML = point.lng().toFixed(5);
map.clearOverlays()
map.setCenter(point, 14);
var marker = new GMarker(point);
map.addOverlay(marker);
}
}
);
}
}
</script>
答案 0 :(得分:3)
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Gecoding</title>
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map { height: 100% }
</style>
<script type="text/javascript"
src="http://maps.googleapis.com/maps/api/js?sensor=true&v=3&libraries=geometry">
</script>
<script type="text/javascript">
var geocoder;
var map;
function initialize()
{
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(17.447246,78.362029);
var myOptions =
{
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map"), myOptions);
var marker=new google.maps.Marker({
position: latlng,
map: map
});
marker.setMap(map);
document.getElementById("lat").innerHtml=latlng.lat.toFixed(5);
document.getElementById("lng").innerHTML=latlng.lng.toFixed(5);
}
function getAddress()
{
var address = document.getElementById("address").value;
var latlng;
geocoder.geocode({'address': address},function(results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
latlng = results[0].geometry.location;
document.getElementById("lat").innerHTML= latlng.lat();
document.getElementById("lng").innerHTML= latlng.lng();
map.setCenter(results[0].geometry.location);
marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
marker.setMap(Map);
}
else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
</script>
</script>
<body onload="initialize();">
<form onsubmit="getAddress();return false;" method="post">
<input id="address" type="text" size="60" name="address"/>
<input type="submit" value="Search!" /><br /><br />
Latitude: <span id="lat"></span> <br /><br />
Longitude: <span id="lng"></span>
</form>
<table>
<tr><td><span></span></td></tr>
<tr><td><span></span></td></tr>
</table>
<div align="center" id="map" style="width: 100%;top: 5%; height: 95%"><br/></div>
</div>
<script type="text/javascript">
</script>
</body>
</html>
答案 1 :(得分:0)
尝试使用new google.maps.map
代替new GMap2
编辑:
要使用api-3删除标记,您必须执行
google.maps.Map.prototype.markers = new Array();
google.maps.Map.prototype.addMarker = function(marker) {
this.markers[this.markers.length] = marker;
};
google.maps.Map.prototype.getMarkers = function() {
return this.markers
};
google.maps.Map.prototype.clearMarkers = function() {
for(var i=0; i<this.markers.length; i++){
this.markers[i].setMap(null);
}
this.markers = new Array();
};
中提取的解决方案