基于android的GPS导航软件

本文详细介绍了如何使用Android平台开发GPS导航软件,包括获取当前位置和通过地址获取坐标的功能。通过集成Google Maps API,实现动态更新MapView,并在地图上进行标记。重点讨论了GPS硬件获取坐标和网络地址解析坐标的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 最近做一个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");
		}
	}

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

使用GPS 定位,首先,需要在清单文件(AndroidManifest.xml)中注册获取定位的权限: **1.获取位置管理器对象LocationManager** ``` import android.location.LocationManager; LocationManager lm; // lm =(LocationManager) this.getSystemService(Context`.LOCATION_SERVICE); // ``` **2.一般使用LocationManager的getLastKnownLocation(LocationManager.GPS_PROVIDER);方法获取Location对象** ``` String provider = LocationManager.GPS_PROVIDER;// 指定LocationManager的定位方法 Location location = locationManager.getLastKnownLocation(provider);// 调用getLastKnownLocation()方法获取当前的位置信息 ``` 不过不建议用这种方法,有几点原因: 一,在很多提供定位服务的应用程序中,不仅需要获取当前的位置信息,还需要监视位置的变化,在位置改变时调用特定的处理方法 ,其中LocationManager提供了一种便捷、高效的位置监视方法requestLocationUpdates(),可以根据位置的距离变化和时间间隔设定,产生位置改变事件的条件,这样可以避免因微小的距离变化而产生大量的位置改变事件 。 二,当你开启GPS,provider的值为GPS。这时的定位方式为GPS,由于GPS定位慢,所以它不可能立即返回你一个Location对象,所以就返回null了。 **3.推荐locationManager.requestLocationUpdates();方法** LocationManager中设定监听位置变化的代码如下: ``` lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10,new MyLocationListener()); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值