Showing posts with label location. Show all posts
Showing posts with label location. Show all posts

Feb 13, 2014

My Location&My Path app(do it android mission 20)


This app is display my current moving path on map.

If you run the app the app displays 3 button and 1 input text.

If you touched 'rec' button then app getting your location using GPS or Network every 3 second.

And app save in database that displays your moving path on map using polyline.

If you touched 'stop' button then app delete polyline and database.


This picture is normal state(you run the app)

This picture is touched 'rec'button

This picture is touched 'stop' button


1. Using Database


Adding MainActivity.java code for using database

MainActivity.java

  1. mapview.getSettings().setDatabasePath("/data/data/"+this.getPackageName()+"/databases/");
  2. mapview.getSettings().setDomStorageEnabled(true);
  3. mapview.getSettings().setDatabaseEnabled(true);
  4. mapview.getSettings().setAppCacheEnabled(true);
It code need for using database.

and need a javascrpit code

index.html

  1. function setdatabase(){
  2.     getMylocation();
  3.     var sumlatlnt=curLat+","+curLng;
  4.     localStorage.setItem(keycnt,sumlatlnt);
  5.     keycnt++;
  6. }
important is "localStorage.setItem(key,value)"

This code is record to database.

and if you reading value in database

You need a this code

"var temp=localStorage.getItem(key)"

and remove code is
"localStorage.removeItem(key)"


2. overlay using polyline

It's simple drawing polyline using google maps API

you just add this code to initialize
  1. var polyOptions = {
  2.     strokeColor: '#000000',
  3.     strokeOpacity: 1.0,
  4.     strokeWeight: 3
  5.   };
  6.   poly = new google.maps.Polyline(polyOptions);
  7.   poly.setMap(map);
  8.   path = poly.getPath();
you possible to reference google API about polyOption.

and adding event function "path.push(event.latLng);"

But I used another method.

  1. if(navigator.geolocation) {
  2.      navigator.geolocation.getCurrentPosition(function(position) {  
  3.          curLat=position.coords.latitude;
  4.          curLng=position.coords.longitude;
  5.          var newpos = new google.maps.LatLng(curLat,curLng);
  6.          map.setCenter(newpos);
  7.          path.push(newpos);
  8.      }function() {
  9.       handleNoGeolocation(true);
  10.     });
  11.    
  12.      
  13.   } else {
  14.    
  15.     handleNoGeolocation(false);
  16.   }
because I want my current location using gps

"path.push(latLng)" is draw polyline on map.

And "path.clear()" is clear polyline on map.







Feb 12, 2014

Get my location using geocoding& reverse geocoding



This app is get mylocation and my address using reverse geocoding.

You will typing for want place. This app find that place using geocoding.

And you see icon(starbucks and kfc) using overlay.





 If you running the app that will see message(GPS coordinate)

And you touched check button that will see your location address
using reverse geocoding


If you typing place that will see that place map&marker.





