GMaps API:通过纬度/纬度坐标和地址搜索地图



我一直在使用Google Maps API中的Geocoder服务来支持地图上的搜索框。我希望允许用户按地址,城市和坐标以及可能支持的任何其他内容自由搜索。直到最近,如果我将纬度/经度坐标传递给地理编码器,它只会向我返回这些特定坐标的结果,但最近它已更改为进行反向查找并为我提供离坐标最近的地址。我实际上希望该位置直接位于坐标处,因为这是最相关的。

任何想法如何从搜索框中解析出各种输入形式的坐标或让地理编码器恢复到其早期行为?

我不确定为什么它会更改为显示反向地理编码,而没有看到代码。但是,我建议改用地点 API 库的自动完成功能。

您不能只使用正则表达式来检查输入的输入是否是纬度/液化天然气对吗?然后,如果是解析该对并直接导航到坐标。像这样:

  var latLngRegex =   /^(-?d+(.d+)?),s*(-?d+(.d+)?)$/; //Regex for checking that it is a latlng pair that has been entered
  var address = document.getElementById("txt_googlesearch").value;
  if (address=='' || address=='Search') {
    return;
  }
  if (latLngRegex.test(address)) //Run the regex against the entered value
  {
    var coords = address.split(","); //Split the address into 2 decimal values
    var mapPoint = new GLatLng(parseInt(coords[0]), parseInt(coords[1]));  //Create a gLatLng from the split values
    map.setCenter(mapPoint); //Move the map to the entered location
    return;
  } 
  //Call Geocoder as before

最新更新