Sie sind auf Seite 1von 29

Development

 
Android  

Loca+on  Based  Services  


•  It  is  a  radio-­‐naviga+on  system  consis+ng  of  24  
GPS  
satellites  orbi+ng  the  earth,  and  their  respec+ve  
ground  sta+ons.  To  calculate  a  posi+on,  these  
satellites  serve  as  reference  points  when  
calcula+ng  posi+ons  rela+ve  to  a  maEer  of  
meters.  
•  In  other  words,  GPS  triangulates  the  posi+on  of  
a  satellite,  its  ground  sta+on  and  the  area  you  
want  to  locate.  However,  this  is  done  in  a  more  
scien+fic  and  finite  measurement  of  travel  +me,  
distance,  signal  experiences,  and  atmospheric  
delays.  
•  To  measure  GPS  points,  you  must  use  a  GPS  
receiver  that  calculates  its  posi+on  by  the  signals  
transmiEed  to  the  satellites.  These  satellites  
send  messages  containing  informa+on  such  as  
the  +me  the  message  was  sent  and  the  satellites  
orbital  informa+on.  Once  it  receives  this  
informa+on,  the  GPS  receiver  will  then  measure  
the  transit  +me  of  the  sent  message  and  
calculate  the  distance  of  the  GPS  receiver  to  the  
each  of  the  satellites.  
•  The  loca+on  is  then  displayed  through  GPS  maps  
Cell  tower  
•  An  alterna+ve  method  to  
determine  the  loca+on  of  a  cell  
phone  is  to  es+mate  its  distance  
to  three  nearby  cell  towers.  
•  Distance  of  the  phone  to  each  
antenna  could  be  es2mated  
based  upon  the  lag  2me  between  
the  moment  the  tower  sends  a  
ping  to  the  phone  and  receives  
the  answering  ping  back.  
Long  &  Lat  
•  La+tude  &  LongitudeLa+tude  in  GPS-­‐Decimal  
nota+on:+90.00000  (North)to-­‐90.000000  
(South)    
•  Longitude  GPS-­‐Decimal  nota+on:+180.000000  
(East)to-­‐180.000000  (West)  
Loca+on  based  services  (LBS)  
•  Different  technology  used  to  find  the  device’s  current  loca+on  
•  Two  main  elements  
–  Loca+onManager  
•  Provide  hooks  to  the  loca+on-­‐based  services  
–  Loca+onProvider  
•  Each  of  which  represents  a  different  loca+on-­‐finding  technology  used  to  
determine  the  device’s  current  loca+on  
•  Using  the  Loca+onManager,  you  can:  
–  Obtain  your  current  loca+on  
–  Track  Movement  
–  Set  proximity  alerts  for  detec+ng  movement  into  and  out  of  a  specified  
area  
Loca+on  Provider  
•  Depending  on  the  device,  there  may  be  several  
technologies  that  Android  can  use  to  determine  
the  current  loca+on  
•  Each  technology,  or  Loca+on  Provider,  will  offer  
different  capabili+es  including  power  
consump+on,  monetary  cost,  accuracy,  and  the  
ability  to  determine  al+tude,  speed,  or  heading  
informa+on  
Loca+on  Provider    
•  Loca+onManager  class  includes  sta+c  string  
constants  that  return  the  provider  name  for  the  
two  most  common  Loca+on  Providers  
–  Loca+onManager.GPS_PROVIDER  
–  Loca+onManager.NETWORK_PROVIDER  
•  To  get  a  list  of  names  for  all  the  providers  
Boolean  enabledOnly  =  true;  
List<String>  providers    
 =  loca+onManager.getProviders(enabledOnly);  
Loca+on  Provider  
•  Most  scenarios,  to  explicitly  choose  the  loca+on  provider  to  
use  
•  Specify  the  requirements  that  a  provider  must  meet  and  let  
Android  determine  the  best  technology  to  use  
•  Use  Criteria  class  to  dictate  the  requirements  of  a  provider  
in  terms  of    
–  Accuracy  
–  Power  
–  Cost  
–  Ability  to  return  values  of  al+tude,  speed,  and  bearing  
Criteria  Example  
•  E.g.  
 Criteria  criteria  =  new  Criteria();  
 criteria.setAccuracy(Criteria.ACCURACY_COARSE);  
 criteria.setPowerRequirement(Criteria.POWER_LOW);  
 criteria.setAl+tudeRequired(  false  );  
 criteria.setBearingRequired(  false  );  
 criteria.setSpeedRequired(  false  );  
 criteria.setCostAllowed(  true  );  

 String  bestProvider  =  loca+onManager.getBestProvider(  criteria,  


true  );  
Criteria  
•  If  more  than  one  Loca+on  Provider  matches,  
the  one  with  the  greatest  accuracy  is  returned.  
•  If  no  Loca+on  Provider  meet  requirements,  
the  criteria  is  loosened,  in  the  following  order:  
–  Power  used  
–  Accuracy  
–  Ability  to  return  bearing,  speed  and  al+tude  
Loca+on  Manager  
•  The  purpose  of  LBS  is  to  find  the  physical  loca+on  of  the  device  
•  Access  to  LBS  sis  handled  using  the  Loca+on  Manager  system  
Service.  
•  To  access  the  Loca+on  Manager,  request  an  instance  of  the  
LOCATION_SERVICE  using  the  getSystemService  method  
–  E.g.  
 String  serviceString  =  Context.LOCATION_SERVICE  
 Loca+onManager  loca+onManager;  
 loca+onManager  =  
   (Loca+onManager)  getSystemService(serviceString);  
