xml
<RelativeLayout 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"
tools:context="com.example.mapp.MainActivity$PlaceholderFragment" >
<com.amap.api.maps2d.MapView
android:layout_marginTop="5dp"
android:id="@+id/map_map01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="30dp"
android:orientation="horizontal">
<TextView
android:textSize="15sp"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="起点 终点:"/>
<TextView
android:textColor="#FF0000"
android:id="@+id/text_address"
android:textSize="12sp"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="大兴------->通州"/>
</LinearLayout>
<LinearLayout
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<Button
android:id="@+id/but_bus"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent"
android:text="公交"/>
<Button
android:id="@+id/but_drive"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent"
android:text="自驾"/>
<Button
android:id="@+id/but_walk"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent"
android:text="步行"/>
</LinearLayout>
</RelativeLayout>
android代码
package com.example.mapp;
import java.util.List;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.AMap.OnMapClickListener;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.overlay.BusRouteOverlay;
import com.amap.api.maps2d.overlay.DrivingRouteOverlay;
import com.amap.api.maps2d.overlay.WalkRouteOverlay;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.GeocodeSearch.OnGeocodeSearchListener;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.amap.api.services.route.BusPath;
import com.amap.api.services.route.BusRouteResult;
import com.amap.api.services.route.DrivePath;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.RouteSearch.BusRouteQuery;
import com.amap.api.services.route.RouteSearch.DriveRouteQuery;
import com.amap.api.services.route.RouteSearch.OnRouteSearchListener;
import com.amap.api.services.route.RouteSearch.WalkRouteQuery;
import com.amap.api.services.route.WalkPath;
import com.amap.api.services.route.WalkRouteResult;
import android.support.v4.app.Fragment;
import android.telephony.SmsManager;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends Activity implements OnRouteSearchListener {
private MapView mapView;
private AMap aMap;
private Boolean isFirst;
private LatLng lat_star;
private LatLng lat_end;
private TextView text_address;
private StringBuffer sb = new StringBuffer();
private DriveRouteQuery query;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
mapView = (MapView) findViewById(R.id.map_map01);
mapView.onCreate(savedInstanceState);
aMap = mapView.getMap();
text_address = (TextView) findViewById(R.id.text_address);
findViewById(R.id.but_bus).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
rote("bus");
}
});
findViewById(R.id.but_drive).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
rote("drive");
}
});
findViewById(R.id.but_walk).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
rote("walk");
}
});
isFirst = true;
aMap.setOnMapClickListener(new OnMapClickListener() {
@Override
public void onMapClick(LatLng lat) {
if (isFirst) {
lat_star = new LatLng(lat.latitude, lat.longitude);
CodeToString(lat_star, MainActivity.this);
} else {
lat_end = new LatLng(lat.latitude, lat.longitude);
CodeToString(lat_end, MainActivity.this);
}
}
});
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
/**
* @param arg0坐标
* @param con
* 坐标和字符串地址转换
*/
public void CodeToString(LatLng arg0, Context con) {
System.out.println("--------------CodeToString---------------------"+ arg0.latitude + "---" +
arg0.longitude);
GeocodeSearch geo = new GeocodeSearch(con);
geo.setOnGeocodeSearchListener(new OnGeocodeSearchListener() {
private String address = "";
@Override
public void onRegeocodeSearched(RegeocodeResult result, int rCode) {
System.out
.println("----------onRegeocodeSearched------------------");
if (result != null
&& result.getRegeocodeAddress() != null
&& result.getRegeocodeAddress().getFormatAddress() != null) {
address = result.getRegeocodeAddress().getFormatAddress();
if (isFirst) {
if(sb.length()>2){
sb.delete(0, sb.length() - 1);// 清空
}
sb.append(address + "----");
text_address.setText(sb.toString());
System.out.println("------address---aa------------"+address+isFirst);
isFirst = false;
} else {
sb.append(address);
text_address.setText(sb.toString());
System.out.println("------address-----bb----------"+address+isFirst);
isFirst = true;
}
} else {
System.out.println("坐标转换不成功" + rCode);
}
}
@Override
public void onGeocodeSearched(GeocodeResult arg0, int arg1) {
System.out.println("--------onGeocodeSearched---------------");
}
});
LatLonPoint point = new LatLonPoint(arg0.latitude, arg0.longitude);
RegeocodeQuery query = new RegeocodeQuery(point, 100,
GeocodeSearch.AMAP);
geo.getFromLocationAsyn(query);
}
/**
* 路径规划
*/
protected void rote(String str) {
System.out.println("-----------------进入路线规划-----------------");
RouteSearch routeSerch = new RouteSearch(MainActivity.this);
routeSerch.setRouteSearchListener(MainActivity.this);
LatLonPoint start_lat = new LatLonPoint(lat_star.latitude,
lat_star.longitude);
LatLonPoint end_lat = new LatLonPoint(lat_end.latitude,
lat_end.longitude);
RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(start_lat,
end_lat);
if ("bus".equals(str)) {
BusRouteQuery query=new BusRouteQuery(fromAndTo,RouteSearch.BusDefault,null,0);
routeSerch.calculateBusRouteAsyn(query);// 异步路径规划驾车模式查询 //
} else if ("drive".equals(str)) {
query = new DriveRouteQuery(fromAndTo, RouteSearch.DrivingDefault,
null, null, "");
routeSerch.calculateDriveRouteAsyn(query);// 异步路径规划驾车模式查询 //
} else {
WalkRouteQuery query=new WalkRouteQuery(fromAndTo, RouteSearch.WalkDefault);
routeSerch.calculateWalkRouteAsyn(query);// 异步路径规划驾车模式查询 //
}
}
// 公交
@Override
public void onBusRouteSearched(BusRouteResult result, int rCode) {
System.out.println("-------------------------公交路径规划回调方法-------------------------"+ rCode + "---"+ result);
if(rCode==0){
if(result != null && result.getPaths() != null
&& result.getPaths().size() > 0){
BusRouteResult busRouteResult=result;
BusPath busPath=busRouteResult.getPaths().get(0);
aMap.clear();
BusRouteOverlay busRouteOverlay=new BusRouteOverlay(this,aMap, busPath, busRouteResult.getStartPos(), busRouteResult.getTargetPos());
busRouteOverlay.removeFromMap();
busRouteOverlay.addToMap();
busRouteOverlay.zoomToSpan();
}else {
Toast.makeText(MainActivity.this, "对不起 没有搜索到相关数据", 0).show();
}
}else if (rCode == 27) {
Toast.makeText(MainActivity.this, "搜索失败 请检查网络连接", 0).show();
} else if (rCode == 32) {
Toast.makeText(MainActivity.this, "验证无效", 0).show();
} else {
Toast.makeText(getApplicationContext(), "错误码" + rCode, 0).show();
}
}
// 自驾
@Override
public void onDriveRouteSearched(DriveRouteResult result, int rCode) {
System.out.println("-------------------------驾车路径规划回调方法-------------------------"+ rCode + "---"+ result);
if (rCode == 0) {
if (result != null && result.getPaths() != null
&& result.getPaths().size() > 0) {
DriveRouteResult driveRouteResult = result;
DrivePath drivePath = driveRouteResult.getPaths().get(0);
aMap.clear();// 清理地图上的所有覆盖物
DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay(
this,aMap, drivePath, driveRouteResult.getStartPos(),
driveRouteResult.getTargetPos());
drivingRouteOverlay.removeFromMap();
drivingRouteOverlay.addToMap();
drivingRouteOverlay.zoomToSpan();
} else {
Toast.makeText(MainActivity.this, "对不起 没有搜索到相关数据", 0).show();
}
} else if (rCode == 27) {
Toast.makeText(MainActivity.this, "搜索失败 请检查网络连接", 0).show();
} else if (rCode == 32) {
Toast.makeText(MainActivity.this, "验证无效", 0).show();
} else {
Toast.makeText(getApplicationContext(), "错误码" + rCode, 0).show();
}
}
// 步行
@Override
public void onWalkRouteSearched(WalkRouteResult result, int rCode) {
System.out.println("路径规划回调 -------------------------- 步行");
if (rCode == 0) {
if (result != null && result.getPaths() != null && result.getPaths().size() > 0) {
WalkRouteResult walkRouteResult = result;
WalkPath walkPath = walkRouteResult.getPaths().get(0);
aMap.clear();// 清理地图上的所有覆盖物
WalkRouteOverlay walkRouteOverlay = new WalkRouteOverlay(this, aMap, walkPath, walkRouteResult.getStartPos(), walkRouteResult.getTargetPos());
walkRouteOverlay.removeFromMap();
walkRouteOverlay.addToMap();
walkRouteOverlay.zoomToSpan();
} else {
Toast.makeText(MainActivity.this, "对不起 没有搜索到相关数据", 0).show();
}
} else if (rCode == 27) {
Toast.makeText(MainActivity.this, "搜索失败 请检查网络连接", 0).show();
} else if (rCode == 32) {
Toast.makeText(MainActivity.this, "验证无效", 0).show();
} else {
Toast.makeText(getApplicationContext(), "错误码" + rCode, 0).show();
}
}
}
效果图
版权声明:本文为shuiermengqi原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。