谷歌地理代码Javascript API-如何获得坐标
Google Geocode Javascript API - How to get coordinates?
我希望谷歌地图将用户提交的地址转换为地图坐标。我只想把这些坐标存储在一个变量中,稍后访问:
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
。
其他谷歌地图功能在此页面上运行成功。是我的JavaScript错了,还是我不正确地使用了这个地理编码器?
来自https://developers.google.com/maps/documentation/javascript/geocoding
地理编码服务需要一个回调方法,以便在检索地理编码器的结果时执行。这个回调应该按顺序传递两个参数来保存结果和一个状态代码。
示例:
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);
}
});
注意:这是异步的本质,所以不要试图将其放在返回语句
results[0].geometry.location
的函数中试试这个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
相关文章:
- 如何在OpenLayers中获得真实坐标
- 获得旋转的顶部和左侧坐标
- 推送通过单独的 AJAX 查询获得的坐标后,无法在地图上添加折线
- 如何使用fabric js中的image.centerObject()将对象居中后获得图像坐标
- 如何使用setDragImage获得正确的X和Y坐标
- 如何获得svg地图的纬度和经度坐标's的起始点(x=0,y=0)
- 在热图上映射鼠标点击坐标以获得不同的分辨率&JS/Jquery中的屏幕大小
- 谷歌地理代码Javascript API-如何获得坐标
- 如何获得谷歌地图角落的坐标
- JavaScript获得最近点击字符的页面坐标
- 如何从HTML文件中获得一组坐标的相应元素?
- 如何在Javascript/HTML5的TeeChart中获得最新绘制的点坐标
- 如何获得webgl顶点的二维坐标
- 无需绘图即可获得SVG路径坐标
- 如何从WordPress自定义字段获得坐标来嵌入谷歌地图
- 如何获得坐标数组的最大/最小边界
- 我如何创建一个svg精灵,然后获得每个svg的坐标
- 获得画布绘制的精确坐标
- 谷歌Chrome扩展获得选择坐标
- 如何获得X/Y坐标与jquerymobile所有的时间我的手指正在移动?像鼠标移动桌面