基于Android的GPS导航软件


最近做一个GPS导航软件,其中主要是用Android读取网络上的google地图,然后显示在手机上。可以实现位置查询,输入一个位置,可以读取到相应的坐标,然后通过坐标,动态的更新MapView.

1.如果获取现在的位置可以通过手机的gps硬件或者网络,通过Provider来获取坐标。

  1. myLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);  
  2.      
  3.    //用于真机测试   
  4. myLocation = myLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);  

再通过Location获得坐标,代码:

  1. if(location!=null){  
  2.     //得到经度和纬度   
  3.     double longitude = location.getLongitude();  
  4.     double latitude = location.getLatitude();  
  5.     int a=(int) (longitude*1E6);  
  6.     int b=(int) (latitude*1E6);  
  7.     System.out.println(a+"  "+b);  
  8.     GeoPoint geoPoint= new GeoPoint(b,a);             
  9. }  

2.如果通过地方来获取现在的坐标,可以利用下面的代码。

  1. private void updateFromAddress(String address){  
  2.     Geocoder gc = new Geocoder(this, Locale.getDefault());  
  3.     List<Address> locations = null;  
  4.     try {  
  5.         locations=gc.getFromLocationName(address, 10);  
  6.         if(locations.size()>0)  
  7.         {  
  8.             Address myAddress =locations.get(0);   
  9.             double lantitude = myAddress.getLatitude();  
  10.             double longitude = myAddress.getLongitude();  
  11.             System.out.println(lantitude+"  "+longitude);  
  12.             lastGeoPoint = getGpFromDouble(lantitude, longitude);//自己定义的函数得到坐标点。   
  13.             System.out.println(lastGeoPoint);  
  14.             updateMapView(lastGeoPoint);//通过坐标更新地图   
  15.             sign(lastGeoPoint);//在地图上进行标记   
  16.         }  
  17.         else   
  18.             System.out.println("locations is null");  
  19.           
  20.     }  
  21.     catch(Exception e )  
  22.     {  
  23.         System.out.println("catch exception");  
  24.     }  
  25. }  

加注释的代码都是自己定义的函数,这里只说明与主题所表述相关的功能。

相关内容