【问题标题】:Map APIs for windows PHONE 8.1 in HTML, CSS, JS以 HTML、CSS、JS 格式映射 Windows PHONE 8.1 的 API
【发布时间】:2014-07-16 20:41:26
【问题描述】:

我知道有一个类似的问题 (Map APIs not working for windows phone in HTML),但我没有足够的声誉来发表评论,所以我不得不问我自己的问题。

我已成功将 bing 地图添加到我的 windows 8.1 商店应用程序(HTML、JS、CSS),但是当我尝试将 Visual Studio(地图 SDK)中的引用添加到 windows phone 8.1 时,我无法使用该选项。我完成了之前提出的问题的答案,示例中有一个 Windows 商店应用程序(很好!)、一个移动 Web 应用程序和一个 C# 示例。所以我尝试了移动网络版本,但只是添加了这个链接

<script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0&mkt=en-gb"></script> 

显然给出了您无法将远程脚本添加为本地脚本的错误(类似这些行)

所以我尝试从上面的 url 复制代码并在本地保存,但这给了我关于脚本尝试注入动态内容的错误。现在,对于下一步要尝试什么,我有点不知所措...任何有用的内容的建议/链接将不胜感激!

我应该提一下,我看过很多关于添加地图的文档,并且它们包含在 windows phone 8.1 sdk 中,所以你不需要 bing 地图 sdk 但是这些主要是 xaml,有没有办法访问通过js实现sdk?

