gpt4 book ai didi

javascript - PhoneGap + 谷歌地图 API v3 : Not displaying (at all) on Android

转载 作者:可可西里 更新时间:2023-11-01 18:46:05 26 4
gpt4 key购买 nike

情况

我目前正在为一位希望集成 Google map 的客户开发应用程序。他希望 map 显示从用户所在位置到他办公室的路线。

我在 Windows 8 上工作,没有任何 IDE(使用 Sublime Text 2)。

我已经设法让它工作 a) 在我的本地 Chrome 浏览器中,b) 在 Ripple Emulator for PhoneGap/Cordova >2.0.0 中。但是,每当我尝试时,它都无法在我的 Android 手机 (HTC Sensation) 上运行。这让我发疯,我正准备放弃它并找到其他一些“笨拙”的解决方案(如静态 map 或 geo:url 界面)。

在尝试实际实现 map 之前,我运行了 PhoneGap Geolocation 完整示例,发现 here .我注意到我的 Android 手机确实正确显示了我的当前位置(纬度/经度/时间戳等)。因此,我相信我的手机上设置了正确的权限(位置 -> 等)。

问题

Google map 根本没有显示在我的 Android 设备上。我看到红色背景(用于调试),所以我知道高度和宽度没问题。但我没有看到任何谷歌地图的迹象(没有按钮、叠加层、网格或任何东西)。

代码

用于加载 jQuery、Cordova 和 Maps API v3 的 HTML 代码:

<script type="text/javascript" src="js/jquery-1.10.0.min.js" ></script>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3&sensor=true&language=da"></script>

这是我用来放置 map 的 HTML:

<div id="map-canvas"
style="display:block;
overflow:hidden;
position:relative;
border:1px solid #aaaaaa;
width: 100%;
height: 400px;
background: red;">
</div>
<div id="map-panel" style="width:100%; height:90%; position:relative; "></div>

这是我完整的 Google Maps JS(在它自己的文件中):

var map,
userPosition,
officeLocation,
directionsDisplay,
directionsService;

google.maps.event.addDomListener(window, 'load', setup);

function setup() {
document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
navigator.geolocation.getCurrentPosition(onSuccess, onError, {enableHighAccuracy:true});
}
}

function onSuccess(position) {
userPosition = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
navigator.notification.alert("Found user position");

initializeMaps();
//$('#map-canvas').css({'height': $(window).height()/2, 'width': '99%'});
}

function onError(error) {
navigator.notification.alert("code: " + error.code + ",\n" +
"message: " + error.message);
}

function initializeMaps() {
directionsDisplay = new google.maps.DirectionsRenderer();
directionsService = new google.maps.DirectionsService();

officeLocation = new google.maps.LatLng(55.689403, 12.521281);

var myOptions = {
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: officeLocation
};

map = new google.maps.Map(document.getElementById('map-canvas'), myOptions);
directionsDisplay.setMap(map);

if (userPosition != '') {
var userPosMarker = new google.maps.Marker({
position: userPosition,
map: map,
title: "Din Placering"
});

calculateRoute();
}
else {
navigator.notification.alert("userPosition is null");
}
}

function calculateRoute() {
//navigator.notification.alert("calculateRoute");
var request = {
origin: userPosition,
destination: officeLocation,
travelMode: google.maps.DirectionsTravelMode["DRIVING"]
};

directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setPanel(document.getElementById('map-panel'));
directionsDisplay.setDirections(response);

//navigator.notification.alert("Show directions");
}
else {
navigator.notification.alert("Got status NOT OK from google");
}
});
}

function reloadGoogleMap() {
if (map === null || map === undefined) {
navigator.notification.alert("map is %s", map);
}
else {
var currCenter = map.getCenter();
google.maps.event.trigger(map, "resize");
map.setCenter(currCenter);
map.setZoom(12);
//navigator.notification.alert("reloaded map");
}
}

这是我的初始化代码(放在我的 head 标签的底部):

<script type="text/javascript" charset="utf-8">
//navigator.notification.alert("listen for deviceready");
document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
//navigator.notification.alert("device ready");
...
// calls other initialization functions
...
initMaps();
..
}
</script>

我的 AndroidManifest.xml 中有这些(以及其他):

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.INTERNET" />

我的 config.xml 中有这些(位于/assets/www/config.xml):

<access origin="*" />

...

<feature name="http://api.phonegap.com/1.0/device"/>
<feature name="http://api.phonegap.com/1.0/geolocation"/>
<feature name="http://api.phonegap.com/1.0/notification"/>

