Google 地理编码 Javascript API - 如何获取坐标?
Google Geocode Javascript API - How to get coordinates?
我想要 Google 地图将用户提交的地址转换为地图坐标。我只想将这些坐标存储在一个变量中,以便稍后访问:
function map_search() {
console.log("search form functional"); // sanity check
var searchLoc = $('#search_location').val();
console.log(searchLoc);
var searchCoords = geocoder.geocode( {'address': searchLoc});
console.log(searchCoords);
...
}
我是这样设置的:
var geocoder = new google.maps.Geocoder();
这在定义 var seachCoords
之前有效。在控制台中,它只是打印为 undefined
.
其他Google 地图功能在此页面上运行成功。
是我的 JavaScript 错了,还是我使用此地理编码器不当?
试试这个 php 脚本:
function getCoordinates($address){
$address = str_replace(" ", "+", $address); // replace all the white space with "+" sign to match with google search pattern
$url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=$address";
$response = file_get_contents($url);
$json = json_decode($response,TRUE); //generate array object from the response from the web
return ($json['results'][0]['geometry']['location']['lat'].",".$json['results'][0]['geometry']['location']['lng']);
}
如何调用php中的函数:
echo getCoordinates('740 Story Rd San Jose CA 95122');
Result : 37.328865,-121.8581845
来自 https://developers.google.com/maps/documentation/javascript/geocoding
的文档
The Geocoding service requires a callback method to execute upon retrieval of the geocoder's results. This callback should pass two parameters to hold the results and a status code, in that order.
示例:
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log(results); // all results
console.log(results[0].geometry.location); // "first" location
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
注意:这是本质上的异步,所以不要试图将它放在 returns results[0].geometry.location
的函数中 [= =22=] 语句
我想要 Google 地图将用户提交的地址转换为地图坐标。我只想将这些坐标存储在一个变量中,以便稍后访问:
function map_search() {
console.log("search form functional"); // sanity check
var searchLoc = $('#search_location').val();
console.log(searchLoc);
var searchCoords = geocoder.geocode( {'address': searchLoc});
console.log(searchCoords);
...
}
我是这样设置的:
var geocoder = new google.maps.Geocoder();
这在定义 var seachCoords
之前有效。在控制台中,它只是打印为 undefined
.
其他Google 地图功能在此页面上运行成功。 是我的 JavaScript 错了,还是我使用此地理编码器不当?
试试这个 php 脚本:
function getCoordinates($address){
$address = str_replace(" ", "+", $address); // replace all the white space with "+" sign to match with google search pattern
$url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=$address";
$response = file_get_contents($url);
$json = json_decode($response,TRUE); //generate array object from the response from the web
return ($json['results'][0]['geometry']['location']['lat'].",".$json['results'][0]['geometry']['location']['lng']);
}
如何调用php中的函数:
echo getCoordinates('740 Story Rd San Jose CA 95122');
Result : 37.328865,-121.8581845
来自 https://developers.google.com/maps/documentation/javascript/geocoding
的文档The Geocoding service requires a callback method to execute upon retrieval of the geocoder's results. This callback should pass two parameters to hold the results and a status code, in that order.
示例:
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log(results); // all results
console.log(results[0].geometry.location); // "first" location
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
注意:这是本质上的异步,所以不要试图将它放在 returns results[0].geometry.location
的函数中 [= =22=] 语句