Android Phonegap - 如何打开本地谷歌地图应用程序 [英] Android Phonegap - How to Open Native Google Maps Application

查看:31
本文介绍了Android Phonegap - 如何打开本地谷歌地图应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Phonegap 开发 Android 应用程序,但无法弄清楚如何打开本机 Google 地图应用程序以显示路线.我通过插入 URL 打开地图没有任何问题,但打开应用程序让我难倒.目前只能在iOS平台上找到Phonegap相关的开发建议.

我已将谷歌地图添加到白名单权限,在我的脚本标签中包含正确的 Cordova.js 文件和谷歌地图链接,在我的 AndroidManifest.xml 文件中拥有正确的权限,包括 Cordova.jar 和谷歌地图 API我的构建路径,res目录中有Phonegap xml文件夹,assets/www目录中有js文件,libs目录中有jar文件.

我想要完成的事情:

    1. 单击链接后,打开本机 Google 地图应用程序.如果设备上未安装该应用程序,则通知用户未安装谷歌地图,必须安装.
      一个.我已经在检查网络连接并按照应该完成的方式进行处理.
    2. 谷歌地图打开,显示从用户当前位置到点击链接位置的路线.

以下示例在我的 Android 设备上的运行方式与在 iOS 上的运行方式完全相同,但显然无法打开 Google 地图应用程序的操作.

href="http://maps.google.com/maps?q=TD 华盛顿特区"><img src="img/ico_pin.png"/>白宫</a></a>

第二个示例通过包含结束位置对第一个示例进行了添加,尽管我无法弄清楚如何插入当前位置.但最重要的是,它仍然无法打开 Google 地图应用程序.

<a href="javascript:openMaps('daddr=1600+Pennsylvania+Ave+NW+Washington+DC+20500+USA');"><img src="img/ico_pin.png"/>白宫</a>函数 openMaps(querystring) {var maproot = '';maproot = 'http://maps.google.com/maps?saddr=Current+Location&';window.location = maproot + 查询字符串;}

在下面的第三个示例中,我能够在我的应用程序中显示地图.它确实显示了正确的路线(从 A 点到 B 点的俯视图),但同样没有打开实际的 Google 地图应用程序.

<img src="img/ico_pin.png"/>白宫</a><div data-role="内容"><div class="ui-bar-c ui-corner-all ui-shadow" style="padding: 1em;"><div id="mapdirections_canvas" style="height: 300px;"></div>

函数 getDirections() {var directionService = new google.maps.DirectionsService();无功地图;var directionDisplay = new google.maps.DirectionsRenderer();var mapOptions = {缩放:9,缩放控制:真,mapTypeId: google.maps.MapTypeId.ROADMAP};var map = new google.maps.Map(document.getElementById("mapdirections_canvas"), mapOptions);DirectionsDisplay.setMap(地图);var myLatLng = new google.maps.LatLng(gmmLat, gmmLong);如果(document.getElementById(whDirections")){无功请求 = {来源:myLatLng,目的地:新 google.maps.LatLng(38.897096, -77.036545),旅行模式:google.maps.TravelMode.DRIVING};路线Service.route(请求,功能(结果,状态){如果(状态 == google.maps.DirectionsStatus.OK){DirectionsDisplay.setDirections(result);}});}

如果有人对此有链接或任何想法,我将非常感谢您的帮助.除了Hello World"应用程序,这是我的第一个移动应用程序.如果我错过了什么,或者我只是完全错误地做这件事,请告诉我.如果需要任何其他信息,请告诉我.

预先感谢您的帮助.

使用 geo:输入 uri.

打开地图

并且用户可以选择打开其设备上安装的任何地图应用程序.

I'm working on an Android application using Phonegap and am having trouble figuring out how to open the native Google Maps application to display directions. I don't have any issue opening a map by inserting a URL but opening the application has me stumped. So far I have only been able to find Phonegap related suggestions for development on the iOS platform.

I have added Google Maps to the whitelist permissions, am including the correct Cordova.js file and google maps link in my script tag, have the correct permissions in my AndroidManifest.xml file, included Cordova.jar and Google Map API in my build path, have the Phonegap xml folder in the res directory, js file in my assets/www directory, jar file in the libs directory.

What I am trying to accomplish:

    1. When a link is clicked, open the native Google Maps application. If the application is not installed on the device, notify the user that Google Maps is not installed and must be installed.
      a. I am already checking for network connectivity and handling that as it should be done.
    2. Google Maps opens displaying directions from the user's current location to the location of the link clicked.

The example below works on my Android device exactly the same as it does on iOS but obviously does not open the actions Google Maps application.

<a> href="http://maps.google.com/maps?q=TD Washington DC"><img src="img/ico_pin.png" />White House</a></a>

The second example adds on to the first by including the end location although I wasn't able to figure out how to insert the current location. Most importantly though, it still doesn't open the Google Maps application.

<a href="javascript:openMaps('daddr=1600+Pennsylvania+Ave+NW+Washington+DC+20500+USA');"><img src="img/ico_pin.png" />White House</a>

function openMaps(querystring) {
    var maproot = '';
    maproot = 'http://maps.google.com/maps?saddr=Current+Location&';
    window.location = maproot + querystring;
}

In the third example below, I'm able to display a map within my application. It does show the correct route (from an overhead view from point A to point B) but again doesn't open the actual Google Maps application.

<a id="whDirections" href="#mm_directions" onclick="getDirections()">
                    <img src="img/ico_pin.png" />White House</a>

<div data-role="content">
    <div class="ui-bar-c ui-corner-all ui-shadow" style="padding: 1em;">
        <div id="mapdirections_canvas" style="height: 300px;"></div>
    </div>
</div>

function getDirections() {
    var directionsService = new google.maps.DirectionsService();
    var map;
    var directionsDisplay = new google.maps.DirectionsRenderer();
    var mapOptions = {
        zoom: 9,
        zoomControl: true,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("mapdirections_canvas"), mapOptions);
    directionsDisplay.setMap(map);

    var myLatLng = new google.maps.LatLng(gmmLat, gmmLong);
    if (document.getElementById("whDirections")) {
        var request = {
            origin: myLatLng,
        destination: new google.maps.LatLng(38.897096, -77.036545), 
            travelMode: google.maps.TravelMode.DRIVING
    };
    directionsService.route(request, function(result, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(result); 
        }
    });
}

If anyone has a link or any idea on this, I would really appreciate the help. Other than a 'Hello World' app, this is my first mobile application. Please let me know if I have missed anything or if I am just doing this completely wrong. If any additional information is needed, please let me know.

Thanks in advance for the help.

解决方案

Use the geo: type uri.

<a href="geo:38.897096,-77.036545">open map</a>

and the user will get the choice of opening any of the mapping applications installed on their device.

这篇关于Android Phonegap - 如何打开本地谷歌地图应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
移动开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