This is index.html file

  1. <!DOCTYPE html>
  2. <html>
  3.   <head>
  4.     <title>Geolocation</title>
  5.     <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
  6.     <meta charset="utf-8">
  7.     <style>
  8.       html, body, #map-canvas {
  9.         height: 100%;
  10.         margin: 0px;
  11.         padding: 0px
  12.       }    
  13.        #panel {
  14.         position: absolute;
  15.         top: 5px;
  16.         left: 50%;
  17.         margin-left: -180px;
  18.         z-index: 5;
  19.         background-color: #fff;
  20.         padding: 5px;
  21.         border: 1px solid #999;
  22.       }
  23.     </style>
  24.    
  25.     <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true"></script>
  26.     <script>
  27. var map;
  28. var pos;
  29. var marker;
  30. var starbuckslatlng;
  31. var kfclatlng;
  32. var geocoder;
  33. function initialize() {
  34.   var mapOptions = {
  35.     zoom: 15
  36.   };
  37.   var curLat;
  38.   var curLng;
  39.   geocoder = new google.maps.Geocoder();
  40.   map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);
  41.   if(navigator.geolocation) {
  42.      navigator.geolocation.getCurrentPosition(function(position) {  
  43.          curLat=position.coords.latitude;
  44.          curLng=position.coords.longitude;
  45.          
  46.          alert("My location is\nlatitude:"+curLat+"\nlongitude:"+curLng);  
  47.          pos = new google.maps.LatLng(curLat,curLng);
  48.          marker = new google.maps.Marker({
  49.              map: map,
  50.             //animation: google.maps.Animation.BOUNCE,
  51.             position: pos
  52.         });
  53.         map.setCenter(pos);
  54.         reverseGeocode(curLat,curLng);  
  55.      }function() {
  56.       handleNoGeolocation(true);
  57.     });
  58.    
  59.      
  60.   } else {
  61.    
  62.     handleNoGeolocation(false);
  63.   }
  64.   addItem();
  65. }
  66. function reverseGeocode(relat,relng){   //for reverse Geocoding
  67.    
  68.     var regeocoder = new google.maps.Geocoder();
  69.     var relatlng=new google.maps.LatLng(relat,relng);
  70.     var info = new google.maps.InfoWindow({
  71.         map: map,
  72.         position: relatlng
  73.       });
  74.    
  75.    
  76.     regeocoder.geocode({ 'latLng': relatlng}function(results, status){
  77.         if(status == google.maps.GeocoderStatus.OK){
  78.              
  79.             if(results[1]){
  80.                 info.setContent(results[1].formatted_address);
  81.                 info.open(map,marker);
  82.             }else{
  83.                 alert("Geocoder failed due to:"+status);
  84.             }
  85.         }
  86.     });
  87.    
  88. }
  89. function addItem(){    //for add Item(overlay)
  90.    
  91.     starbuckslatlng=new google.maps.LatLng(35.218,128.6);
  92.     kfclatlng=new google.maps.LatLng(35.215,128.6);
  93.     starbucksMap=new google.maps.Marker({
  94.         position:starbuckslatlng,
  95.         icon:'starbucks.jpg',
  96.         map:map
  97.     });
  98.    
  99.     kfcMap=new google.maps.Marker({
  100.         position:kfclatlng,
  101.         icon:'kfc.jpg',
  102.         map:map
  103.     });
  104.    
  105.    
  106.     google.maps.event.addListener(starbucksMap,'click',function(){
  107.         alert("여기는 스타벅스입니다");
  108.     });
  109.     google.maps.event.addListener(kfcMap,'click',function(){
  110.         alert("여기는 kfc입니다");
  111.     });
  112. }
  113. function handleNoGeolocation(errorFlag) {  
  114.   if (errorFlag) {
  115.     var content = 'Error: The Geolocation service failed.';
  116.   } else {
  117.     var content = 'Error: Your browser doesn\'t support geolocation.';
  118.   }
  119.   var options = {
  120.     map: map,
  121.     position: new google.maps.LatLng(35125),
  122.     content: content
  123.   };
  124.   var infowindow = new google.maps.InfoWindow(options);
  125.   map.setCenter(options.position);
  126. }
  127. function codeAddress() {    //for  Geocoding
  128.    
  129.   var address = document.getElementById('address').value;
  130.   geocoder.geocode( { 'address': address}function(results, status) {
  131.     if (status == google.maps.GeocoderStatus.OK) {
  132.       map.setCenter(results[0].geometry.location);
  133.       var marker2 = new google.maps.Marker({
  134.           map: map,
  135.           position: results[0].geometry.location
  136.       });
  137.       var infowindow=new google.maps.InfoWindow({
  138.           map:map,
  139.           position: results[0].geometry.location,
  140.           content:address
  141.       });
  142.       infowindow.open(map,marker2);
  143.     } else {
  144.       alert('Geocode was not successful for the following reason: ' + status);
  145.     }
  146.   });
  147. }
  148. google.maps.event.addDomListener(window, 'load', initialize);
  149.     </script>
  150.   </head>
  151.   <body>
  152.       <div id="panel">
  153.       <input id="address" type="textbox" value="창원시청">
  154.       <input type="button" value="Go!!" onclick="codeAddress()">
  155.     </div>
  156.     <div id="map-canvas"></div>
  157.   </body>
  158. </html>


This is mainactivity.java



  1. package com.answerofgod.mymapgeocode;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.webkit.GeolocationPermissions;
  5. import android.webkit.GeolocationPermissions.Callback;
  6. import android.webkit.WebChromeClient;
  7. import android.webkit.WebView;
  8.                                             // for using geolocation method
  9. public  class MainActivity extends Activity implements GeolocationPermissions.Callback {
  10.    
  11.    
  12.     WebView mapview;    //just webview.
  13.     String url="file:///android_asset/www/index.html";  //local html file
  14.     protected void onCreate(Bundle savedInstanceState) {
  15.         super.onCreate(savedInstanceState);
  16.         setContentView(R.layout.activity_main);
  17.        
  18.        
  19.         mapview=(WebView) findViewById(R.id.mapview);   //casting webview
  20.         mapview.getSettings().setJavaScriptEnabled(true);                       //webview options
  21.         mapview.getSettings().setGeolocationEnabled(true);                      //
  22.         mapview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);   //
  23.        
  24.         Geoclient geoclient=new Geoclient();    //casting class
  25.        
  26.         mapview.setWebChromeClient(geoclient);  //set webchromeclient for permission
  27.         String origin="";  
  28.         geoclient.onGeolocationPermissionsShowPrompt(origin,this);  //for permission
  29.         mapview.loadUrl(url);
  30.     }
  31.         public void invoke(String origin, boolean allow, boolean retain) {
  32.        
  33.        
  34.     }
  35.        
  36.    
  37.     class Geoclient extends WebChromeClient{    //for display mylocation
  38.         @Override
  39.         public void onGeolocationPermissionsShowPrompt(String origin,Callback callback){
  40.            
  41.             super.onGeolocationPermissionsShowPrompt(origin, callback);
  42.             callback.invoke(origin,true,false);
  43.         }
  44.        
  45.     }
  46. }