Loca+on  Manager  
•  Before  usage  of  Loca+on  Manager  
•  Add  uses-­‐permission  tags  to  your  manifest  to  support  access  to  the  LBS  hardware  
•  Default  providers  
–  GPS  
–  Network  
•  GPS  provider  requires  Fine  permission  
•  Network  provider  requires  Coarse  permission  
–  E.g.  
 <uses-­‐permission    
 android:name  =  “android.permission.ACCESS_FINE_LOCATION”/>  
 <uses-­‐permission    
 android:name  =  “android.permission.ACCESS_COARSE_LOCATION”/>  
Retrieving  Loca+on  
•  Use  “getLastKnownLoca+on”  method  
–  E.g.  
 String  provider  =  Loca+onManager.GPS_PROVIDER  
 Loca+on  loca+on  =  loca+onManager.getLastKnowLoca+on(provider);  
–  Does  not  ask  the  Loca+on  Provider  to  update  the  current  Posi+on.  
•  Loca+on  object  returns  
–  La+tude  &  Longitude  
–  Bearing  
–  Speed  
–  Time  the  loca+on  fix  was  taken  
Geocoding  
•  Translate  between  street  addresses  and  
longitude/la+tude  map  coordinates.  
•  Geocoding  class  provides  two  geocoding  
func+ons:  
–  Forward  Geocoding  
•  Find  the  la+tude  and  longitude  of  an  address  
–  Reverse  Geocoding  
•  Find  the  street  address  for  a  given  la+tude  and  longitude  
Geocoding  
•  Results  are  contextualized  using  a  locale  
–  Locale  is  used  to  define  your  usual  loca+on  and  language  
–  E.g.  
 Geocoder  geocoder  =  new  Geocoder
(  getApplica+onContext(),  Locale.getDefault());  
•  Geocoding  func+ons  return  a  list  of  Address  objects  
•  List  can  contain  several  possible  results,  up  to  a  limit  
specified  when  making  the  call  
Geocoding:  warning  
•  Geocoding  lookups  are  performed  
synchronously,  so  they  will  block  the  calling  
thread.  
•  Slow  data  connec+ons  can  lead  to  an  
Applica+on  Unresponsive  dialog  
•  Best  to  move  lookups  into  a  Service  or  
background  thread  
Reverse  Geocoding  
•  Returns  street  addresss  for  physical  loca+ons,  
specified  by  latutude/longitude  pairs.  
•  Pass  the  target  la+tude  and  longitude  to  
Geocoder’s  getFromLoca+on  method  
•  Return  a  list  of  possible  addresses    
•  Return  null  if  could  not  resolve  any  addresses  
Reverse  Geocoding:  Example  
Loca+on  =  loca+onManager.getLastKnownLoca+on
(Loca+onManager.GPS_PROVIDER);  
Double  la+tude  =  loca+on.getLa+tude();  
Double  longitude  =  loca+on.getLongitude();  

Geocoder  gc  =  new  Geocoder(this,  Locale.getDefault());  

List<Address>  addresses  =  null;  


