我的应用程序目前根据硬编码的纬度和经度查找最近的滑雪区。我希望能够输入地址并将该地址转换为经度和纬度以进行搜索。我正在使用 Google Maps api 和 places 库。我知道我不知何故需要使用地理编码,但不知道该怎么做。这是代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ski finder</title>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true&libraries=places"></script>
<style>
#map {
height: 400px;
width: 600px;
border: 1px solid #333;
margin-top: 0.6em;
}
</style>
<script>
var map;
var infowindow;
function initialize() {
var loca = new google.maps.LatLng(41.7475, -74.0872);
map = new google.maps.Map(document.getElementById('map'), {
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: loca,
zoom: 8
});
var request = {
location: loca,
radius: 50000,
name: 'ski',
keyword: 'mountain',
type: ['park']
};
infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
service.nearbySearch(request, callback);
}
function callback(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
createMarker(results[i]);
}
}
}
function createMarker(place) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
google.maps.event.addListener(marker, 'mouseover', function() {
infowindow.setContent(place.name);
infowindow.open(map, this);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<h1> Welcome to Ski Finder </h1>
<p> Please enter you location below, and we will find your local ski areas. </p>
<form>
<label for="zip">Zip Code: </label>
<input type = "text" name="zip" placeholder = "12345" autofocus>
</form>
<div id="map"></div>
<div id="text">
</body>
</html>
我也尝试使用谷歌地图开发者页面上的示例,但无法正常工作。先谢谢了。