地图上不需要的灰色区域

时间:2013-10-24 11:13:20

标签: android cordova leaflet

我正在尝试在Android中使用PhoneGap和LeafLet。

我阅读了有关LeafLet的教程,我选择以mobile example

开头

我对这个灰色区域的问题..为什么会出现这个灰色区域? browser上没有灰色区域。除文件路径外,我没有更改任何内容。

如何制作全屏地图?

更新注意:我正在使用10“平板电脑横向模式,现在我在3,2”手机中尝试了此示例,并且3.2“屏幕没有问题.10”屏幕问题 - 景观模式

enter image description here

MainActivity.java

public class MainActivity extends DroidGap {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        appView= (CordovaWebView) findViewById(R.id.tutorialView);

        appView.loadUrl("file:///android_asset/www/main.html");

    }

}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <org.apache.cordova.CordovaWebView
            android:id="@+id/tutorialView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

</RelativeLayout>

main.html中

<!DOCTYPE html>
<html>
<head>
    <title>Leaflet mobile example</title>

    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

    <link rel="stylesheet" href="leaflet.css" />
    <!--[if lte IE 8]><link rel="stylesheet" href="leaflet.ie.css" /><![endif]-->

    <script src="leaflet.js"></script>

    <style>
        body {
            padding: 0;
            margin: 0;
        }
        html, body, #map {
            height: 100%;
        }
    </style>
</head>
<body>
    <div id="map"></div>

    <script>
        var map = L.map('map');

        L.tileLayer('http://{s}.tile.cloudmade.com/BC9A493B41014CAABB98F0471D759707/997/256/{z}/{x}/{y}.png', {
            maxZoom: 18,
            attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://cloudmade.com">CloudMade</a>'
        }).addTo(map);

        function onLocationFound(e) {
            var radius = e.accuracy / 2;

            L.marker(e.latlng).addTo(map)
                .bindPopup("You are within " + radius + " meters from this point").openPopup();

            L.circle(e.latlng, radius).addTo(map);
        }

        function onLocationError(e) {
            alert(e.message);
        }

        map.on('locationfound', onLocationFound);
        map.on('locationerror', onLocationError);

        map.locate({setView: true, maxZoom: 16});
    </script>
</body>
</html>

2 个答案:

答案 0 :(得分:2)

Leaflet doesn't always correctly infer the dimensions of maps without explicit dimensions.作为Leaflet此类问题的一般解决方案,请尝试将此添加到您的javascript:

setTimeout(map.invalidateSize.bind(map));

这将导致Leaflet重新计算地图的尺寸​​,并希望修复您正在看到的灰色瓷砖。

偶尔也可能需要进一步延迟重新计算,例如setTimeout(map.invalidateSize.bind(map),200);这可能会在不寻常的浏览器加载条件下修复灰色瓷砖。

答案 1 :(得分:0)

position:absolute元素设置#map帮助了我。试试这种风格:

<style>
    body {
        padding: 0;
        margin: 0;
    }
    #map {
        height: 100%;
        width: 100%;
        position: absolute;
    }
</style>