从GWT调用帧js

时间:2017-04-17 11:15:52

标签: javascript gwt

我试图从GWT调用帧js。

我的java代码是:

 public class Trips extends MyPopup{
    Frame f = new Frame("js/location.html")

    public popupPanel(){
          super() ;
         setAddress(0d,0d,"asknjsnkjsd") ;

     }
     //other methods.object of this class is created while loading

    public static native void setAddress(Double lat,Double longitude,String address) /*-{
           $wnd.alert(address);
          $wnd.setAddress(lat,longitude,address) ;
     }-*/;

}

我的js代码是:

 var map ;
 var marker ;

 function initMap() {
var locat = new google.maps.LatLng(parseFloat("34.149486"),parseFloat("-117.257317"));

map = new google.maps.Map(document.getElementById('map'), {
        center : {
            lat : 34.149486,
            lng : -117.257317
        },
        zoom : 13
    });

myMap  = map;


marker = new google.maps.Marker({
    position : {
        lat : 34.149486,
        lng : -117.257317
    },
    map : map,
    draggable : true,
    anchorPoint : new google.maps.Point(0, -29)
});

myMarker = marker ;

var geocoder = geocoder = new google.maps.Geocoder();

var card = document.getElementById('pac-card');
var input = document.getElementById('pac-input');
var strictBounds = document.getElementById('strict-bounds-selector');

map.controls[google.maps.ControlPosition.TOP_RIGHT].push(card);

var autocomplete = new google.maps.places.Autocomplete(input);

// Bind the map's bounds (viewport) property to the autocomplete object,
// so that the autocomplete requests use the current map bounds for the
// bounds option in the request.
autocomplete.bindTo('bounds', map);

var infowindow = new google.maps.InfoWindow();
var infowindowContent = document.getElementById('infowindow-content');
infowindow.setContent(infowindowContent);

google.maps.event
        .addListener(
                marker,
                "dragend",
                function(e) {
                    var lat, lng, address;

                    geocoder
                            .geocode(
                                    {
                                        'latLng' : marker.getPosition()
                                    },
                                    function(results, status) {
                                        document
                                                .getElementById('pac-input').value = results[0].formatted_address;
                                        var latitide = results[0].geometry.location.lat();
                                        var longitude = results[0].geometry.location.lng();
                                        var addressLocation = results[0].formatted_address;
                                    });
                });

autocomplete.addListener('place_changed', function() {
    infowindow.close();
    marker.setVisible(false);
    var place = autocomplete.getPlace();
    if (!place.geometry) {
        // User entered the name of a Place that was not suggested and
        // pressed the Enter key, or the Place Details request failed.
        window
                .alert("No details available for input: '" + place.name
                        + "'");
        return;
    }

    // If the place has a geometry, then present it on a map.
    if (place.geometry.viewport) {
        map.fitBounds(place.geometry.viewport);
    } else {
        map.setCenter(place.geometry.location);
        map.setZoom(17); // Why 17? Because it looks good.
    }
    marker.setPosition(place.geometry.location);
    marker.setVisible(true);

    var address = '';
    var latitide = place.geometry.location.lat();
    var longitude = place.geometry.location.lng();
    var addressLocation = place.formatted_address;
    if (place.address_components) {
        address = [
                (place.address_components[0]
                        && place.address_components[0].short_name || ''),
                (place.address_components[1]
                        && place.address_components[1].short_name || ''),
                (place.address_components[2]
                        && place.address_components[2].short_name || '') ]
                .join(' ');
    }

    infowindowContent.children['place-icon'].src = place.icon;
    infowindowContent.children['place-name'].textContent = place.name;
    infowindowContent.children['place-address'].textContent = address;
    infowindow.open(map, marker);
});

// Sets a listener on a radio button to change the filter type on Places
// Autocomplete.
}

 function setOriginAddressWithLatitude(lat,long,address){
     window
     .alert("No details available for input: '" + lat + " " + long +"  " + address
        + "'");

      document.getElementById('pac-input').value = address;
//initMap() ;
    map.setCenter(new google.maps.LatLng(lat,long));
    marker.setPosition(new google.maps.LatLng(lat,long));

  }

html文件是:

<!DOCTYPE html>
<html>
  <head>
   <script src="https://maps.googleapis.com/maps/api/js?key=API_Key&libraries=places&callback=initMap"
        async defer></script>
    <script src = "location.js"></script>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">

  </head>
  <body>
    <div class="pac-card" id="pac-card">
      <div id="pac-container">
        <input id="pac-input" type="text"
            placeholder="Enter a location" value = "St Marys Rd Sydney NSW 2000 Australia">
      </div>
    </div>
    <div id="map"></div>
    <div id="infowindow-content">
      <img src="" width="16" height="16" id="place-icon">
      <span id="place-name"  class="title"></span><br>
      <span id="place-address"></span>
    </div>
  </body>
</html>

我收到了以下错误  无法设置属性&#39;值&#39;在行document.getElementById(&#39; pac-input&#39;)上的null .value = address;

如果我删除上面的行。我收到错误 map.setCenter不是函数

1 个答案:

答案 0 :(得分:0)

我使用了以下

public native void setOriginAddressWithLatitude(Double latitude,Double longitude,String address) /*-{
        var iframe  = $doc.getElementById("viewMapIFrame");
        iframe.contentWindow.setOriginAddressWithLatitude(latitude,longitude,address) ;
    }-*/;