一、main.xml布局,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation
="vertical" android:layout_width="fill_parent"
android:layout_height
="fill_parent">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation
="horizontal"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content">
<EditText android:id="@+id/etFrom"
android:layout_width
="130px"
android:layout_height
="wrap_content"
android:text
="@string/etFrom"/>
<TextView android:gravity="center_horizontal"
android:layout_width
="55px"
android:layout_height
="wrap_content"
android:text
="@string/to"/>
<EditText android:id="@+id/etTo"
android:layout_width
="130px"
android:layout_height
="wrap_content"
android:text
="@string/etTo"/>
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation
="horizontal"
android:gravity
="center_horizontal"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content">
<Button android:id="@+id/btnFrom"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:text
="@string/btnFrom"/>
<Button android:id="@+id/btnTo"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:text
="@string/btnTo"/>
<Button android:id="@+id/btnSearch"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:text
="@string/btnSearch"/>
</LinearLayout>
<com.google.android.maps.MapView android:id="@+id/mapView"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:clickable
="true"
android:apiKey
="0ZUHwocAEeJEiMatLbTddLH_rS92w_CsVyGuNKQ"/>
</LinearLayout>

二、string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, GoogleMapActivity!</string>
<string name="app_name">谷歌地图应用</string>
<string name="btnSearch">查询路径</string>
<string name="to"></string>
<string name="etFrom">东街口</string>
<string name="etTo">国货西路</string>
<string name="btnFrom">显示起点</string>
<string name="btnTo">显示终点</string>
</resources>

三、类GoogleMapActivity

package com.ljq.activity;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

public class GoogleMapActivity extends MapActivity {
private EditText etFrom; //起始地点
private EditText etTo; //终止地点
private Button btnSearch; //路线查询
private Button btnFrom; //显示起始地点按钮
private Button btnTo;//显示终止地点按钮
private MapView mapView;
private MapController mapController;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//初始化
etFrom=(EditText)findViewById(R.id.etFrom);
etTo=(EditText)findViewById(R.id.etTo);
btnSearch=(Button)findViewById(R.id.btnSearch);
btnFrom=(Button)findViewById(R.id.btnFrom);
btnTo=(Button)findViewById(R.id.btnTo);
mapView=(MapView)findViewById(R.id.mapView);
mapController=mapView.getController();

mapController.setZoom(15);
btnSearch.setOnClickListener(listener);
btnFrom.setOnClickListener(listener);
btnTo.setOnClickListener(listener);
}


private OnClickListener listener=new OnClickListener() {

public void onClick(View v) {
String etFrom=GoogleMapActivity.this.etFrom.getText().toString();
String etTo=GoogleMapActivity.this.etTo.getText().toString();
if(v==btnSearch){
GeoPoint formGeoPoint = getGeoPointByAddress(etFrom);
GeoPoint toGeoPoint = getGeoPointByAddress(etTo);

Intent intent=new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setData(Uri.parse(
"http://maps.google.com/maps?f=d&saddr="
+ geoPointToString(formGeoPoint) +"&daddr="
+geoPointToString(toGeoPoint)+"&h1=cn"));
startActivity(intent);
}else if(v==btnFrom){
//得到起始点的GeoPoint
GeoPoint formGeoPoint = getGeoPointByAddress(etFrom);
if(formGeoPoint != null){
//显示到formGeoPoint点
GoogleMapActivity.this.mapController.animateTo(formGeoPoint);
}
}else if(v==btnTo){
//得到起始点的GeoPoint
GeoPoint toGeoPoint = getGeoPointByAddress(etTo);
if(toGeoPoint != null){
//显示到formGeoPoint点
GoogleMapActivity.this.mapController.animateTo(toGeoPoint);
}
}

}

};

@Override
protected boolean isRouteDisplayed() {
return false;
}

/**
* 将经纬度转换为字符串
*
*
@return
*/
private String geoPointToString(GeoPoint geoPoint){
String result="";
if(geoPoint!=null){
double longitude=(int)geoPoint.getLongitudeE6()/1E6;
double latitude=(int)geoPoint.getLatitudeE6()/1E6;
result=String.valueOf(latitude)+","+String.valueOf(longitude);
}
return result;
}

/**
* 通过地址得到GeoPoint
*
*
*
@param address
*
@return
*/
private GeoPoint getGeoPointByAddress(String address){
GeoPoint geoPoint = null;
if(address != ""){
//Geocoder类根据地理名称得到address的方法,getFromLocation()与getFromLocationName()
//Geocoder myGeocoder = new Geocoder(this); //会出现java.io.IOException: Unable to parse response from server异常
Geocoder myGeocoder = new Geocoder(getBaseContext(), Locale.CHINA);
List<Address> addressList = null;
try {
addressList = myGeocoder.getFromLocationName(address, 1);
if(!addressList.isEmpty()){
//获取第一个Address
Address tempAddress = addressList.get(0);
double latitude = tempAddress.getLatitude()*1E6;
double longitude = tempAddress.getLongitude()*1E6;
geoPoint = new GeoPoint((int)latitude, (int)longitude);
}
} catch (IOException e) {
e.printStackTrace();
}
}
return geoPoint;
}
}

四、清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ljq.activity"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".GoogleMapActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:name="com.google.android.maps"/>
</application>
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.INTERNET" />

</manifest>

五、运行结果





更多相关文章

  1. Android里面从Dialog中获取返回值并写入指定TextView组件
  2. Android(安卓)APP--编写简单的答题器
  3. Android中FloatingActionButton实现悬浮按钮实例
  4. Android学习笔记(6)——Android——LoginDemo
  5. Android中浮动按钮
  6. Android按钮实现点击事件的四种方式
  7. Android单选按钮对话框用法实例分析
  8. Android(安卓)管理Fragments
  9. 自定义searchview的编辑框,搜索按钮,删除按钮,光标等

随机推荐

  1. 弹性负载均衡(Elastic Load Balance,ELB)
  2. 云服务器备份(Cloud Server Backup Servic
  3. 云硬盘(Elastic Volume Service,EVS)
  4. 镜像服务(Image Management Service,IMS)
  5. 裸金属服务器(Bare Metal Server,BMS)
  6. cxf 服务端启动 报错ServiceConstruction
  7. base64和图片互转
  8. 常用脚本集锦
  9. pandas处理json数据
  10. Kubernetes容器集群管理环境 - 完整部署(