Android Phonegap - 如何打开原生谷歌地图应用程序

Bri*_*ian 16 javascript android google-maps geolocation cordova

我正在使用Phonegap处理一个Android应用程序,但我无法弄清楚如何打开本地Google Maps应用程序来显示方向.我通过插入URL打开地图没有任何问题,但打开应用程序让我感到难过.到目前为止,我只能在iOS平台上找到与Phonegap相关的开发建议.

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

我想要完成的事情:

    1.单击链接后,打开本机Google Maps应用程序.如果设备上未安装该应用程序,请通知用户未安装Google地图并且必须已安装.
      一个.我已经在检查网络连接和处理它应该做的事情.
    2. Google地图会打开,显示从用户当前位置到所点击链接位置的路线.

以下示例适用于我的Android设备,与iOS上的相同,但显然无法打开Goog​​le Maps应用程序的操作.

<a> href="http://maps.google.com/maps?q=TD Washington DC"><img src="img/ico_pin.png" />White House</a></a>
Run Code Online (Sandbox Code Playgroud)

第二个例子通过包括结束位置来增加第一个例子,尽管我无法弄清楚如何插入当前位置.但最重要的是,它仍然无法打开Goog​​le地图应用程序.

<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;
}
Run Code Online (Sandbox Code Playgroud)

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

<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); 
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

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

在此先感谢您的帮助.

Sim*_*ald 56

使用geo:type uri.

<a href="geo:38.897096,-77.036545">open map</a>
Run Code Online (Sandbox Code Playgroud)

用户可以选择打开其设备上安装的任何映射应用程序.

  • Cordova 3.6.0引入了第二个白名单,用于限制允许哪些URL启动外部应用程序.[Cordova 3.6.0白名单指南](http://cordova.apache.org/docs/en/3.6.0/guide_appdev_whitelist_index.md.html)所以你需要在config.xml中明确添加:<access origin =" tel:*"launch-external ="yes"/> <access origin ="geo:*"launch-external ="yes"/> (14认同)
  • 地理URI方案地理位置:纬度,经度http://developer.android.com/guide/appendix/g-app-intents.html (4认同)

Car*_*ano 14

这项工作对我来说......

var addressLongLat = yourLatitude+','+yourLongitude
Run Code Online (Sandbox Code Playgroud)

对于Android Maps App:

window.open("geo:"+addressLongLat);
Run Code Online (Sandbox Code Playgroud)

对于iOs Maps App:

window.open("http://maps.apple.com/?q="+addressLongLat, '_system');
Run Code Online (Sandbox Code Playgroud)

对于App Browser中的Google地图:

window.open("http://maps.google.com/?q="+addressLongLat, '_system');
Run Code Online (Sandbox Code Playgroud)


小智 5

适用于所有版本的phonegap/cordova或网络浏览器.它将谷歌地图本机应用程序.

对于从当前位置导航到q - window.open("google.navigation:q = 23.3728831,85.3372199&mode = d",'_ system`);

对于搜索 - window.open("geo:0,0?q = pizza","_ system");

请阅读此处 - https://developers.google.com/maps/documentation/android/intents