您好我想知道Geocoder是否还在工作?当我初始化新的google.maps.geocoder()时似乎没有反应;我的目的是从纬度和经度获取位置地址。
警报A和B正在运行。在警报C停止。
$('#btn').click(function initialize() {
alert("A");
var loc = {};
alert("B");
var geocoder = new google.maps.Geocoder();
alert("C");
if(google.loader.ClientLocation) {
alert("D");
loc.lat = google.loader.ClientLocation.latitude;
loc.lng = google.loader.ClientLocation.longitude;
var latlng = new google.maps.LatLng(loc.lat, loc.lng);
geocoder.geocode({'latLng': latlng}, function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
alert(results[0]['formatted_address']);
};
});
}
});
答案 0 :(得分:0)
尝试从lat,lng获取地址。单击标记以显示地址。它可以替代你的工作。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Testing</title>
<script src="scripts/jquery-1.9.1.min.js" type="text/javascript"></script>
<script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyA7IZt-36CgqSGDFK8pChUdQXFyKIhpMBY&sensor=true" type="text/javascript"></script>
<script type="text/javascript">
var map;
var geocoder;
var marker;
var latlng;
var infowindow;
$(document).ready(function() {
});
function initialize() {
var mapOptions = {
//center: new google.maps.LatLng(40.4230, 98.7372), // Coimbatore = (11.0168445, 76.9558321)
zoom: 6,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
geocoder = new google.maps.Geocoder();
infowindow = new google.maps.InfoWindow();
var latlngStr = $('#address').val().split(",");
var lat = parseFloat(latlngStr[0]);
var lng = parseFloat(latlngStr[1]);
latlng = new google.maps.LatLng(lat, lng);
marker = new google.maps.Marker({
position: latlng,
map: map,
draggable: true
});
map.setCenter(latlng);
google.maps.event.addListener(marker, 'click', function(event) {
geocoder.geocode({ 'latLng': event.latLng }, function(results, status) {
infowindow.setContent(results[0].formatted_address);
});
infowindow.setContent(this.html);
infowindow.setPosition(event.latLng);
infowindow.open(map, this);
});
}
</script>
</head>
<body>
<label>
Lat, Lng:
</label>
<input id="address" type="text" placeholder="10.9435131, 76.9383790" />
<input type="button" value="Go" onclick="initialize()" />
<br />
<br />
<div id="map-canvas" style="width: 800px; height: 500px">
</div>
</body>
</html>