实现查询地址和路线导航的代码

来源:互联网 发布:lol数据受损 编辑:程序博客网 时间:2024/04/20 22:10
    以下代码主要实现的是根据地址名得到经纬度,并在地图上显示及路线规划,导航。收藏路线和查看路线。

源代码:

package com.logistics;

import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.android.maps.GeoPoint;


public class Map_index extends Activity {

private GeoPoint fromGeoPoint, toGeoPoint;
private EditText mEditText01;
private EditText mEditText02;
private Button mButton01,mButton02;
private String TAG = "HIPPO_GEO_DEBUG";

public static final int SAVEROUTE = Menu.FIRST;
public static final int ROUTELIST = Menu.FIRST + 1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_citytocity);
mButton01 = (Button) findViewById(R.id.search_route);
mButton02 = (Button) findViewById(R.id.map_cancel);

mEditText01 = (EditText) findViewById(R.id.search_startingpoint);
mEditText01.setText(getResources()
.getText(R.string.str_default_starting_point).toString());
mEditText02 = (EditText) findViewById(R.id.search_destination);
mEditText02.setText(getResources()
.getText(R.string.str_default_address).toString());

// 按钮 查询地址规划路径
mButton01.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) { // 选择方式
final CharSequence[] items = { "乘车", "步行", "驾车" };
AlertDialog.Builder builder = new AlertDialog.Builder(
Map_index.this);
builder.setTitle("选择交通方式");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
StringBuilder params = new StringBuilder()
.append("&dirflg=");
switch (item) {
case 0:
params.append("r");
break;
case 1:
params.append("w");
break;
case 2:
params.append("d");
break;
default:
break;
}
getMap(params.toString());
}
});

AlertDialog alert = builder.create();
alert.show();
}
});

// 取消
mButton02.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
finish();
}
});

}



// TODO Auto-generated method stub
protected void getMap(String params) {
if (mEditText01.getText().toString() != ""
& mEditText01.getText().toString() != "") {
/* 取得User要前往的GeoPoint对象 */
fromGeoPoint = getGeoByAddress(mEditText01.getText().toString());
toGeoPoint = getGeoByAddress(mEditText02.getText().toString());

/* 路径规划Intent */
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);

/* 传入路径规划所需要的地标地址 */

intent.setData(Uri.parse("http://maps.google.com/maps?f=d&saddr="
+ GeoPointToString(fromGeoPoint) + "&daddr="
+ GeoPointToString(toGeoPoint) + "&hl=cn&t=m&" + params));
// 强制使用谷歌地图打开
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
& Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
intent.setClassName("com.google.android.apps.maps",
"com.google.android.maps.MapsActivity");
startActivity(intent);
}
}





/* 从输入的Address地址,取得其GeoPoint对象 */
private GeoPoint getGeoByAddress(String strSearchAddress) {
GeoPoint gp = null;
try {
if (strSearchAddress != "") {
Geocoder mGeocoder01 = new Geocoder(Map_index.this, Locale
.getDefault());

List<Address> lstAddress = mGeocoder01.getFromLocationName(
strSearchAddress, 1);
if (!lstAddress.isEmpty()) {
Address adsLocation = lstAddress.get(0);
double geoLatitude = adsLocation.getLatitude() * 1E6;
double geoLongitude = adsLocation.getLongitude() * 1E6;
gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);
} else {

Log.i(TAG, "Address Geopoint not Found.");
}
}
} catch (Exception e) {
e.printStackTrace();
}
return gp;
}

/* 将Geopoint里的经纬度以String,String返回 */
private String GeoPointToString(GeoPoint gp) {
String strReturn = "";
try {
/* 当Location存在 */
if (gp != null) {
double geoLatitude = (int) gp.getLatitudeE6() / 1E6;
double geoLongitude = (int) gp.getLongitudeE6() / 1E6;
strReturn = String.valueOf(geoLatitude) + ","
+ String.valueOf(geoLongitude);
}
} catch (Exception e) {
e.printStackTrace();
}
return strReturn;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, SAVEROUTE, 0, "收藏路线")
.setIcon(getResources().getDrawable(R.drawable.list_add));
menu.add(0, ROUTELIST, 1, "路线列表")
.setIcon(getResources().getDrawable(R.drawable.business));
return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case SAVEROUTE:
Long mRowId=-1L;
Intent intent1 = new Intent(Map_index.this,
MapCollectionPath.class);

Bundle fpoint = new Bundle();
fpoint.putLong("mRowId", mRowId);
fpoint.putString("fav_startingpoint", mEditText01.getText()
.toString());
fpoint.putString("fav_destination", mEditText02.getText()
.toString());
fpoint.putString("title","");
intent1.putExtras(fpoint);
startActivity(intent1);
break;


case ROUTELIST:
Intent intent2 = new Intent(Map_index.this, MapList.class);
startActivity(intent2);
break;
}
return super.onOptionsItemSelected(item);
}


}
1 0