0

次のコードは正しく機能しますが、「start」値は、マップを含むdivに割り当てられたdata-start属性から取得されます。

(ジオロケーションコードから)現在の場所の現在の座標を受け入れるには、次のものが必要です。

    var geocoder = new google.maps.Geocoder();
        geocoder.geocode({ address: **start** }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                opts.center = results[0].geometry.location;
                self.map = new google.maps.Map($('#map')[0], opts);
                $.post($('#stockists form').attr('action'), { query: start }, function(data) {
                    self.addMarkers(data.results);
                });
                self.locateUser();
            } else {
                alert("Geocoder failed due to: " + status);
            }
        });

開始値は、データ属性からの静的な値ではなく、現在の場所を取得することを想定しています。あなたはここで完全なコードを見ることができます

アイデアは、現在の場所に基づいてdata.resultsをロードすることです。

4

1 に答える 1

1

(ユーザーの)現在の場所は、 geolocationを使用して検出できます。

したがって、手順は次のとおりです。

  1. ユーザーの場所を検出する
  2. 逆ジオコーディングを使用してformatted_addressをリクエストします
  3. $('#map').data('start')逆ジオコーディングの結果に設定
  4. 新しいMap-objectを作成し、init-methodを呼び出します。new Map().init();

例:

  var geocoder=new google.maps.Geocoder();
  navigator.geolocation.getCurrentPosition(function(position) {
    geocoder.geocode({latLng:new google.maps.LatLng(position.coords.latitude, 
                                                    position.coords.longitude)},
          function(results,status){
            if (status == google.maps.GeocoderStatus.OK) {
              $('#map').data('start',results[0].formatted_address);
              new Map().init();
          }
        })
  });
于 2013-01-17T12:12:12.267 に答える