onkey在iOS和Android上启动Navigation应用

onkey在iOS和Android上启动Navigation应用

本文介绍了如何使用RAD Studio FireMonkey在iOS和Android上启动Navigation应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以说,如果不涉及Java,就无法激发意图。仍然可以通过C ++将URL发送到iOS以打开导航吗?我对iOS的开发没有太多经验。

UPDATE: Further research reveals that this may be impossible for the time being, even if I wrote some platform specific code. This answer says that intents cannot be fired without Java being involved. Is there still a way the URL can be sent to iOS with C++ to open Navigation? I don't have much experience with iOS development.

推荐答案

因此,事实证明URL是两种操作系统的答案。通过一些帮助程序功能将URL发送到操作系统,可以实现打开导航的功能。我在这里的博客上找到了此源代码。它是用Delphi编写的,但事实证明您可以将Delphi文件添加到项目中,在C ++中编译#include filename.hpp并使用在Delphi中创建的功能。

So it turns out that a URL is the answer for both operating systems. I was able to achieve opening the Navigation by sending a URL to the OS through some helper functions. I found this source code on a blog here. It was written in Delp but it turns out that you can add the Delphi file to your project, compile #include "filename.hpp" in your C++ and use the functions you created in Delphi.

unit OpenViewUrl;

interface

uses System.Sensors;

// URLEncode is performed on the URL
// so you need to format it   protocol://path
function OpenURL(const URL: string; const DisplayError: Boolean = False): Boolean;
function OpenNavigation(const Q: string): Boolean; overload;
function OpenNavigation(const Q: string; const Coord: TLocationCoord2D): Boolean; overload;

implementation

uses
  IdURI, SysUtils, Classes, FMX.Dialogs
{$IFDEF ANDROID}
  , FMX.Helpers.Android, Androidapi.JNI.GraphicsContentViewText,
  Androidapi.JNI.Net, Androidapi.JNI.JavaTypes, Androidapi.Helpers;
{$ELSE}
{$IFDEF IOS}
  , iOSapi.Foundation, FMX.Helpers.iOS, Macapi.Helpers;
{$ELSE};
{$ENDIF IOS}
{$ENDIF ANDROID}


function OpenURL(const URL: string; const DisplayError: Boolean = False): Boolean;
{$IFDEF ANDROID}
var
  Intent: JIntent;
begin
// There may be an issue with the geo: prefix and URLEncode.
// will need to research
  Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW,
    //TJnet_Uri.JavaClass.parse(StringToJString(TIdURI.URLEncode(URL))));
    TJnet_Uri.JavaClass.parse(StringToJString(URL)));
  try
    SharedActivity.startActivity(Intent);
    exit(true);
  except
    on e: Exception do
    begin
      if DisplayError then ShowMessage('Error: ' + e.Message);
      exit(false);
    end;
  end;
end;
{$ELSE}
{$IFDEF IOS}
var
  NSU: NSUrl;
begin
  // iOS doesn't like spaces, so URL encode is important.
  // NSU := StrToNSUrl(TIdURI.URLEncode(URL));
  NSU := StrToNSUrl(URL);
  if SharedApplication.canOpenURL(NSU) then
    exit(SharedApplication.openUrl(NSU))
  else
  begin
    if DisplayError then
      ShowMessage('Error: Opening "' + URL + '" not supported.');
    exit(false);
  end;
end;
{$ELSE}
begin
  raise Exception.Create('Not supported!');
end;
{$ENDIF IOS}
{$ENDIF ANDROID}



function OpenNavigation(const Q: string): Boolean;
var Coord: TLocationCoord2D;
begin
  Coord.Latitude := 0.0;
  Coord.Longitude := 0.0;
  OpenNavigation(Q, Coord);
end;


function OpenNavigation(const Q: string; const Coord: TLocationCoord2D): Boolean;
var
  CoordString: String;
begin
  //Open in Google Maps
  {$IFDEF ANDROID}
  exit(OpenURL('http://maps.google.com/?q=' + Q));
  {$ELSE}

  //In iOS, if Google Maps is installed, use it, otherwise, use Apple Maps
  //If we have coordinates, use them as the start address
  {$IFDEF IOS}
  //Get a string of the longitute and latitute seperated by a comma if set
  if (Coord.Latitude <> 0.0) or (Coord.Longitude <> 0.0) then
  begin
    CoordString := Coord.Latitude.ToString + ',' + Coord.Longitude.ToString;
  end
  else begin
    CoordString := '';
  end;
  if not OpenURL('comgooglemaps://?daddr=' + Q) then
  begin
    if (0.0 < CoordString.Length) then
    begin
      exit(OpenURL('http://maps.apple.com/?daddr=' + Q + '&saddr=loc:' + CoordString));
    end
    else begin
      exit(OpenURL('http://maps.apple.com/?daddr=' + Q));
    end;
  end
  else begin
    exit(true);
  end;
  {$ELSE}
  //Unsupported platform
  exit(false);
  {$ENDIF IOS}
  {$ENDIF ANDROID}
end;


end.

最后一个功能是我添加的。如果使用Apple Maps,它将根据操作系统打开maps应用程序,并可选地使用一组坐标进行导航,因为它不像Google Maps那样处理它。如果有人可以,我还无法在iOS上测试Google Maps。

The last function is one that I added. It will open the maps app according to the OS and optionally takes a set of coordinates to navigate from if using Apple Maps, because it does not handle it as well as Google Maps. I have not been able to test Google Maps on iOS if anyone would be able to, let me know.

这篇关于如何使用RAD Studio FireMonkey在iOS和Android上启动Navigation应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 09:56