start navigation with a button

marcick

Well-Known Member
Licensed User
Longtime User
Hi guys,
I would like to implement in my application a function that clicking a button starts the MAPS application or directly the NAVIGATOR application of the device, passing a couple of coordinates.
Is it possible ?
Marco
 

warwound

Expert
Licensed User
Longtime User
Yes you can do that, take a look here: http://www.b4x.com/forum/basic4android-updates-questions/7280-starting-geo-uri-intent.html#post41618

That shows how to open the built in Android Map application at a geographical point.

You can also open the built in StreetView application like this:

B4X:
Dim Intent1 As Intent
Dim Uri As String
Uri="google.streetview:cbll="&ALatitude&","&ALongitude
Intent1.Initialize(Intent1.ACTION_VIEW, Uri)
StartActivity(Intent1)

I don't have example code to hand but you can also start StreetView and specify exact point of view - direction, zoom level and yaw.

There's an Intent you can create to start the Map application with directions - again i don't have example code to hand.

Built in means if the application is installed - if it's not installed you'll probably get an exception.

Martin.
 
Upvote 0

rboeck

Well-Known Member
Licensed User
Longtime User
Hi,

i have tried to call streetview from an working google map with markers for customers. Now i have tried to add an streetview button.
In germany and austria streetview is only partially available. So if streetview is not available, my button lead to an black screen...
It would be better, not to show the button, that does not work.

I have found following code to check, if streetview is available at this location:

B4X:
var map;
var berkeley = new google.maps.LatLng(37.869085,-122.254775);
var sv = new google.maps.StreetViewService();

var panorama;
function initialize() {
  panorama = new google.maps.StreetViewPanorama(document.getElementById("pano"));
  // Set up the map
  var mapOptions = {
    center: berkeley,
    zoom: 16,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    streetViewControl: false
  };
  map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions);
  // getPanoramaByLocation will return the nearest pano when the
  // given radius is 50 meters or less.
  google.maps.event.addListener(map, 'click', function(event) {
      sv.getPanoramaByLocation(event.latLng, 50, processSVData);
  });
}
function processSVData(data, status) {
  if (status == google.maps.StreetViewStatus.OK) {
    var marker = new google.maps.Marker({
      position: data.location.latLng,
      map: map,
      title: data.location.description
    });
    google.maps.event.addListener(marker, 'click', function() {
      var markerPanoID = data.location.pano;
      // Set the Pano to use the passed panoID
      panorama.setPano(markerPanoID);
      panorama.setPov({
        heading: 270,
        pitch: 0
      });
      panorama.setVisible(true);
    });
  }
}

Is this info transferable to B4A or has it to be integrated in an library?
Thanks in advance!
Reinhard
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Is your working google map with markers an android GoogleMap or a WebView displaying a map using the javascript Google Maps API?

If it's a WebView using the Google Maps API then you can update the javascript to call the Google Maps API getPanoramaByLocation method and establish if StreetView exists for a location.

But if your map is an android Google Map then you can't directly make use of the javascript Google Maps API.
My only suggestion would be to use a hidden WebView to run the javascript StreetView API: https://developers.google.com/maps/documentation/javascript/streetview.
Scroll down that page to Directly Accessing Street View Data and you'll see that the getPanoramaByLocation method is supported.
So create a hidden WebView and a webpage that contains javascript to query the StreetView API.
Load the webpage - when you want to know if StreetView exists for a location you'd pass the location coordinates to the hidden WebView which would use javascript to check for StreetView and then call a Sub in your b4a Activity to let your b4a code know the result of the getPanoramaByLocation lookup.

Not impossible but not straightforward.

Martin.
 
Upvote 0

rboeck

Well-Known Member
Licensed User
Longtime User
Hi Martin,

i use the google Maps API; i think, that your idea with the hidden webview is the right direction. In the last days i looked at the Google directions API with many new possibilities (calculate a route to more points with possibility to optimize it) and for using this i would also need this hidden webview?!
Have you made any test with the navigation possibilities?

Thanks for your time,
Reinhard
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Success!

A simple web page:

PHP:
<!DOCTYPE html>
<html>
   <head>
     <title></title>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <script src="https://maps.googleapis.com/maps/api/js?sensor=true"></script>
     <script>
       function checkStreetView(latitude, longitude, meters){
         streetViewService.getPanoramaByLocation(new google.maps.LatLng(latitude, longitude), meters, function(data, status){
           var streetViewAvailable;
           if (status==google.maps.StreetViewStatus.OK) {
             streetViewAvailable=true;
           } else {
             streetViewAvailable=false;
           }
           B4A.CallSub('StreetViewChecked', true, streetViewAvailable.toString());
         });
       }
       function initialize(){
         streetViewService = new google.maps.StreetViewService();
         B4A.CallSub('StreetViewCheckerInitialized', true);
       }
       var streetViewService;
     </script>
   </head>
   <body onload="initialize()">
   </body>
</html>

And a simple b4a project:

B4X:
Sub Process_Globals

End Sub

