1

我制作了一个小脚本,它将经度和纬度返回到输入表单的值。在 HTML 中:

<p id="demo">Click the button to get your coordinates:</p>
<button onclick="getLocation()">Try It</button>
<script>
    var x = document.getElementById("demo");
    function getLocation() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition);
        } else {
            x.innerHTML = "Geolocation is not supported by this browser.";
        }
    }
    function showPosition(position) {
        document.getElementById('latitude').value = position.coords.latitude;
        document.getElementById('longitude').value = position.coords.longitude;

    }
</script>
<input type="text" id="latitude" />
<input type="text" id="longitude" />

有用。但我想让它在春天工作。我试图这样做:

<p id="demo">Click the button to get your coordinates:</p>

<script>
    var x = document.getElementById("demo");
    function getLocation() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition);
        } else {
            x.innerHTML = "Geolocation is not supported by this browser.";
        }
    }
    function showPosition(position) {
        document.getElementById('latitude').value = position.coords.latitude;
        document.getElementById('longitude').value = position.coords.longitude;

    }
</script>

<form:form method="POST" commandName="geolocation">
<form:input path="latitude" id="latitude" type="text" />
<form:input path="longitude" id="longitude" type="text" />
<input id="bigbutton" type="submit" onclick="getLocation()" value="Confirm" />
</form:form>

但它返回我零。怎么了?这是我的表单类:

private double latitude;
    private double longitude;


    public double getLatitude() {
        return latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }


    public double getLongitude() {
        return longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

控制器:

@RequestMapping(value = "/about", method = RequestMethod.GET)
    public String getAboutPage(Map<String, Object> map,
            @ModelAttribute("geolocation") GeoLocation geolocation,
            @ModelAttribute("search") SearchForm query, BindingResult result) {

        map.put("news", new News());
        map.put("newsList", newsService.getAboutPage());
        map.put("temp", TEMP);

        return "about";
    }


    @RequestMapping(value = "/about", method = RequestMethod.POST)
    public String getAboutPageProcess(Map<String, Object> map,
            @ModelAttribute("geolocation") GeoLocation geolocation,
            @ModelAttribute("search") SearchForm query, BindingResult result) {

        System.out.println(geolocation.getLatitude());
        System.out.println(geolocation.getLongitude());
        map.put("news", new News());
        map.put("newsList", newsService.getAboutPage());
        map.put("temp", TEMP);

        return "about";
    }
4

1 回答 1

1

要么使用onsubmitgetLocation返回 false,然后在 中手动提交表单showPosition,要么使用 jQuery/YUI/somethinggetLocation作为非破坏性事件处理程序附加。

编辑:也许是这样的:http: //jsfiddle.net/mz6zN/

于 2013-08-15T13:34:15.637 回答