1

我正在尝试重新设计 phonegap 应用程序以使用 googlemaps 插件和 v2 的 google maps API。

我正在努力更新标记位置,这似乎在 v3 中完美运行,但不适用于插件。

问题似乎是在调用时setMarkerPosition(),标记对象没有被传递给它,所以我得到“无法调用未定义的方法 setPosition”

我已currentPositionMarker在脚本顶部设置为全局变量,虽然我setCurrentPosition()在一开始就定义了它,但我也尝试使用回调再次定义它,但都不起作用。

抱歉,如果它有些愚蠢,这是我的第一个 JavaScript 项目,因此对语言的某些部分的理解仍然非常不完整,因此非常感谢任何指针。

我正在尝试的代码...

function initializeMap()
                {    map = plugin.google.maps.Map.getMap();
                    // map = new plugin.google.maps.Map(document.getElementById('map_canvas'), {
                    //   zoom: 13,

                    // });
                }

    function initLocationProcedure() {
                    initializeMap();
                    if (navigator.geolocation) {
                        navigator.geolocation.getCurrentPosition(
                            displayAndWatch,
                            errorCallback_highAccuracy,
                            {maximumAge:600000, timeout:5000, enableHighAccuracy: true});
                    } else {
                        alert("Your Phone does not support Geolocation");
                    }
                }

    function displayAndWatch(position) {
                    // set current position
                    setCurrentPosition(position);
                    // watch position
                    watchCurrentPosition();
                }

     function errorCallback_highAccuracy(position) {
      }           

    function watchCurrentPosition() {
                    var positionTimer = navigator.geolocation.watchPosition(
                        function (position) { setMarkerPosition(currentPositionMarker,position);
                        }, error, {maximumAge:600000, timeout:5000, enableHighAccuracy: true});
                }
    function error(){
                    }    

    function setMarkerPosition(marker, position) {
           marker.setPosition(
                        new plugin.google.maps.LatLng(
                            position.coords.latitude,
                            position.coords.longitude)

                    );
       }

    function setCurrentPosition(pos) {
    currentPositionMarker = map.addMarker({
      'position': new plugin.google.maps.LatLng(
                            pos.coords.latitude,
                            pos.coords.longitude
                        ),

                }, function(marker) {
        currentPositionMarker = marker;
                });
                    map.setCenter(new plugin.google.maps.LatLng(
                                                    pos.coords.latitude,
                                                    pos.coords.longitude
                                                ));
                }
4

1 回答 1

0

好的,事实证明正在创建标记 var,但不是及时setCurrentPosition(),因为设置标记正在被异步调用setMarkerPosition()。完全删除displayAndWatch()然后setMarkerPosition()从回调中调用setCurrentPosition()似乎是解决方法。

function setCurrentPosition(pos) {
                    map.addMarker({
  'position': new plugin.google.maps.LatLng(
                        pos.coords.latitude,
                        pos.coords.longitude
                    ),

            }, function(marker) {
                        currentPositionMarker = marker;
                        watchCurrentPosition();
            });
                map.setCenter(new plugin.google.maps.LatLng(
                                                pos.coords.latitude,
                                                pos.coords.longitude
                                            ));


            }
于 2015-02-16T15:55:40.530 回答