现在的位置: 首页 > 综合 > 正文

android google maps之 简单交通图(三)

2013年08月14日 ⁄ 综合 ⁄ 共 3313字 ⁄ 字号 评论关闭

看完http://blog.csdn.net/woshishuoshuoa/article/details/9830813后,我们已经可以在我们想要的俩点间画一条简单的交通图了,而这篇文章则可以实现移动一下位置,便可以画出原目的地到新的地点之间的交通图了哦~~~仅仅是在原来的项目修改了MainActivity,源代码如下:

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.PolylineOptions;
import android.app.ProgressDialog;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

public class MainActivity extends FragmentActivity implements LocationListener{
	LocationManager locationManager;
    MapFragment mFragment;
    GoogleMap gMap;
    ProgressDialog progressDialog;
    boolean flag=true;
    String pro="gps";
    Handler handler;
    LatLng latLng;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		SupportMapFragment sFragment=(SupportMapFragment) this.getSupportFragmentManager().findFragmentById(R.id.map);//获得map对象
		gMap=sFragment.getMap();// 得到一个参考的地图
		gMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
		gMap.setTrafficEnabled(true);//交通图
		gMap.setMyLocationEnabled(true);
		locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);// 获取位置管理服务
	    if (!locationManager.isProviderEnabled("gps")) {
			pro="network";
			Toast.makeText(this, "请打开GPS", 2000).show();
			if (!locationManager.isProviderEnabled("network")) {
				Toast.makeText(this, "请打开网络", 2000).show();
			    pro=LocationManager.PASSIVE_PROVIDER;
			    if (!locationManager.isProviderEnabled(pro)) {
			    	Toast.makeText(this, "请打开网络。。。", 3000);
				}
			}
			else {
				flag=true;
			}
	    }
	    else {
			flag=true;
		}
		if (flag) {
     	   progressDialog=new ProgressDialog(this);
	       progressDialog.setTitle("定位");
	       progressDialog.setMessage("正在定位。。。");
	       progressDialog.show();
	  	}
	}
    @Override
    protected void onResume() {
    	// TODO Auto-generated method stub
    	locationManager.requestLocationUpdates(pro, 500, 0, this);
    	super.onResume();
    }
    @Override
    protected void onPause() {
    	// TODO Auto-generated method stub
    	locationManager.removeUpdates(this);
    	super.onPause();
    }
	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		progressDialog.hide();
		if (location==null) {
			Toast.makeText(MainActivity.this, "末有定位", 1000).show();
		}
		else{
				 LatLng tempLatLng=new LatLng(location.getLatitude(), location.getLongitude());
				 if (latLng==null) {
				 latLng=tempLatLng;
				 	}
				else{
					PolylineOptions polylineOptions=new PolylineOptions();
					polylineOptions.add(latLng);
					polylineOptions.add(tempLatLng);
					gMap.addPolyline(polylineOptions);
				}
				gMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));//把摄像头移过来
				gMap.animateCamera(CameraUpdateFactory.zoomTo(15));
		}
		
	}
	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		//network gps 北斗
	}
	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
	}
	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		//loation是根据当前可用的provider获取的
//		List<String> providers=locationManager.getAllProviders();
			// 查找到服务信息
		//	Criteria criteria=new Criteria();
		//	String bestProvider=locationManager.getBestProvider(criteria, false); // 低功耗		 
	}

}

效果图这里就不贴出来了,由于代码做的不够完善,所以运行结果会受安卓版本的影响的~~~欢迎喜欢安卓的亲们给出更为完善的代码哦~~~

抱歉!评论已关闭.