Google and Apple Maps with Delphi XE4

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

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.ios

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.git

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.app

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.ui

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

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

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.url

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

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:orm

Enjoy.

相關文章
相關標籤/搜索