http://www.glennstephens.com.au/2013/06/google-and-apple-maps-with-delphi-xe4/

If you follow the iPhone Maps, one you are probably lost, but two you know that Google Maps is a much better product. What if you want to use Google Maps, and if its not installed use Apple Maps. Luckily there is a way.

Much of the interprocess communication on the iOS can be done with URLs, you can register a custom URL scheme which is what Facebook does. Google Maps uses this ability as does Apple Maps so we can use it to your benefit.

In the Apple.Utils unit you will find in Samples\Delphi\RTL\CrossPlatform Utils there is a function called SharedApplication which returns the instance of UIApplication. UIApplication is an very handy tip, you can use the setApplicationIconBadgeNumber() method to set the number that appears on your App Icon, you can use the setNetworkActivityIndicatorVisible() method to set the network progress indicator at the top of the screen, it really is quite useful. The unit also has an OpenURL function which is very handy; think of it like the iOS equivilant of ShellExecute and you’ll be right.

So for checking if you have Google Maps you can use the canOpenURL method of UIApplication to determine if the URL can in fact be opened.The following will get the URLs for the various map systems for a lat/long for Google and Apple respectivly.

function TfrmMyApp.GetGoogleMapsUrl: string;
begin
    Result := Format(comgooglemaps://?center=%s,%s&zoom=14′,
    [LastLocation.Latitude.ToString, LastLocation.Longitude.ToString]
);
end;

function TfrmMyApp.GetAppleMapsUrl: string;
begin
    Result := Format(‘http://maps.apple.com/?ll=%s,%s’,
    [LastLocation.Latitude.ToString, LastLocation.Longitude.ToString]);
end;

and then to check try and open the Google First, and then Apple Maps if you really have to you would just need the following.

if SharedApplication.canOpenURL(StringToNSUrl(GetGoogleMapsUrl)) then
    OpenURL(GetGoogleMapsUrl)
else
    OpenURL(GetAppleMapsUrl);

This example is based on the lat/long only and there are plenty of ways to create the Maps URLs from things like getting directions, passing in an address. For a complete list for Google Maps and Apple Maps, check out:

Enjoy.

posted on 2014-05-12 16:33  行者无疆-九头鸟  阅读(305)  评论(0编辑  收藏  举报