Does anyone have a quick way to load lat and lon from Google maps into a DB?

netchicken

Active Member
Licensed User
Longtime User
Here is a good way to show your location using the Google map, but is there an opposite way, so that you click on a location and pass it to storedlat and storedlon?

Otherwise people have to type it in by hand.

B4X:
Dim Intent1 As Intent
   URI= "geo:" & storedlat & "," & storedlon & "?q=" & storedlat & "," & storedlon

'streetview works as well
'   URI= "google.streetview:cbll=" & storedlat & "," & storedlon & "&cbp=1"

'google.streetview:cbll=lat,lng&cbp=1,yaw,,pitch,zoom&mz=mapZoom
   Intent1.Initialize(Intent1.ACTION_VIEW,URI)
   Intent1.SetComponent("googlemaps")
   StartActivity(Intent1)
 

warwound

Expert
Licensed User
Longtime User
No need for additional libraries...

Take a look at this webpage, orginally the Google Maps API example from Google Maps Javascript API V3 Tutorial - Google Maps JavaScript API V3 - Google Code.

B4X:
<!DOCTYPE html>
<html>
<head>
<title>Google Maps API LocationMap</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<meta charset="UTF-8">
<style type="text/css">
html{
   height:100%;
}
body{
   height:100%;
   margin:0;
   padding:0;
}
#map_canvas{
   height:100%;
}
</style>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
function initialize(){
   function getQueryObject(){
      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 mapOptions={}, query=getQueryObject();
   query.lat=query.lat || 0;
   query.lng=query.lng || 0;
   query.zoom=query.zoom || 0;
   mapOptions.center=new google.maps.LatLng(parseFloat(query.lat), parseFloat(query.lng));
   mapOptions.mapTypeId=google.maps.MapTypeId.ROADMAP;
   mapOptions.zoom=parseInt(query.zoom);
   
   map=new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
}
var map;
google.maps.event.addDomListenerOnce(window, 'load', initialize);
</script>
</head>
<body>
<div id="map_canvas"></div>
</body>
</html>

I've added the getQueryObject() function which can get any query values passed to the webpage in it's URL.
The code checks the query values - if any of lat, lng or zoom are not present in the URL then a value of 0 is used.

Here's a B4A project that uses the map:

B4X:
'   Google Maps API LocationMap
Sub Process_Globals
End Sub

Sub Globals
   Dim WebView1 As WebView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   WebView1.Initialize("")
   Activity.AddView(WebView1, 0, 0, -1, -1)
   
   Dim Lat As Float
   Dim Lng As Float
   Dim Zoom As Int
   
   Lat=52.746008
   Lng=0.400069
   Zoom=12
   
   WebView1.LoadUrl("file:///android_asset/map-simple.html?lat="&Lat&"&lng="&Lng&"&zoom="&Zoom)
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sample code attached.

Martin.
 

Attachments

  • LocationMap.zip
    6.3 KB · Views: 231
Upvote 0

gehrlekrona

Member
Licensed User
Longtime User
Not getting the Lat & Long

I don't see where you get the click event so you can detect the Lat and Long from the map. I donloaded the sample code and I guess I only see that it sets the location to whatever is in the code. Nothing else.
How do you click the map to get the Lat and Long coordinates?
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Hi.

Take a look at the attached code.

It's much the same as i originally posted with these updates.

Added my WebViewExtras library and used it to add a javascript interface to the WebView.
The WebView can now call B4A Subs, so i created a Sub that the WebView can send a String to:

B4X:
'   Google Maps API LocationMap
Sub Process_Globals
End Sub

Sub Globals
   Dim WebView1 As WebView
   Dim WebViewExtras1 As WebViewExtras
End Sub

Sub Activity_Create(FirstTime As Boolean)
   WebView1.Initialize("")
   Activity.AddView(WebView1, 0, 0, -1, -1)
   
   '   add a javascript interface to the WebView
   WebViewExtras1.addJavascriptInterface(WebView1, "B4A")
   
   Dim Lat As Float
   Dim Lng As Float
   Dim Zoom As Int
   
   Lat=52.746008
   Lng=0.400069
   Zoom=12
   
   WebView1.LoadUrl("file:///android_asset/map_simple.html?lat="&Lat&"&lng="&Lng&"&zoom="&Zoom)
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub HandleMapClick(ClickedLocation As String)
   Log("You clicked on location: "&ClickedLocation)
End Sub

Added an event listener to the map to detect a map click.
When the map is clicked, the clicked location is sent to the B4A Sub 'HandleMapClick':

B4X:
<script type="text/javascript">
function initialize(){
   function getQueryObject(){
      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 mapOptions={}, query=getQueryObject();
   query.lat=query.lat || 0;
   query.lng=query.lng || 0;
   query.zoom=query.zoom || 0;
   mapOptions.center=new google.maps.LatLng(parseFloat(query.lat), parseFloat(query.lng));
   mapOptions.mapTypeId=google.maps.MapTypeId.ROADMAP;
   mapOptions.zoom=parseInt(query.zoom);
   
   map=new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
   
   google.maps.event.addListener(map, 'click', function(myMouseEvent){
      //   this event listener is executed when the map is clicked
      //   it is passed a MouseEvent object: http://code.google.com/apis/maps/documentation/javascript/reference.html#MouseEvent
      
      //   convert the MouseEvent LatLng property into a string: http://code.google.com/apis/maps/documentation/javascript/reference.html#LatLng
      var clickedLocation=myMouseEvent.latLng.toUrlValue();
      
      //   send the clickedLocation string to the B4A Sub HandleMapClick
      B4A.CallSub('HandleMapClick', true, clickedLocation);
   });
}
var map;
google.maps.event.addDomListenerOnce(window, 'load', initialize);
</script>

Run the code and look in the Log and you'll see the clicked location displayed.

Martin.
 

Attachments

  • MapClick.zip
    6.6 KB · Views: 223
Upvote 0

metrick

Active Member
Licensed User
Longtime User
Martin:
Is there a way to add movable markers to the map with location of Lat and Lon information?
 
Upvote 0

smasher230

Member
Licensed User
Longtime User
How to start maps in satelite view

Hi,

I apologise if this is not the place to ask this question. I have found this solution very useful.

However, is there a way to start the webview in Satelite view rather than the normal map?

:sign0104:

Hi.

Take a look at this thread: http://www.b4x.com/forum/basic4android-updates-questions/20538-google-maps.html#post118313

If you follow it through you'll find code that works to move a single marker on a map, the concept is the same if you want to move more than one marker.

Martin.
 
Upvote 0

metrick

Active Member
Licensed User
Longtime User
Martin:
All I get from post#5 download is white screen.
My gps is on and working with other apps.
Do I missing something?
Thanks
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Hi.

I just downloaded the same demo code and ran it on my ZTE Blade - CyanogenMod7 (Gingerbread) - and it works fine, the map displays sunny Norfolk no problems.

You are refrring to Post #5 are you?
GPS is not required - it's just a WebView displaying a Google Maps API map...

Martin.
 
Upvote 0

metrick

Active Member
Licensed User
Longtime User
Martin:
Is there a way to pass array to mapclick(google map api) from B4A?
I have multiple locations that I would like to mark on the map with custom icon.
Nothing come up with search on the forum.
Thanks in advance.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Martin:
Is there a way to pass array to mapclick(google map api) from B4A?
I have multiple locations that I would like to mark on the map with custom icon.
Nothing come up with search on the forum.
Thanks in advance.

Not directly - the best solution would be to turn the Array into a JSON String and pass that String to your WebView map.
http://www.b4x.com/forum/basic4andr...als/6923-android-json-tutorial.html#post39966

Then you can use the WebView's built in JSON support to convert the String back into an Array.
Example javascript:

B4X:
var myString='[[25, 36], [50, 12], [9, 41]]';
var myArray=JSON.parse(myString);
// myArray should now be an an Array containing 3 2d Arrays

Martin.
 
Upvote 0
Top