Sub Globals
   Dim StreetViewCheckerIsInitialized As Boolean=False
   Dim WebViewExtras1 As WebViewExtras
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Dim WebView1 As WebView
   WebView1.Initialize("WebView1")
   
   WebViewExtras1.Initialize(WebView1)
   
   '   seems to work with no need to add the WebView to the Activity
   '   Activity.AddView(WebViewExtras1, 0, 0, 100%x, 100%y)
   
   Dim JavascriptInterface1 As DefaultJavascriptInterface
   JavascriptInterface1.Initialize
   WebViewExtras1.AddJavascriptInterface(JavascriptInterface1, "B4A")
   
   WebViewExtras1.LoadUrl("file:///android_asset/streetview_checker.html")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub StreetViewCheck(Latitude As Double, Longitude As Double, Meters As Int)
   If StreetViewCheckerIsInitialized Then
     Dim Javascript As StringBuilder
     Javascript.Initialize
     Javascript.Append("javascript:checkStreetView(")
     Javascript.Append(Latitude)
     Javascript.Append(",")
     Javascript.Append(Longitude)
     Javascript.Append(",")
     Javascript.Append(Meters)
     Javascript.Append(");")
     
     Log("StreetViewCheck: "&Javascript.ToString)
     
     WebViewExtras1.ExecuteJavascript(Javascript.ToString)
     
     '   now waiting for the StreetViewChecked Sub to be called by the webpage javascript
     
   Else
     Log("The StreetViewChecker is not yet initialized")
   End If
End Sub

Sub StreetViewChecked(Available As String)
   '   this Sub is called by the webpage javascript once it has received the result of it's getPanoramaByLocation request
   '   Available will be a String 'true' or 'false'
   '   (the webpage javascript can only pass String values to B4A, it cannot pass a Boolean)
   Log("StreetViewChecked result is "&Available)
End Sub

Sub StreetViewCheckerInitialized
   '   this Sub is called by the webpage javascript once it has initialized itself
   '   if an error occurs this Sub will not get called so some sort of error trapping ought to be implemented
   Log("StreetViewCheckerInitialized")
   StreetViewCheckerIsInitialized=True
   '   StreetViewCheck(52.77132, 0.38332, 10)   '   i know there's no StreetView available here
   StreetViewCheck(52.75424, 0.40267, 50)   '   i know there is StreetView available here
End Sub

Testing with some coordinates of local places where i know there is or isn't StreetView it works perfectly.

I've used the new version of my WebViewExtras library here and you can download that from: http://android.martinpearman.co.uk/b4a/temp/WebViewExtras2_20130728.zip.
(The code could be modified to work with the older version of WebViewExtras that is available on the forum).

You have various options if you need to get directions between places.

There is the HTTP Google Directions API: https://developers.google.com/maps/documentation/directions/.
No javascript used with the HTTP API so no need for a WebView - just use the B4A HTTP library.

The javascript Google Maps API has a more feature packed directions service and as you said would require a WebView.

There is the open source OSRM project which i'm sure provides a HTTP service similar to the HTTP Google Directions API.
The advantage over Google's service is that you can use the OSRM directions in any way you want, whereas the directions obtained from the Google Directions API must only be used to display a route on a Google map.

And the open source MapQuest Directions API is another HTTP option where you can do as you wish with the directions.

Martin.
 

Attachments

  • StreetViewChecker.zip
    7 KB · Views: 308
Upvote 0

rbsoft

Active Member
Licensed User
Longtime User
Upvote 0

rboeck

Well-Known Member
Licensed User
Longtime User
Hi Martin,

i am realy impressed, how fast you could realize this idea! I needed only few minutes till it worked. Only one parameter is not clear for me: How can i know the meters parameter in StreetViewCheck? I have tried an possible value and it worked; i can imagine, that this parameter in useable, when bridges make two different levels of streets?
To try and test your ideas with the navigation i need more time...

@rbsoft: I am looking for an intent to start google earth with an defined position and zoom level, did you ever find something?

Greetings
Reinhard
 
Upvote 0

rboeck

Well-Known Member
Licensed User
Longtime User
Hi Martin,

today i tried the new googlemapsextras library and played around... I have already working the above routines with streetview checking. But now i overruled my and your routines an enabled streetview inside a museum - an it worked!!
Try this coordinates: Lat: 48.203718 Long: 16.361456 in google maps, now switch to streetview with the same coordinates.
The picture comes from google earth, but its itentical to that, what you will see.
Can you have a look at your routines at post#7 if you can find a solution for streetviewcheck inside buildings.
Greetings
Reinhard

P.S. I have searched for other places, where streetview works; i think, it was the only one in austria. Maybe in the future we will see more...
 

Attachments

  • KMH Vienna.jpg
    KMH Vienna.jpg
    37.9 KB · Views: 239
Last edited:
Upvote 0

warwound

Expert
Licensed User
Longtime User
Hi there.

I'm not sure exactly what you're asking...

I assumed the javascript streetview checker was returning false - there is no streetview for (48.203718, 16.361456) - and you wanted to know if it could be updated to correctly return true to indicate that there is streetview.

But i just recompiled the project from post #7 and the streetview check returns true - there is streetview for (48.203718, 16.361456).

B4X:
StreetViewCheck(48.203718,16.361456, 10)

Have i misunderstood your post or are you getting a different result from the streetview checker?

Martin.
 
Upvote 0

rboeck

Well-Known Member
Licensed User
Longtime User
Hi Martin,

it's true, my app had other coordinates and so i made wrong assumptions.
Did you try streetview inside the museum?
 
Upvote 0

rboeck

Well-Known Member
Licensed User
Longtime User
After a long time i renewed old code and found out that is routine is not working on todays systems - the problem is, that there is no initialation - in the log file you see the message:
'The StreetViewChecker is not yet initialized'
Is this problem from new webengines, new javascript or other webviewextras2 routines?

Thanks,
Reinhard
 
Upvote 0
Top