Java 用于显示不工作的驾驶路线的Android项目
根据本教程,我正在开发一个android应用程序,用于显示两点之间的驾驶路线- ,但我的应用程序在运行后停止。 我不使用kml,因为我知道谷歌不再支持kml。 但是,当我运行这段代码时,它停止了。 谁能告诉我,原因是什么 编辑结果表明,我的androidmanfest.xml中没有包含谷歌地图库。 现在,它没有显示任何错误,但在手机上安装后,它崩溃了。 谁能告诉我,为什么 谢谢 路线路径主要活动Java 用于显示不工作的驾驶路线的Android项目,java,android,google-maps,driving-directions,Java,Android,Google Maps,Driving Directions,根据本教程,我正在开发一个android应用程序,用于显示两点之间的驾驶路线- ,但我的应用程序在运行后停止。 我不使用kml,因为我知道谷歌不再支持kml。 但是,当我运行这段代码时,它停止了。 谁能告诉我,原因是什么 编辑结果表明,我的androidmanfest.xml中没有包含谷歌地图库。 现在,它没有显示任何错误,但在手机上安装后,它崩溃了。 谁能告诉我,为什么 谢谢 路线路径主要活动 public class RoutePath extends MapActivity {
public class RoutePath extends MapActivity {
MapView mapView;
private RoutePath _activity;
GeoPoint srcGeoPoint,destGeoPoint;
private static List<Overlay> mOverlays;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedData data = SharedData.getInstance();
mapView = new MapView(this,data.getAPIKEY());
mapView.setClickable(true);
setContentView(mapView);
_activity = this;
double src_lat = data.getSrc_lat();
double src_long = data.getSrc_lng();
double dest_lat = data.getDest_lat();
double dest_long = data.getDest_lng();
if(src_lat == -1 || src_long == -1 || dest_lat == -1 || dest_long == -1){
showAlert("Please enter source and destination points");
}else{
srcGeoPoint = new GeoPoint((int) (src_lat * 1E6),(int) (src_long * 1E6));
destGeoPoint = new GeoPoint((int) (dest_lat * 1E6),(int) (dest_long * 1E6));
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable srcdrawable = this.getResources().getDrawable(R.drawable.pin_green);
CustomItemizedOverlay srcitemizedOverlay = new CustomItemizedOverlay(srcdrawable);
//CustomItemizedOverlay srcitemizedOverlay = new CustomItemizedOverlay(getDrawable("com/agarwal/route/pin_green.png"));
OverlayItem srcoverlayitem = new OverlayItem(srcGeoPoint, "Hello!", "This is your Location.");
Drawable destdrawable = this.getResources().getDrawable(R.drawable.pin_red);
CustomItemizedOverlay destitemizedOverlay = new CustomItemizedOverlay( destdrawable );
// CustomItemizedOverlay destitemizedOverlay = new CustomItemizedOverlay(getDrawable("com/agarwal/route/pin_red.png"));
OverlayItem destoverlayitem = new OverlayItem(destGeoPoint, "Hello!", "This is dest Location.");
srcitemizedOverlay.addOverlay(srcoverlayitem);
destitemizedOverlay.addOverlay(destoverlayitem);
mapOverlays.add(srcitemizedOverlay);
mapOverlays.add(destitemizedOverlay);
connectAsyncTask _connectAsyncTask = new connectAsyncTask();
_connectAsyncTask.execute();
mapView.setBuiltInZoomControls(true);
mapView.displayZoomControls(true);
mOverlays = mapView.getOverlays();
mapView.getController().animateTo(srcGeoPoint);
mapView.getController().setZoom(12);
//SharedData data = SharedData.getInstance();
data.setAPIKEY("APIKEY");
data.setSrc_lat(17);
data.setSrc_lng(78);
data.setDest_lat(18);
data.setDest_lng(77);
startActivity(new Intent(RoutePath.this,RoutePath.class));
}
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
private class connectAsyncTask extends AsyncTask<Void, Void, Void>{
private ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
progressDialog = new ProgressDialog(_activity);
progressDialog.setMessage("Fetching route, Please wait...");
progressDialog.setIndeterminate(true);
progressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
fetchData();
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if(doc!=null){
Overlay ol = new MyOverlay(_activity,srcGeoPoint,srcGeoPoint,1);
mOverlays.add(ol);
NodeList _nodelist = doc.getElementsByTagName("status");
Node node1 = _nodelist.item(0);
String _status1 = node1.getChildNodes().item(0).getNodeValue();
if(_status1.equalsIgnoreCase("OK")){
NodeList _nodelist_path = doc.getElementsByTagName("overview_polyline");
Node node_path = _nodelist_path.item(0);
Element _status_path = (Element)node_path;
NodeList _nodelist_destination_path = _status_path.getElementsByTagName("points");
Node _nodelist_dest = _nodelist_destination_path.item(0);
String _path = _nodelist_dest.getChildNodes().item(0).getNodeValue();
List<GeoPoint> _geopoints = decodePoly(_path);
GeoPoint gp1;
GeoPoint gp2;
gp2 = _geopoints.get(0);
Log.d("_geopoints","::"+_geopoints.size());
for(int i=1;i<_geopoints.size();i++) // the last one would be crash
{
gp1 = gp2;
gp2 = _geopoints.get(i);
Overlay ol1 = new MyOverlay(gp1,gp2,2,Color.BLUE);
mOverlays.add(ol1);
}
Overlay ol2 = new MyOverlay(_activity,destGeoPoint,destGeoPoint,3);
mOverlays.add(ol2);
progressDialog.dismiss();
}else{
showAlert("Unable to find the route");
}
Overlay ol2 = new MyOverlay(_activity,destGeoPoint,destGeoPoint,3);
mOverlays.add(ol2);
progressDialog.dismiss();
mapView.scrollBy(-1,-1);
mapView.scrollBy(1,1);
}else{
showAlert("Unable to find the route");
}
}
}
Document doc = null;
private void fetchData()
{
StringBuilder urlString = new StringBuilder();
urlString.append("http://maps.google.com/maps/api/directions/xml?origin=");
urlString.append( Double.toString((double)srcGeoPoint.getLatitudeE6()/1.0E6 ));
urlString.append(",");
urlString.append( Double.toString((double)srcGeoPoint.getLongitudeE6()/1.0E6 ));
urlString.append("&destination=");//to
urlString.append( Double.toString((double)destGeoPoint.getLatitudeE6()/1.0E6 ));
urlString.append(",");
urlString.append( Double.toString((double)destGeoPoint.getLongitudeE6()/1.0E6 ));
urlString.append("&sensor=true&mode=driving");
Log.d("url","::"+urlString.toString());
HttpURLConnection urlConnection= null;
URL url = null;
try
{
url = new URL(urlString.toString());
urlConnection=(HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = (Document) db.parse(urlConnection.getInputStream());//Util.XMLfromString(response);
}catch (MalformedURLException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}catch (ParserConfigurationException e){
e.printStackTrace();
}
catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private List<GeoPoint> decodePoly(String encoded) {
List<GeoPoint> poly = new ArrayList<GeoPoint>();
int index = 0, len = encoded.length();
int lat = 0, lng = 0;
while (index < len) {
int b, shift = 0, result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
GeoPoint p = new GeoPoint((int) (((double) lat / 1E5) * 1E6),
(int) (((double) lng / 1E5) * 1E6));
poly.add(p);
}
return poly;
}
private void showAlert(String message){
AlertDialog.Builder alert = new AlertDialog.Builder(_activity);
alert.setTitle("Error");
alert.setCancelable(false);
alert.setMessage(message);
alert.setPositiveButton("Ok",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
alert.show();
}
private Drawable getDrawable(String fileName){
return Drawable.createFromStream(_activity.getClass().getClassLoader().getResourceAsStream(fileName), "pin");
}
}
公共类RoutePath扩展了MapActivity{
地图视图;
私人路线路径活动;
地质点SRCEOPENT,DESTGEOPENT;
私有静态列表移动列表;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
SharedData=SharedData.getInstance();
mapView=新的mapView(这个,data.getAPIKEY());
mapView.setClickable(真);
setContentView(地图视图);
_活动=此;
double src_lat=data.getSrc_lat();
double src_long=data.getSrc_lng();
double dest_lat=data.getDest_lat();
double dest_long=data.getDest_lng();
如果(src|u lat==-1 | | src|u long==-1 | | dest|u lat==-1 | | dest|u long==-1){
showAlert(“请输入源点和目标点”);
}否则{
srcGeoPoint=新的地质点((int)(src_lat*1E6),(int)(src_long*1E6));
dest GeoPoint=新的地质点((int)(dest_lat*1E6),(int)(dest_long*1E6));
List mapOverlays=mapView.getOverlays();
Drawable srcdawable=this.getResources().getDrawable(R.Drawable.pin_绿色);
CustomItemizedOverlay srcitemizedOverlay=新的CustomItemizedOverlay(srcdrawable);
//CustomItemizedOverlay srciitemizedoverlay=新的CustomItemizedOverlay(getDrawable(“com/agarwal/route/pin_green.png”);
OverlayItem srcoverlayitem=新的OverlayItem(srcGeoPoint,“您好!”,“这是您的位置”);
Drawable destdrawable=this.getResources().getDrawable(R.Drawable.pin_red);
CustomItemizedOverlay destinitemizedoverlay=新的CustomItemizedOverlay(destdravable);
//CustomItemizedOverlay destinitemizedoverlay=新的CustomItemizedOverlay(getDrawable(“com/agarwal/route/pin_red.png”);
OverlayItem destoverlayitem=新的OverlayItem(destGeoPoint,“你好!”,“这是dest位置”);
srcitemizedOverlay.addOverlay(srcoverlayitem);
desttemizedoverlay.addOverlay(destoverlayitem);
添加(srcitemizedOverlay);
mapOverlays.add(目标化覆盖);
connectAsyncTask_connectAsyncTask=新建connectAsyncTask();
_connectAsyncTask.execute();
mapView.SetBuilTinZoomControl(真);
mapView.DisplayZoomControl(真);
mOverlays=mapView.getOverlays();
mapView.getController().animateTo(srcGeoPoint);
mapView.getController().setZoom(12);
//SharedData=SharedData.getInstance();
数据。setAPIKEY(“APIKEY”);
数据集(17);
数据集:液化天然气(78);
数据集(18);
数据。设置目的地液化天然气(77);
startActivity(新意图(RoutePath.this,RoutePath.class));
}
}
@凌驾
受保护的布尔值isRouteDisplayed(){
//TODO自动生成的方法存根
返回false;
}
私有类connectAsyncTask扩展了AsyncTask{
私有进程对话;
@凌驾
受保护的void onPreExecute(){
//TODO自动生成的方法存根
super.onPreExecute();
progressDialog=新建progressDialog(_活动);
setMessage(“正在提取路径,请稍候…”);
progressDialog.setUndeterminate(true);
progressDialog.show();
}
@凌驾
受保护的Void doInBackground(Void…参数){
//TODO自动生成的方法存根
fetchData();
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
//TODO自动生成的方法存根
super.onPostExecute(结果);
如果(doc!=null){
叠加ol=新的MyOverlay(_活动,srcGeoPoint,srcGeoPoint,1);
添加(ol);
NodeList _NodeList=doc.getElementsByTagName(“状态”);
Node node1=_nodelist.item(0);
字符串_status1=node1.getChildNodes().item(0.getNodeValue();
如果(_status1.equalsIgnoreCase(“OK”)){
NodeList_NodeList_path=doc.getElementsByTagName(“概述”多段线);
节点路径=\u节点列表路径。项(0);
元素状态路径=(元素)节点路径;
NodeList\u NodeList\u destination\u path=\u status\u path.getElementsByTagName(“点”);
Node\u nodelist\u dest=\u nodelist\u destination\u path.item(0);
字符串_path=_nodelist_dest.getChildNodes().item(0.getNodeValue();
列表_地质点=解码多边形(_路径);
地质点gp1;
地质点gp2;
gp2=_地质点。获取(0);
Log.d(“_geopoints”,”:“+_geopoints.size());
对于(inti=1;i>1):(结果
<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" >
<com.google.android.maps.MapView
android:id="@+id/mapid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="APIKEY"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"
tools:context=".RoutePath" />
</RelativeLayout>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.path"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="com.google.android.maps"/>
<activity
android:name=".RoutePath"
android:label="@string/title_activity_path" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>