在我看来,我的 onSuccess 方法从未被调用过,但我没有看到任何表明用户位置无效的警报。事实上,我确实在手机上看到的唯一通知是:

map is %s, undefined

在 ripple 模拟器中,每当我加载应用程序时,我都会得到“找到的用户位置”。

请帮忙!

[编辑]我忘了说我正在使用 Adob​​e 的 http://build.phonegap.com实际构建应用程序。

[编辑2]我现在尝试像这样使用 Google API key :

<script src="http://maps.google.com/maps/api/js?key=AIzaSyB1uhDdWjtNEl9K35lJtuq5Sw2BjKR8-OM&sensor=false" type="text/javascript"></script>

但是,唉,没有变化。还是什么都没有。

[编辑3]

这是我的完整androidManifest.xml:

<code>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:windowSoftInputMode="adjustPan"
package="com.alphastagestudios.danmarksflyttemand" android:versionName="1.0" android:versionCode="2" android:hardwareAccelerated="true">
<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:xlargeScreens="true"
android:resizeable="true"
android:anyDensity="true"
/>

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.RECORD_VIDEO"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />


<application android:icon="@drawable/icon" android:label="@string/app_name"
android:hardwareAccelerated="true"
android:debuggable="true">
<activity android:name="DanmarksFlyttemandApp" android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="17"/>

<permission android:name="com.alphastagestudios.danmarksflyttemand.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
<uses-permission android:name="com.alphastagestudios.danmarksflyttemand.permission.MAPS_RECEIVE"/>
</manifest>

</code>

[编辑4]我现在尝试使用 PhoneGap 地理定位和基本谷歌地图构建一个最小示例。我通过 Eclipse 使用 Cordova v. 2.9.0(最新)手动构建它。奇怪的是,PhoneGap 地理定位示例本身运行良好,但当我引入谷歌地图代码时,它全部停止工作。我在使用和不使用 google API key 的情况下尝试了这个最小示例。没有区别。

这是我使用的 index.html:

<!DOCTYPE html>
<html>
<head>
<title>Device Properties Example</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">

<style>
body, html, #map-canvas {
width: 100%;
height: 400px;
margin: 0;
padding: 0;
}
</style>

<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script type="text/javascript" charset="utf-8">

// Wait for device API libraries to load
document.addEventListener("deviceready", onDeviceReady, false);

// device APIs are available
function onDeviceReady() {
navigator.geolocation.getCurrentPosition(onSuccess, onError);
}

// onSuccess Geolocation
function onSuccess(position) {
var element = document.getElementById('geolocation');
element.innerHTML = 'Latitude: ' + position.coords.latitude + '<br />' +
'Longitude: ' + position.coords.longitude + '<br />' +
'Altitude: ' + position.coords.altitude + '<br />' +
'Accuracy: ' + position.coords.accuracy + '<br />' +
'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '<br />' +
'Heading: ' + position.coords.heading + '<br />' +
'Speed: ' + position.coords.speed + '<br />' +
'Timestamp: ' + position.timestamp + '<br />';
initMap();
}

// onError Callback receives a PositionError object
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}

var map,
userPosition,
officeLocation,
directionsDisplay,
directionsService;

function initMap() {
//directionsDisplay = new google.maps.DirectionsRenderer();
//directionsService = new google.maps.DirectionsService();

officeLocation = new google.maps.LatLng(55.689403, 12.521281);
var myOptions = {
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: officeLocation
};

map = new google.maps.Map(document.getElementById('map-canvas'), myOptions);
//directionsDisplay.setMap(map);
}

</script>

</head>
<body>
<p id="geolocation">Finding geolocation...</p>

<div id="map-canvas"></div>
</body>
</html>

[编辑5]我现在尝试使用 PhoneGap (Cordova)v 构建应用程序。 2.6.0、2.9.0 和 2.8.1 - 均无效。手机的地理定位工作正常,但谷歌地图没有显示。我只看到它应该在的默认灰色背景。

最佳答案

我遇到了同样的问题,结果证明只是我在创建 phonegap 项目后没有更新白名单。 Phonegap 拒绝了 google maps JS URL,因此它从未被下载和执行。

例如:

 <access origin="*.google.com"/>
<access origin="*.googleapis.com"/>

关于javascript - PhoneGap + 谷歌地图 API v3 : Not displaying (at all) on Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17423558/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com