Javascript谷歌地图api输入地址显示位置
我的应用程序目前根据硬编码的纬度和经度查找最近的滑雪区。我希望能够键入一个地址,并将该地址转换为搜索的纬度和经度。我正在使用谷歌地图api和places库。我知道我需要使用地理编码,但不知道怎么做。代码如下:Javascript谷歌地图api输入地址显示位置,javascript,google-maps-api-3,geocoding,Javascript,Google Maps Api 3,Geocoding,我的应用程序目前根据硬编码的纬度和经度查找最近的滑雪区。我希望能够键入一个地址,并将该地址转换为搜索的纬度和经度。我正在使用谷歌地图api和places库。我知道我需要使用地理编码,但不知道怎么做。代码如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>ski finder</title> <script
<!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>
滑雪爱好者
#地图{
高度:400px;
宽度:600px;
边框:1px实心#333;
边缘顶部:0.6em;
}
var映射;
var信息窗口;
函数初始化(){
var loca=new google.maps.LatLng(41.7475,-74.0872);
map=new google.maps.map(document.getElementById('map'){
mapTypeId:google.maps.mapTypeId.ROADMAP,
中心:失水事故,
缩放:8
});
var请求={
地点:失水事故,
半径:50000,
名字:'滑雪',
关键词:"山",,
类型:[“公园”]
};
infowindow=new google.maps.infowindow();
var service=newgoogle.maps.places.PlacesService(地图);
服务.nearbySearch(请求、回调);
}
函数回调(结果、状态){
if(status==google.maps.places.PlacesServiceStatus.OK){
对于(var i=0;i
邮政编码:
此外,我还尝试在谷歌地图开发者页面上使用该示例,但未能使其正常工作。
提前谢谢 使用地理编码器设置传入位置搜索的位置: 代码片段:
var地理编码器;
var映射;
var信息窗口;
函数初始化(){
geocoder=新的google.maps.geocoder();
var loca=new google.maps.LatLng(41.7475,-74.0872);
map=new google.maps.map(document.getElementById('map'){
mapTypeId:google.maps.mapTypeId.ROADMAP,
中心:失水事故,
缩放:8
});
}
函数回调(结果、状态){
if(status==google.maps.places.PlacesServiceStatus.OK){
对于(var i=0;i
#地图{
高度:400px;
宽度:600px;
边框:1px实心#333;
边缘顶部:0.6em;
}
滑雪爱好者
欢迎来到Ski Finder
请在下面输入您的位置,我们将找到您当地的滑雪区
邮政编码:
PC Pro-谷歌地图高级示例
var-map=null;
$(文档).ready(函数(){
//设置纬度和经度的值
var纬度=parseFloat(“51.515252”);
变量经度=parseFloat(“-0.189852”);
//设置谷歌地图
负荷图(纬度、经度);
//添加标记
设置标记(纬度、经度);
//在按钮单击事件上设置地址查找
$(“#查找”)。单击(函数(){
var address=$('#address').val();
var geocoder=new google.maps.geocoder();
geocoder.geocode({'address':address},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
//在地图上显示新位置
setupMarker(结果[0].geometry.location.lat(),结果[0].geometry.location.lng())
}
else警报(“无法获得结果,原因:+状态”);
});
});
});
//加载地图
loadMap=功能(纬度、经度){
var latlng=新的google.maps.latlng(纬度、经度);
变量myOptions={
缩放:7,
中心:拉特林,
mapTypeId:google.maps.mapTypeId.ROADMAP
};
map=新的google.maps.map(document.getElementById(“map”),myOptions);
}
//在地图上指定的纬度和经度处设置标记和信息窗口
setupMarker=f
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PC Pro - Google Maps Advanced Example</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var map = null;
$(document).ready(function () {
// Set values for latitude and longitude
var latitude = parseFloat("51.515252");
var longitude = parseFloat("-0.189852");
// Setup the Google map
loadMap(latitude, longitude);
// Add the marker
setupMarker(latitude, longitude);
// Setup the address lookup on the button click event
$('#lookup').click(function() {
var address = $('#address').val();
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
// Show the new position on the map
setupMarker(results[0].geometry.location.lat(), results[0].geometry.location.lng())
}
else alert("Unable to get a result, reason: " + status);
});
});
});
// Loads the maps
loadMap = function (latitude, longitude) {
var latlng = new google.maps.LatLng(latitude, longitude);
var myOptions = {
zoom: 7,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map"), myOptions);
}
// Setups a marker and info window on the map at the latitude and longitude specified
setupMarker = function(latitude, longitude) {
// Generate the position from the given latitude and longitude values
var pos = new google.maps.LatLng(latitude, longitude);
// Define the marker on the map in the specified location
var marker = new google.maps.Marker({
position: pos,
map: map,
title: name
});
// Add a listener to this marker to display the information window on click
var info = "This is a marker for the following co-ordinates:<br />latitude: " + latitude + "<br/>longitude: " + longitude;
google.maps.event.addListener(marker, 'click', function () {
var infowindow = new google.maps.InfoWindow({
content: info
});
infowindow.open(map, marker);
});
}
</script>
</head>
<body>
<label for="address">Address:</label>
<input id="address" type="text" style="width:540px" />
<input id="lookup" type="button" value="Lookup" />
<div id="map" style="width:600px;height:600px;margin-top:10px;"></div>
</body>
</html>