GPS Location and Google map

jnbarban

Member
Licensed User
Longtime User
Hello, I want to show on "WebView" a GoogleMap map localized on my GPS position. do you know how i can do that ?

I have try to use the GPS Exemple to get my location but i get no position ...i just see satellite change like :

2 27.6000045856 false 217 12
18 12.45687456552 false 140 32
....

I want also see on my map some "Flag" (exemple : like "Market center" in order to show all market near me.)
In my case, i know all positions of my flags, i want to put this on the map.

Can you help me ?
 

warwound

Expert
Licensed User
Longtime User
Hi.

Here's some sample code for you.
(Mostly borrowed from the forum tutorials).

I've broken the solution down into two activities.

Main activity:

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim gpsClient As GPS
   Dim userLocation As Location
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim getUserLocationButton As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      gpsClient.Initialize("gpsClient")
      userLocation.Initialize
   End If
End Sub

Sub Activity_Resume
   Activity.LoadLayout("lytMain")
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub getUserLocationButton_Click
   If gpsClient.GPSEnabled=False Then
      ToastMessageShow("Please enable your device's GPS capabilities", True)
        StartActivity(gpsClient.LocationSettingsIntent)
   Else
      gpsClient.Start(0, 0)
      ProgressDialogShow("Waiting for GPS location")
   End If
End Sub

Sub gpsClient_LocationChanged (gpsLocation As Location)
   ProgressDialogHide
   userLocation=gpsLocation
   gpsClient.Stop
   StartActivity("actDisplayMap")
End Sub

actDisplayMap activity:

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim mapWebView As WebView
End Sub

Sub Activity_Create(FirstTime As Boolean)

End Sub

Sub Activity_Resume
   Activity.LoadLayout("lytDisplayMap")
   mapWebView.Width=100%x
   mapWebView.Height=100%y
   ProgressDialogShow("Creating the map")
   mapWebView.LoadUrl("file:///android_asset/location_map.htm?lat="&Main.userLocation.Latitude&"&lng="&Main.userLocation.Longitude&"&zoom=8")
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub mapWebView_PageFinished (Url As String)
   ProgressDialogHide
End Sub

And the map/webpage code:

B4X:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html{
   height:100%;
}
body{
   height:100%;
   margin:0px;
   padding:0px;
}
#locationMap{
   height:100%;
}
</style>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript">
function initialize(){
   function getQueryObject(){
      //   getQueryObject() returns an object literal of name/values from the map page's URL if any are present
      var query=location.search, args={};
      query=query.length>0?query.substring(1):'';
      var items=query.split('&'), item, name, value, i;
      for(i=0; i<items.length; i++){
         item=items[i].split('=');
         name=decodeURIComponent(item[0]);
         value=decodeURIComponent(item[1]);
         args[name]=value;
      }
      return args;
   }
   var map, mapOptions={}, query=getQueryObject(), marker, markerOptions={}, lat=0, lng=0, zoom=0, createMarker=false;
   if(query.lat && query.lng){
      lat=parseFloat(query.lat);
      lng=parseFloat(query.lng);
      createMarker=true;
   }
   if(query.zoom){
      zoom=parseInt(query.zoom);
   }
   mapOptions.zoom=zoom;
   mapOptions.center=new google.maps.LatLng(lat, lng);
   mapOptions.mapTypeId=google.maps.MapTypeId.ROADMAP;
   map=new google.maps.Map(document.getElementById('locationMap'), mapOptions);
   if(createMarker){
      markerOptions.map=map
      markerOptions.position=new google.maps.LatLng(lat, lng);
      marker=new google.maps.Marker(markerOptions)
   }
}
</script>
</head>
<body onLoad="initialize()">
   <div id="locationMap"></div>
</body>
</html>

The code should be self-explanatory (that's my excuse for no comments lol).

Complete source code attached.

Martin.
 

Attachments

  • LocationMap.zip
    7.8 KB · Views: 2,507
Upvote 0

ciginfo

Well-Known Member
Licensed User
Longtime User
Bonjour,

Same thing for me I cannot get my position, I can see only satellites.

If I use warwound's source code attached i see only "Waiting for GPS location"

Merci de m'aider
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Updating Location on map constantly

Hello everyone, i was wondering if anyone can help me out by giving me some hints on how to constantly update my location on a map at let's say 30 seconds intervals without reloading the map, just updating the actual location, is this possible if so, can anyone please give me some hints?

thanks,
Walter
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Hello everyone, i was wondering if anyone can help me out by giving me some hints on how to constantly update my location on a map at let's say 30 seconds intervals without reloading the map, just updating the actual location, is this possible if so, can anyone please give me some hints?

thanks,
Walter

I shall have my new MapView library available within a few days i hope.

One of the Overlay layers available in the new library is called MyLocationOverlay.

It's a wrapper for the OSMDroid MyLocationOverlay.

Take a look at that link and the methods available - you can enable followLocation and use setLocationUpdateMinTime and you MapView will automatically update, center on and display your location.

I'm adding the documentation to the library at the moment and writing some tutorials.
After that and some final tests i'll have it uploaded for everyone to try.

PM me if you want to get an early copy to beta test.

Martin.
 
Upvote 0

ttsolution

Member
Licensed User
Longtime User
How to calculate distance (in meters)

Hello everyone. I'm new to B4A, and I'm seeking for help to calculate distance (in meters) between 2 pairs of Latitude/Longitude.

P1 :
- Latitude
- Longitude
P2 :
- Latitude
- Longitude

How to calculate distance from P1 to P2 (in meters) ?

Many thanks for your help

Jonh,
 
Upvote 0

goron

Member
Licensed User
Longtime User
I'm using:

meter = Floor(CurrentLocation.DistanceTo(TargetLocation)) 'in meters
DisTxt = " meter"
If meter > 1000 Then
DisTxt = " KM"
meter = Round2 (meter / 1000, 2)
End If

HTH, Gideon
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Upvote 0

klaus

Expert
Licensed User
Longtime User
The routine below does it:
B4X:
Sub CalcDistance(lat1 As Double, lng1 As Double, lat2 As Double, lng2 As Double) As Double
    Dim l1, l2 As Location
    
    l1.Initialize2(lat1, lng1)
    l2.Initialize2(lat2, lng2)
    
    Return l1.DistanceTo(l2)
End Sub
Needs the GPS library and the lat lng values in deciamal degrees.

Best regards.
 
Upvote 0

Hubert Brandel

Active Member
Licensed User
Longtime User
sounds interessting, my problem is, that my Samsung Galaxy S3+ don't find satelits ... :eek:
After minutes waiting, I do use google maps without GPS, only with the cell phone position (witch is not very accurate).

Is this normal ?
 
Upvote 0
Top