导依赖
java
xml
<!--允许程序打开网络套接字-->
<uses-permission android:name="android.permission.INTERNET" />
<!--允许程序设置内置sd卡的写权限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!--允许程序获取网络状态-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!--允许程序访问WiFi网络信息-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!--允许程序读写手机状态和身份-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!--允许程序访问CellID或WiFi热点来获取粗略的位置-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="85b5d31b064042c527907ebe7803624c">
//开发者申请的key
</meta-data>
一堆图片
<com.amap.api.maps.MapView
android:id="@+id/MapView"
android:layout_width="match_parent"
android:layout_height="match_parent"></com.amap.api.maps.MapView>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="lishuya.jiyun.com.day18lianximap.MainActivity">
<LinearLayout
android:id="@+id/Top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_gravity="center_vertical"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始位置:" />
<EditText
android:id="@+id/Start_Place"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="开始位置" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始位置:" />
<EditText
android:id="@+id/End_Place"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="结束位置" />
</LinearLayout>
<Button
android:id="@+id/Submit_Btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索" />
</LinearLayout>
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private EditText Start_Place;
private EditText End_Place;
private Button Submit_Btn;
//开始位置和结束位置的字符串
private String startPlace, endPlace;
//查询你自己输入的路径的
private GeocodeSearch searchStart;
private GeocodeSearch searchEnd;
//这两个类是用来得到经纬度
private LatLonPoint latLonPointStart;
private LatLonPoint latLonPointEnd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
searchStart = new GeocodeSearch(this);
searchEnd = new GeocodeSearch(this);
}
private void initView() {
Start_Place = (EditText) findViewById(R.id.Start_Place);
End_Place = (EditText) findViewById(R.id.End_Place);
Submit_Btn = (Button) findViewById(R.id.Submit_Btn);
Submit_Btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.Submit_Btn:
startPlace = Start_Place.getText().toString();
endPlace = End_Place.getText().toString();
if (startPlace.isEmpty() || endPlace.isEmpty()) {
Toast.makeText(this, "请重新输入地址", Toast.LENGTH_SHORT).show();
} else {
//初始化开始位置的经纬度
initStart();
}
break;
}
}
//初始化终点路径
private void initEnd() {
GeocodeQuery query1 = new GeocodeQuery(endPlace, "");
searchEnd.getFromLocationNameAsyn(query1);
searchEnd.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {
@Override
public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
}
@Override
public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
GeocodeAddress addr = geocodeResult.getGeocodeAddressList().get(0);
latLonPointEnd = addr.getLatLonPoint();
Log.e("end", latLonPointEnd.toString());
if (latLonPointEnd == null || latLonPointStart == null) {
Toast.makeText(MainActivity.this, "正在异步处理", Toast.LENGTH_SHORT).show();
} else {
Intent intent = new Intent(MainActivity.this, HomeActivity.class);
Bundle bundle = new Bundle();
//序列化的第二种方式
bundle.putParcelable("start", latLonPointStart);
bundle.putParcelable("end", latLonPointEnd);
intent.putExtras(bundle);
startActivity(intent);
}
}
});
}
private void initStart() {
GeocodeQuery query = new GeocodeQuery(startPlace, "");
//查询你输入的字符串
searchStart.getFromLocationNameAsyn(query);
searchStart.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {
@Override
public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
}
@Override
public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
GeocodeAddress addr = geocodeResult.getGeocodeAddressList().get(0);
latLonPointStart = addr.getLatLonPoint();
Log.e("start", latLonPointStart.toString());
initEnd();
}
});
}
}
//3D地图so及jar
compile 'com.amap.api:3dmap:latest.integration'
//定位功能
compile 'com.amap.api:location:latest.integration'
//搜索功能
compile 'com.amap.api:search:latest.integration'
testCompile 'junit:junit:4.12'