Google Maps JS API v3未与Phonegap iOS 3.5.0和jQuery Mobile进行地理定位

时间:2014-07-21 13:43:46

标签: jquery-mobile cordova google-geolocation

设置

目前正在建立我的手机间隙iOS应用程序,其中包括地理定位,以及将结果发送到数据库并返回附近的位置。因为我正在使用jQuery Mobile和Phonegap,所以我在浏览器上进行了完美的测试,地理定位是成功的,所有返回的内容也是如此。在iOS设备上,没有那么多。当发送设置的纬度和经度时,一切都成功返回,但是当使用谷歌地图地理定位,并最终使用地理定位的cordova插件时,两者都失败了,因为无法找到我的位置,即使它们是完全相同的代码,减去手机间隙网站上的初始化程序。

守则

的jQuery

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
$(document).ready(function () {
    //Load Google Map & Geolocate
    $(document).on("pagecreate", "#game", function () {
        // keep as separate function call to allow for DOM to update after a tag
        loadGoogleMap();
    });

    function loadGoogleMap() {
            var defaultLatLng = new google.maps.LatLng(34.0983425, -118.3267434); //default location is Hollywood, CA

            if (navigator.geolocation) {
                function success(pos) {
                    //set fields for current user latitude and longitude
                    $("#lat_in").val(pos.coords.latitude);
                    $("#lon_in").val(pos.coords.longitude);

                    //location found
                    drawMap(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude));
                }

                function fail(error) {
                    //try cordova geolocation plugin
                    function onSuccess(position) {
                        alert("CDV position success");
                        //alert(position.coords.latitude);
                        //alert(position.coords.longitude);
                    }

                    function onError(error) {
                        alert("CDV position error");
                        //alert("code: " + error.code + "\n" + "message: " + error.message);
                    }

                    navigator.geolocation.getCurrentPosition(onSuccess, onError);

                    alert("CDV position fail. Google geolocate fail"); //WHERE THE ERROR IS THROWN. THIS IS ALWAYS THE OUTCOME

                    //drawMap(defaultLatLng); //Failed to find location. Show default location
                }

                //Geolocate. Cache location for 5 mins. Timeout after 6 seconds
                navigator.geolocation.getCurrentPosition(success, fail, {
                    maximumAge: 500000,
                    enableHighAccuracy: true,
                    timeout: 6000
                });
            } else {
                drawMap(defaultLatLng); //No geolocation support, show default map
            }

            function drawMap(latlng) {
                    //Collect Data to Send
                    var uname = $("#unm_in").val();
                    var lat = $("#lat_in").val();
                    var lon = $("#lon_in").val();

                    var myOptions = {
                        zoom: 16,
                        center: latlng,
                        mapTypeId: google.maps.MapTypeId.ROADMAP
                    };

                    var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);

                    //update latitude and longitude on DB for logged in user && get nearby users
                    $.post("PHP_ON_SERVER", {
                        uname: uname,
                        lat: lat,
                        lon: lon
                    }, function (data) {
                        $.each(data, function (key, value) {
                            //Define variables
                            var allUsrLoc = new google.maps.LatLng(value.lat, value.lon);
                            var contentString = value.uname;
                            var mkrImg = "inUsrIcon.png"; //icon for logged in user
                            var inUserName = value.fname + " " + value.lname;

                            //if info belongs to current user
                            if (contentString === uname) {
                                var marker = new google.maps.Marker({
                                    position: allUsrLoc,
                                    map: map,
                                    title: "Player",
                                    icon: mkrImg
                                });

                                //change header to name and points
                                $("#inUserInfo").text("Hi " + inUserName + " || You have " + value.points + " points");

                                //disable tag button if not it
                                if (value.isit === "notit") {
                                    $("#tagBtn").addClass("ui-state-disabled");
                                }
                            }

                            //if not current user and is not it
                            else if (contentString != uname && value.isit != "it") {
                                var marker = new google.maps.Marker({
                                    position: allUsrLoc,
                                    map: map,
                                    title: "Player"
                                });

                                google.maps.event.addListener(marker, 'click', function () {
                                    //Change selected player hidden field to match marker
                                    $("#unm_sel").val(contentString);
                                    $("#lat_sel").val(value.lat);
                                    $("#lon_sel").val(value.lon);
                                    $("#isit_sel").val(value.isit);

                                    //Change tag button to selected player's username
                                    $("#tagBtn").text("Tag " + contentString);
                                });
                            }

                            //no condition for if not current user and is it. TBD if anything will come
                        });
                    }, "json");
                } //end drawMap
        } // end loadGoogleMap
}

HTML

<div data-role="content" role="main" id="map-canvas">
<!-- Map loads here -->
</div>

需要了解的更多内容

我一直在阅读其他相关问题,但其他人都没有工作。要么他们说使用插件,我无济于事,或者设置我已经在做的<access origin="*" />。此外,大多数其他问题都围绕着3.0.0之前版本的手机缺口,这是它自己的怪物。此外,地图加载,只显示没有标记。在DB上,正在更新位置的用户被设置为(0,0)。最后,在任何其他应用程序中使用地理位置时,我测试的iOS设备已成功找到我的位置。另外,我在与浏览器相同的WiFi网络上进行测试,因此排除了由于网络或硬件导致内部无法定位的任何内容。

如果您想查看工作示例,请询问。

0 个答案:

没有答案