【问题讨论】:

    标签: javascript html windows-phone-8.1 bing-maps


    【解决方案1】:

    实际上你可以将 Win8.1 的 bing APi 添加到 WP8.1,只需破解 bing map APi dll 并将文件设置到 Visual Studio 并像这样添加 th bing 引用

     <!-- Bing Maps references -->
    <script type="text/javascript" src="/Bing.Maps.JavaScript/js/veapicore.js"></script>
    <script type="text/javascript" src="/Bing.Maps.JavaScript/js/veapiModules.js"></script>
    

    然后像使用 Win8.1 一样正常工作, 看我的项目。顺便添加一个代码来验证连接状态,导致这个地图只有在连接退出时才能工作(如果你手动添加一些图钉,它可以在没有连接的情况下工作)

    Fair Of Sfax WP

    【讨论】:

    • 应该避免黑客攻击。他们会在某个时候破裂。通常在您的应用运行至关重要时。
    • 我似乎有一个问题,即在获取方向时,两点之间的距离没有返回,其他一切正常,但我没有得到路线摘要或距离,无法弄清楚为什么,方向有效和老好...
    • 不,它工作正常,因为我已经编写了 2 点路由,请参阅 Win 8.1 上的示例并执行相同操作。
    【解决方案2】:

    因为 Bing 地图扩展仅适用于 Windows 8.1 应用程序(希望他们会更新它...),并且因为您不能在 WinJS 页面控件中包含外部 JS 库引用,所以这就是您可以获得 Bing 地图的方式开发 WP8.1 HTML/JS 应用程序:

    在您的页面控制 html 中插入一个 webview,例如:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8" />
    <title>mapcontrol</title>
    
    </head>
    <body>
        <div class="mapcontrol fragment">
            <section id="mapcanvas" class="" aria-label="Main content" role="main">
                <x-ms-webview id="Map" style="width:100%; height:100%"></x-ms-webview>
            </section>
        </div>
    </body>
    </html>
    

    创建一个HTML页面和一个JS文件,并放入创建地图所​​需的所有代码:

    HTML 页面(一个 WP8 源示例的副本):

    <!DOCTYPE html>
    <html>
    <head>
    <title>Bing Map</title>
    <script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx? v=7.0&mkt=en-gb"></script>
    <script type="text/javascript" src="/pages/bingmap/bingmap.js"></script>
    </head>
    <body>
    <div id="mapContainer">
        <div id='myMap'></div>
        <div class="appBar">
            <div class="appBarContainer">
                <img id="searchBtn" src="images/search.png" alt="searchBtn" />
                <img id="gpsBtn" src="images/gps.png" alt="gpsBtn" />
                <img id="trafficBtn" src="images/traffic.png" alt="trafficBtn" />
                <img id="mapModeBtn" src="images/layers.png" alt="mapModeBtn" />
            </div>
        </div>
        <div id="navBar">
            <img id="upBtn" src="images/up.png" alt="upBtn" />
            <img id="leftBtn" src="images/left.png" alt="leftBtn" />
            <img id="rightBtn" src="images/right.png" alt="rightBtn" />
            <img id="downBtn" src="images/down.png" alt="downBtn" />
        </div>
        <div id="zoomRotateBar">
            <img id="zoomInBtn" src="images/zoomIn.png" alt="zoomInBtn" />
            <img id="zoomOutBtn" src="images/zoomOut.png" alt="zoomOutBtn" />
            <div id="rotationBtns">
                <img id="rotateCWBtn" src="images/cwRotation.png" alt="rotateCWBtn" />
                <img id="rotateCCWBtn" src="images/ccwRotation.png" alt="rotateCCWBtn" />
            </div>
        </div>
    </div>
    
    <div id="searchPanel" style="display:none;">
        <h2>Search</h2>
        <input type="text" id="searchTbx" />
    </div>
    
    <div id="mapModePanel" style="display:none;">
        <h2>Map Mode</h2>
    
        <ul>
            <li>
                <input type="radio" name="mapMode" id="autoMode" value="auto" checked="checked" />
                <label for="autoMode">Auto</label>
            </li>
            <li>
                <input type="radio" name="mapMode" id="aerialMode" value="aerial" />
                <label for="aerialMode">Aerial</label>
            </li>
            <li>
                <input type="radio" name="mapMode" id="birdseyeMode" value="birdseye" />
                <label for="birdseyeMode">Birdseye</label>
            </li>
            <li>
                <input type="radio" name="mapMode" id="osMode" value="os" />
                <label for="osMode">Ordnance Survey</label>
            </li>
            <li>
                <input type="radio" name="mapMode" id="roadMode" value="road" />
                <label for="roadMode">Road</label>
            </li>
        </ul>
    </div>
    </body>
    </html>
    

    此页面的JS代码(示例源的副本):

    (function () {
        var map,
        searchManager,
        geoLocationProvider,
        gpsLayer,
        gpsEnabled = false,
        trafficLayer,
        trafficEnabled = false;
    
    // INIZIO INIT
    function init() {
    
        getMap();
    
        //Test for multi-touch support
        if (navigator.msMaxTouchPoints && navigator.msMaxTouchPoints > 1) {
            //Hide zoom and rotate controls
            document.getElementById('zoomRotateBar').style.display = 'none';
        } else {
            //Only display rotation buttons when the map style is birdseye
            Microsoft.Maps.Events.addHandler(map, 'maptypechanged', updateNavBar);
            Microsoft.Maps.Events.addHandler(map, 'viewchangeend', updateNavBar);
    
            //Add zooming and rotating functionality
            addListener(document.getElementById('zoomInBtn'), 'click', function () {
                map.setView({ zoom: map.getZoom() + 1 });
            });
    
            addListener(document.getElementById('zoomOutBtn'), 'click', function () {
                map.setView({ zoom: map.getZoom() - 1 });
            });
    
            addListener(document.getElementById('rotateCWBtn'), 'click', function () {
                map.setView({ heading: map.getHeading() + 90 });
            });
    
            addListener(document.getElementById('rotateCCWBtn'), 'click', function () {
                map.setView({ heading: map.getHeading() - 90 });
            });
        }
    
        //Test for single-touch support
        if (navigator.msMaxTouchPoints && navigator.msMaxTouchPoints > 0) {
            //Hide navigation controls
            document.getElementById('navBar').style.display = 'none';
        } else {
            //Add panning functionality
            addListener(document.getElementById('upBtn'), 'click', function () {
                map.setView({ center: map.getCenter(), centerOffset: new Microsoft.Maps.Point(0, 100) });
            });
    
            addListener(document.getElementById('leftBtn'), 'click', function () {
                map.setView({ center: map.getCenter(), centerOffset: new Microsoft.Maps.Point(100, 0) });
            });
    
            addListener(document.getElementById('rightBtn'), 'click', function () {
                map.setView({ center: map.getCenter(), centerOffset: new Microsoft.Maps.Point(-100, 0) });
            });
    
            addListener(document.getElementById('downBtn'), 'click', function () {
                map.setView({ center: map.getCenter(), centerOffset: new Microsoft.Maps.Point(0, -100) });
            });
        }
    
        addListener(document.getElementById('searchBtn'), 'click', function () {
            document.getElementById('searchPanel').style.display = '';
            document.getElementById('searchTbx').focus();
        });
    
        addListener(document.getElementById('searchTbx'), 'keydown', function (e) {
            if (!e) {
                e = window.event;
            }
    
            //process search when enter key pressed
            if (e.keyCode == 13) {
                search(this.value);
            }
        });
    
        addListener(document.getElementById('gpsBtn'), 'click', toggleGPS);
    
        addListener(document.getElementById('trafficBtn'), 'click', toggleTraffic);
    
        addListener(document.getElementById('mapModeBtn'), 'click', function () {
            document.getElementById('mapModePanel').style.display = '';
        });
    
        var mapModeBtns = document.getElementsByName('mapMode');
    
        for (var i = 0; i < mapModeBtns.length; i++) {
            addListener(mapModeBtns[i], 'click', function () {
                setMapMode(this.value);
    
                document.getElementById('mapModePanel').style.display = 'none';
            });
        }
    }
    // FINE INIT
    
    
    
    function getMap() {
        var mapOptions = {
            credentials: "YOUR BING MAPS KEY",
            showDashboard: false,
            showCopyright: false,
            showScalebar: false,
            enableSearchLogo: false,
            enableClickableLogo: false,
            backgroundColor: new Microsoft.Maps.Color(255, 0, 0, 0)
        };
    
        // Initialize the map
        map = new Microsoft.Maps.Map(document.getElementById("myMap"), mapOptions);
    
        gpsLayer = new Microsoft.Maps.EntityCollection();
        map.entities.push(gpsLayer);
    }
    
    function updateNavBar() {
        if (map.isRotationEnabled()) {
            document.getElementById('rotationBtns').style.display = '';
        } else {
            document.getElementById('rotationBtns').style.display = 'none';
        }
    }
    
    function search(query) {
        if (searchManager) {
            var request = {
                where: query,
                count: 1,
                callback: geocodeCallback,
                errorCallback: geocodeError
            };
    
            searchManager.geocode(request);
        } else {
            //Load the Search module and create a search manager.
            Microsoft.Maps.loadModule('Microsoft.Maps.Search', {
                callback: function () {
                    //Create the search manager
                    searchManager = new Microsoft.Maps.Search.SearchManager(map);
    
                    //Perfrom search logic
                    search(query);
                }
            });
        }
    }
    
    function geocodeCallback(response, userData) {
        if (response &&
            response.results &&
            response.results.length > 0) {
            var r = response.results[0];
            var l = new Microsoft.Maps.Location(r.location.latitude, r.location.longitude);
    
            //Display result on map        
            var p = new Microsoft.Maps.Pushpin(l);
            map.entities.push(p);
    
            //Zoom to result
            map.setView({ center: l, zoom: 15 });
        } else {
            showMessage("Not results found.");
        }
    
        document.getElementById('searchPanel').style.display = 'none';
    }
    
    function geocodeError(request) {
        showMessage("Unable to Geocode request.");
    
        document.getElementById('searchPanel').style.display = 'none';
    }
    
    function toggleGPS() {
        gpsEnabled = !gpsEnabled;
    
        // Initialize the location provider
        if (!geoLocationProvider) {
            geoLocationProvider = new Microsoft.Maps.GeoLocationProvider(map);
        }
    
        //Clear the GPS layer 
        gpsLayer.clear();
    
        if (gpsEnabled) {
            // Get the user's current location
            geoLocationProvider.getCurrentPosition({
                successCallback: function (e) {
                    gpsLayer.push(new Microsoft.Maps.Pushpin(e.center));
                },
                errorCallback: function (e) {
                    showMessage(e.internalError);
                }
            });
        } else {
            //Remove the accuracy circle and cancel any request that might be processing
            geoLocationProvider.removeAccuracyCircle();
            geoLocationProvider.cancelCurrentRequest();
        }
    }
    
    function toggleTraffic() {
        trafficEnabled = !trafficEnabled;
    
        //Check to see if the traffic layer exists
        if (trafficLayer) {
            if (trafficEnabled) {
                trafficLayer.show();
            } else {
                trafficLayer.hide();
            }
        } else {
            //Load the traffic module and create the traffic layer.
            Microsoft.Maps.loadModule('Microsoft.Maps.Traffic', {
                callback: function () {
                    //Create the traffic layer
                    trafficLayer = new Microsoft.Maps.Traffic.TrafficLayer(map);
    
                    //Get the base tile layer and set the opacity
                    var layer = trafficLayer.getTileLayer();
                    layer.setOptions({ opacity: 0.5 });
    
                    trafficLayer.show();
                }
            });
        }
    }
    
    function setMapMode(mode) {
        var m;
    
        switch (mode) {
            case 'auto':
                m = Microsoft.Maps.MapTypeId.auto;
                break;
            case 'aerial':
                m = Microsoft.Maps.MapTypeId.aerial;
                break;
            case 'birdseye':
                m = Microsoft.Maps.MapTypeId.birdseye;
                break;
            case 'os':
                m = Microsoft.Maps.MapTypeId.ordnanceSurvey;
                break;
            case 'road':
            default:
                m = Microsoft.Maps.MapTypeId.road;
                break;
        }
    
        map.setView({ mapTypeId: m });
    }
    
    function showMessage(msg) {
        try {
            alert(msg);
        }
        catch (e) {
            if (Windows != null &&
                Windows.UI != null &&
                Windows.UI.Popups != null) {
                var popup = Windows.UI.Popups.MessageDialog(msg);
                popup.showAsync();
            }
        }
    }
    
    //Cross browser support for adding events. Mainly for IE7/8
    function addListener(element, eventName, eventHandler) {
        if (element.addEventListener) {
            element.addEventListener(eventName, eventHandler, false);
        } else if (element.attachEvent) {
            if (eventName == 'DOMContentLoaded') {
                eventName = 'readystatechange';
            }
            element.attachEvent('on' + eventName, eventHandler);
        }
    }
    
    addListener(document, 'DOMContentLoaded', init);
    })();
    

    (使用您的 Bing 地图开发者密钥更改“您的 BING 地图密钥”) 然后在您的 Page Control JS 代码中导航到 webview 中托管的页面:

    // For an introduction to the Page Control template, see the following documentation:
    // http://go.microsoft.com/fwlink/?LinkId=232511
    (function () {
        "use strict";
    
    var ControlConstructor = WinJS.UI.Pages.define("/pages/mapcontrol/mapcontrol.html", {
        // This function is called whenever a user navigates to this page. It
        // populates the page elements with the app's data.
        ready: function (element, options) {
            //// TODO: Initialize the page here.
    
    
            var webview = document.getElementById("Map");
            webview.navigate("ms-appx-web:///pages/bingmap/bingmap.html");
    
    
        },
    
    
    });
    
    })();
    

    【讨论】:

    • 我试过这个,虽然它似乎加载了外部 js (
    【解决方案3】:

    使用您引用的 Bing Maps JavaScript 控件在 WP8 和 8.1 中确实有效。不久前,我整理了一个适用于 WP、Win8 和 Web 的跨平台代码示例,并在此处提供:http://code.msdn.microsoft.com/Cross-Platform-Bing-Maps-e96600d5

    【讨论】:

    • 谢谢你的回复,我已经看过你的例子了,但是windows phone例子没有使用winjs,在线版本我无法使用上面的ms-appx-web建议!
    • 听起来您正在尝试在地图控件完全加载外部文件之前使用它。在尝试加载地图之前,请确保 DOM 已加载。在我指出的代码示例中,我通过将以下内容添加到我的 JavaScript 文件的末尾来做到这一点: document.addEventListener('DOMContentLoaded', GetMap, false);
    猜你喜欢
    • 1970-01-01
    • 2015-12-20
    • 2015-01-21
    • 1970-01-01
    • 1970-01-01
    • 2014-07-30
    • 1970-01-01
    • 1970-01-01
    • 2015-02-13
    相关资源
    最近更新 更多