最近做一个GPS导航软件,其中主要是用android读取网络上的google地图,然后显示在手机上。可以实现位置查询,输入一个位置,可以读取到相应的坐标,然后通过坐标,动态的更新MapView.
1.如果获取现在的位置可以通过手机的gps硬件或者网络,通过Provider来获取坐标。
myLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); //用于真机测试 myLocation = myLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
再通过Location获得坐标,代码:
if(location!=null){ //得到经度和纬度 double longitude = location.getLongitude(); double latitude = location.getLatitude(); int a=(int) (longitude*1E6); int b=(int) (latitude*1E6); System.out.println(a+" "+b); GeoPoint geoPoint= new GeoPoint(b,a); }
2.如果通过地方来获取现在的坐标,可以利用下面的代码。
private void updateFromAddress(String address){ Geocoder gc = new Geocoder(this, Locale.getDefault()); List<Address> locations = null; try { locations=gc.getFromLocationName(address, 10); if(locations.size()>0) { Address myAddress =locations.get(0); double lantitude = myAddress.getLatitude(); double longitude = myAddress.getLongitude(); System.out.println(lantitude+" "+longitude); lastGeoPoint = getGpFromDouble(lantitude, longitude);//自己定义的函数得到坐标点。 System.out.println(lastGeoPoint); updateMapView(lastGeoPoint);//通过坐标更新地图 sign(lastGeoPoint);//在地图上进行标记 } else System.out.println("locations is null"); } catch(Exception e ) { System.out.println("catch exception"); } }
加注释的代码都是自己定义的函数,这里只说明与主题所表述相关的功能。
基于android的GPS导航软件
最新推荐文章于 2022-10-03 23:53:18 发布