- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
情况
我目前正在为一位希望集成 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 模拟器中,每当我加载应用程序时,我都会得到“找到的用户位置”。
请帮忙!
[编辑]我忘了说我正在使用 Adobe 的 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/
我最近在/ drawable中添加了一些.gifs,以便可以将它们与按钮一起使用。这个工作正常(没有错误)。现在,当我重建/运行我的应用程序时,出现以下错误: Error: Gradle: Execu
Android 中有返回内部存储数据路径的方法吗? 我有 2 部 Android 智能手机(Samsung s2 和 s7 edge),我在其中安装了一个应用程序。我想使用位于这条路径中的 sqlit
这个问题在这里已经有了答案: What's the difference between "?android:" and "@android:" in an android layout xml f
我只想知道 android 开发手机、android 普通手机和 android root 手机之间的实际区别。 我们不能从实体店或除 android marketplace 以外的其他地方购买开发手
自Gradle更新以来,我正在努力使这个项目达到标准。这是一个团队项目,它使用的是android-apt插件。我已经进行了必要的语法更改(编译->实现和apt->注释处理器),但是编译器仍在告诉我存在
我是android和kotlin的新手,所以请原谅要解决的一个非常简单的问题! 我已经使用导航体系结构组件创建了一个基本应用程序,使用了底部的导航栏和三个导航选项。每个导航选项都指向一个专用片段,该片
我目前正在使用 Facebook official SDK for Android . 我现在正在使用高级示例应用程序,但我不知道如何让它获取应用程序墙/流/状态而不是登录的用户。 这可能吗?在那种情
我在下载文件时遇到问题, 我可以在模拟器中下载文件,但无法在手机上使用。我已经定义了上网和写入 SD 卡的权限。 我在服务器上有一个 doc 文件,如果用户单击下载。它下载文件。这在模拟器中工作正常但
这个问题在这里已经有了答案: What is the difference between gravity and layout_gravity in Android? (22 个答案) 关闭 9
任何人都可以告诉我什么是 android 缓存和应用程序缓存,因为当我们谈论缓存清理应用程序时,它的作用是,缓存清理概念是清理应用程序缓存还是像内存管理一样主存储、RAM、缓存是不同的并且据我所知,缓
假设应用程序 Foo 和 Eggs 在同一台 Android 设备上。任一应用程序都可以获取设备上所有应用程序的列表。一个应用程序是否有可能知道另一个应用程序是否已经运行以及运行了多长时间? 最佳答案
我有点困惑,我只看到了从 android 到 pc 或者从 android 到 pc 的例子。我需要制作一个从两部手机 (android) 连接的 android 应用程序进行视频聊天。我在想,我知道
用于使用 Android 以编程方式锁定屏幕。我从 Stackoverflow 之前关于此的问题中得到了一些好主意,并且我做得很好,但是当我运行该代码时,没有异常和错误。而且,屏幕没有锁定。请在这段代
文档说: android:layout_alignParentStart If true, makes the start edge of this view match the start edge
我不知道这两个属性和高度之间的区别。 以一个TextView为例,如果我将它的layout_width设置为wrap_content,并将它的width设置为50 dip,会发生什么情况? 最佳答案
这两个属性有什么关系?如果我有 android:noHistory="true",那么有 android:finishOnTaskLaunch="true" 有什么意义吗? 最佳答案 假设您的应用中有
我是新手,正在尝试理解以下 XML 代码: 查看 developer.android.com 上的文档,它说“starStyle”是 R.attr 中的常量, public static final
在下面的代码中,为什么当我设置时单选按钮的外观会发生变化 android:layout_width="fill_parent" 和 android:width="fill_parent" 我说的是
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
假设我有一个函数 fun myFunction(name:String, email:String){},当我调用这个函数时 myFunction('Ali', 'ali@test.com ') 如何
我是一名优秀的程序员,十分优秀!