Google 地图 - 用户精确定位位置

发布于 2024-10-09 20:58:23 字数 353 浏览 3 评论 0原文

是否可以允许我的网站用户在我使用 Google Maps API 显示的地图上标记地点?然后我需要将该位置坐标保存到数据库中。

我一直在浏览谷歌地图 API,我发现我可以使用网络服务进行如下搜索:

http://maps.google.com/maps/geo?q=Maine,+United+States&output=json&oe=utf8\&sensor=false&key=my_key

但我不确定它是否在门牌号级别上工作(我需要它),而且我不知道确定如何显示“您是说吗?”当用户拼写错误地址时向用户发送信息。

有人有想法吗?

谢谢,

Is it possible to allow users of my website to mark places on a map I display using Google Maps API? I need to then save that location coordinates to a db.

I've been looking through the google maps API, I found that I can use the web service to do searches like this:

http://maps.google.com/maps/geo?q=Maine,+United+States&output=json&oe=utf8\&sensor=false&key=my_key

But I am not sure it's working on a house number level (which I need it to) and I'm not sure how to display a 'did you mean?' to the user when he misspells the address..

Anyone have an idea?

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薆情海 2024-10-16 20:58:23

有一个很好的例子

http://digitalinspiration.com/community/location.html

使用这样的代码:

var map = null;
var geocoder = null;

function initialize() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
 map.addControl(new GLargeMapControl()); 
 map.addControl(new GMapTypeControl()); 
 map.setCenter(new GLatLng(37.4419, -122.1419), 14);
 map.enableScrollWheelZoom();
    geocoder = new GClientGeocoder();
    GEvent.addListener(map, "click", clicked);
  }
}

function showAddress(address) {
  if (geocoder) {
    geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
          alert("We're sorry but '" + address + "' cannot be found on Google Maps. Please try again.");
        } else {
         map.panTo(point); 
    }
  });
}
}

function clicked(overlay, latlng) {
  if (latlng) {
    geocoder.getLocations(latlng, function(addresses) {
      if(addresses.Status.code != 200) {
        alert("reverse geocoder failed to find an address for " + latlng.toUrlValue());
      }
      else {
        address = addresses.Placemark[0];
        var myHtml = address.address;
        map.openInfoWindow(latlng, myHtml);
      }
    });
  }
}

应该是您正在寻找的。

There is nice example of what you want

http://digitalinspiration.com/community/location.html

Using code like this:

var map = null;
var geocoder = null;

function initialize() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
 map.addControl(new GLargeMapControl()); 
 map.addControl(new GMapTypeControl()); 
 map.setCenter(new GLatLng(37.4419, -122.1419), 14);
 map.enableScrollWheelZoom();
    geocoder = new GClientGeocoder();
    GEvent.addListener(map, "click", clicked);
  }
}

function showAddress(address) {
  if (geocoder) {
    geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
          alert("We're sorry but '" + address + "' cannot be found on Google Maps. Please try again.");
        } else {
         map.panTo(point); 
    }
  });
}
}

function clicked(overlay, latlng) {
  if (latlng) {
    geocoder.getLocations(latlng, function(addresses) {
      if(addresses.Status.code != 200) {
        alert("reverse geocoder failed to find an address for " + latlng.toUrlValue());
      }
      else {
        address = addresses.Placemark[0];
        var myHtml = address.address;
        map.openInfoWindow(latlng, myHtml);
      }
    });
  }
}

Should be what you're looking for.

甜味拾荒者 2024-10-16 20:58:23

我已经构建了一个使用 Maps API 来显示全球赛事(300 多项体育赛事)的网站,所以是的,这是可能的。一般来说,地理编码效果非常好(就像在地图网站上一样)。我将其与地图显示一起使用,用户可以拖动标记来调整确切位置,然后保存坐标。这样,您就不再需要“您的意思是吗?”类型的框。

I've built a site which uses Maps API for displaying events across the globe (300+ of sport events), so yes, it's possible. The geocoding works really well in general (just as on the Maps site). I've used it with a Map display where the user can drag a marker to adjust the exact location, then saved the coordinates. In this way, you won't really need a 'did you mean?'-type box.

老旧海报 2024-10-16 20:58:23

我最近编写并开源了一个 Google 地理编码 API 的 SDK这应该会让 API 的使用变得更容易一些。

请注意,这是针对 v3 API 的,而在上面的代码中,您引用的是 v2 API(现已弃用)

API 本身会自动更正以下拼写错误输入。因此,如果您想确保用户输入的位置实际上是街道地址,您可以像这样(使用我的 SDK)执行此操作。

$service = new GoogleGeocodeServiceV3( new CurlCommunicator() );

$response = $service->geocode( '123 Any Street, USA' );

if ( $response->isValid() && $response->hasResults() )
{
  // Is it a street-level result?
  if ( $response->assertType( GoogleGeocodeResponseV3::ACT_STREET_ADDRESS ) )
  {
    // Street-level address found
  } else {
    // Not a street-level address
  }
}

如果他们的拼写错误非常严重,以至于 API 找不到任何内容,那么 $response->hasResults() 将返回 false (或者,count( $response ) 将为 0

I recently wrote and open-sourced an SDK for Google's Geocoding API which should making working with the API a bit easier.

Note that this is for the v3 API, whereas in the code above you're referencing the v2 API (which is now deprecated)

The API itself will auto-correct typos in the input. So if you wanted to make sure that the user entered a location that was actually a street address, you'd do that like so (with my SDK)

$service = new GoogleGeocodeServiceV3( new CurlCommunicator() );

$response = $service->geocode( '123 Any Street, USA' );

if ( $response->isValid() && $response->hasResults() )
{
  // Is it a street-level result?
  if ( $response->assertType( GoogleGeocodeResponseV3::ACT_STREET_ADDRESS ) )
  {
    // Street-level address found
  } else {
    // Not a street-level address
  }
}

If their typo was so egregious that the API didn't find anything, than $response->hasResults() would return false (or, count( $response ) would be 0)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文