Try{  
 addresses    
   =  gc.getFromLoca+on(la+tude,  longitude,  10);  
}  catch(IOExcep+on  e)  {}  
Forward  Geocoding  
•  Determines  map  coordinates  for  a  given  loca+on  
•  Call  getFromLoca+onName  on  a  Geocoder  instance  
•  Pass  in  the  loca+on  and  the  maximum  number  of  results  to  return  
–  E.g.  
List<Address>  result  =  geocoder.getFromLoca+onName(aStreetAddress,  
maxResults);  
•  Returned  list  of  address  can  include  mul+ple  possible  matches  for  
the  named  loca+on  
•  Each  result  will  include  la+tude  and  longitude  and  any  addi+onal  
address  informa+on  avaliable  for  those  coordinates  
Forward  geocoding  
•  If  no  matches  are  found,  null  will  be  returned  
•  Availability,  accuracy,  and  granularity  of  geocoding  results  
will  depend  en+rely  on  the  database  available  for  the  area  
•  Locale  specified  when  crea+ng  the  geocoder  object  is  
par+cularly  important  
•  The  locale  provides  the  geographical  context  for  
interpre+ng  search  requests,  as  the  same  loca+on  names  
can  exist  in  mul+ple  areas.  
•  Consider  selec+ng  a  regional  Locale  to  help  avoid  place  
name  ambiguity  
Forward  Geocoding:  Example  
Geocoder  fwdGeocoder  =  Geocoder(this,  Locale.US);  
String  streetAddress=“180  Ang  Mo  Kio  Avenue  8  
Singapore”  
List<Address>  loca+ons  =  null;  
Try{  
 loca+ons  =  fwdGeocoder.getFromLoca+onName
(streetAddress,  10);  
}  
Map-­‐Based  Ac+vi+es  
•  Usage  of  MapView  
•  Feature  an  interac+ve  map  
•  Supports  both  Overlays  and  by  pinning  Views  to  geographical  
loca+ons  
•  Offer  control  of  the  map  display,  leong  you  control  the    
–  zoom    
–  loca+on    
–  display  modes  
•  Satellite  
•  Street  
•  Traffic  
Suppor+ng  classes  
•  MapView  
–  Actual  Map  View  
•  MapAc+vity  
–  Base  class  extended  to  create  a  new  Ac+vity  that  can  include  a  Map  View.  The  MapAc+vity  
class  handles  the  applica+on  life  cycle  and  background  service  management  required  for  
displaying  maps.  As  a  result,  can  only  use  a  MapView  within  MapAc+vity-­‐derived  Ac+vi+es  
•  Overlay  
–  Class  used  to  annotate  maps.  Enables  the  usage  of  a  canvas  to  draw  any  number  of  layers  that  
are  displayed  on  top  of  a  Map  View  
•  MapController  
–  Used  to  control  the  map,  allowing  developers  to  set  the  center  loca+on  and  zoom  levels  
•  MyLoca+onOverlay  
–  A  special  overlay  that  can  be  used  to  display  the  current  posi+on  and  orienta+on  of  the  device  
•  ItemizedOverlays  and  OverlayItems  
–  Used  together  to  create  a  layr  of  map  markers,  displayed  using  drawable  with  associated  text.  
Crea+ng  a  map-­‐based  ac+vity  
•  Create  a  new  Ac+vity  that  extends  MapAc+vity  
•  Add  a  MapView  to  the  layout  to  display  a  Google  Maps  interface  element  
•  Map  library  is  not  a  standard  package,  it  must  be  explicitly  included  in  the  
applica+on  manifest  
•  Add  using  a  “uses-­‐library”  tag  within  the  applica+on  node  
–  E.g.  
 <uses-­‐library  android:name=“com.google.android.maps”/>  
•  Google  map  downloads  the  map  +les  on  demand  
•  Requires  permission  to  use  the  internet  
–  E.g.  
<uses-­‐permission  android:name=“android.permission.INTERNET”/>    
Example:  Java  
public  class  myMap  extends  MapAc+vity  {  

private  MapView  mapView;  

       /**  Called  when  the  ac+vity  is  first  created.  */  


       @Override  
       public  void  onCreate(Bundle  savedInstanceState)  {  
               super.onCreate(savedInstanceState);  
               setContentView(R.layout.main);  
               mapView  =  (MapView)findViewById(R.id.map_view);  
       }  
}  
Example:  Xml  
<LinearLayout  xmlns:android="h>p://schemas.android.com/apk/res/android"  
       android:orienta+on="ver2cal"  
       android:layout_width="fill_parent"  
       android:layout_height="fill_parent"  
       >  
<com.google.android.maps.MapView      
android:id="@+id/map_view"  
       android:layout_width="fill_parent"    
       android:layout_height="fill_parent"    
       android:enabled="true"  
       android:clickable="true"  
       android:apiKey="0UDG_KZRwXZYEdpMSH1ZAyHbpqWKtw6BgZOyX0Q"  
       />  
•  </LinearLayout>  
Example:  Manifest  
<manifest  xmlns:android="h>p://schemas.android.com/apk/res/android"  
           package="com.nyp.android.test.myMap"  
           android:versionCode="1"  
           android:versionName="1.0">  
       <applica+on  android:icon="@drawable/icon"  android:label="@string/app_name">  
               <ac+vity  android:name=".myMap"  
                                   android:label="@string/app_name">  
                       <intent-­‐filter>  
                               <ac+on  android:name="android.intent.ac2on.MAIN"  />  
                               <category  android:name="android.intent.category.LAUNCHER"  />  
                       </intent-­‐filter>  
               </ac+vity>  
 <uses-­‐library  android:name="com.google.android.maps"/>  

       </applica+on>  

<uses-­‐permission  android:name="android.permission.INTERNET"/>  

</manifest>    
More  examples..  
•  @Prac+cal  

Das könnte Ihnen auch gefallen