package com.hands;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import android.os.Bundle;
import android.util.Log;
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 DrawlineActivity extends MapActivity {
MapView myMapView = null;
MapController myMC = null;
GeoPoint geoPoint = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myMapView = (MapView) findViewById(R.id.mapview);
geoPoint = null;
myMapView.setSatellite(false);
double fromLat = 12.303534;
double fromLong = 76.64611;
double toLat = 12.9715987;
double toLong = 77.5945627;
String sourceLat = Double.toString(fromLat);
String sourceLong = Double.toString(fromLong);
String destinationLat = Double.toString(toLat);
String destinationLong = Double.toString(toLong);
String pairs[] = getDirectionData(sourceLat,sourceLong, destinationLat, destinationLong );
//The above line pairs[] retrieving null value. Showing as NULL POINTER EXCEPTION
String[] lngLat = pairs[0].split(",");
// STARTING POINT
GeoPoint startGP = new GeoPoint((int) (Double.parseDouble(lngLat[1]) * 1E6), (int) (Double.parseDouble(lngLat[0]) * 1E6));
myMC = myMapView.getController();
geoPoint = startGP;
myMC.setCenter(geoPoint);
myMC.setZoom(10);
myMapView.getOverlays().add(new DirectionPathOverlay(startGP, startGP));
// NAVIGATE THE PATH
GeoPoint gp1;
GeoPoint gp2 = startGP;
for (int i = 1; i < pairs.length; i++) {
lngLat = pairs[i].split(",");
gp1 = gp2;
// watch out! For GeoPoint, first:latitude, second:longitude
gp2 = new GeoPoint((int) (Double.parseDouble(lngLat[1]) * 1E6),(int) (Double.parseDouble(lngLat[0]) * 1E6));
myMapView.getOverlays().add(new DirectionPathOverlay(gp1, gp2));
Log.d("xxx", "pair:" + pairs[i]);
}
// END POINT
myMapView.getOverlays().add(new DirectionPathOverlay(gp2, gp2));
myMapView.getController().animateTo(startGP);
myMapView.setBuiltInZoomControls(true);
myMapView.displayZoomControls(true);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
布局
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="034BXAeVNYe1Ob063cZUpablCB_0y7xjEGD3RhQ"
/>
null
Logcat详细信息
08-10 09:03:20.216: W/dalvikvm(305): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-10 09:03:20.246: E/AndroidRuntime(305): FATAL EXCEPTION: main
08-10 09:03:20.246: E/AndroidRuntime(305): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hands/com.hands.DrawlineActivity}: java.lang.NullPointerException
08-10 09:03:20.246: E/AndroidRuntime(305): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-10 09:03:20.246: E/AndroidRuntime(305): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-10 09:03:20.246: E/AndroidRuntime(305): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-10 09:03:20.246: E/AndroidRuntime(305): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-10 09:03:20.246: E/AndroidRuntime(305): at android.os.Handler.dispatchMessage(Handler.java:99)
08-10 09:03:20.246: E/AndroidRuntime(305): at android.os.Looper.loop(Looper.java:123)
08-10 09:03:20.246: E/AndroidRuntime(305): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-10 09:03:20.246: E/AndroidRuntime(305): at java.lang.reflect.Method.invokeNative(Native Method)
08-10 09:03:20.246: E/AndroidRuntime(305): at java.lang.reflect.Method.invoke(Method.java:521)
08-10 09:03:20.246: E/AndroidRuntime(305): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-10 09:03:20.246: E/AndroidRuntime(305): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-10 09:03:20.246: E/AndroidRuntime(305): at dalvik.system.NativeStart.main(Native Method)
08-10 09:03:20.246: E/AndroidRuntime(305): Caused by: java.lang.NullPointerException
08-10 09:03:20.246: E/AndroidRuntime(305): at com.hands.DrawlineActivity.getDirectionData(DrawlineActivity.java:111)
08-10 09:03:20.246: E/AndroidRuntime(305): at com.hands.DrawlineActivity.onCreate(DrawlineActivity.java:44)
08-10 09:03:20.246: E/AndroidRuntime(305): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-10 09:03:20.246: E/AndroidRuntime(305): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-10 09:03:20.246: E/AndroidRuntime(305): ... 11 more
使用getDirection方法代码更新
package com.hands;
导入java.net.HttpurlConnection;导入java.net.URL;
导入javax.xml.parsers.DocumentBuilder;导入javax.xml.parsers.DocumentBuilderFactory;
导入org.w3c.dom.document;导入org.w3c.dom.node;导入org.w3c.dom.nodeList;
导入Android.os.bundle;导入android.util.log;
导入com.google.android.maps.geopoint;导入com.google.android.maps.MapActivity;导入com.google.android.maps.MapController;导入com.google.android.maps.MapView;
公共类DrawlineActivity扩展MapActivity{MapView myMapView=Null;MapController myMC=Null;GeoPoint GeoPoint=Null;@override public void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myMapView = (MapView) findViewById(R.id.mapview);
geoPoint = null;
myMapView.setSatellite(false);
double fromLat = 12.303534;
double fromLong = 76.64611;
double toLat = 12.9715987;
double toLong = 77.5945627;
String sourceLat = Double.toString(fromLat);
String sourceLong = Double.toString(fromLong);
String destinationLat = Double.toString(toLat);
String destinationLong = Double.toString(toLong);
String pairs[] = getDirectionData(sourceLat,sourceLong, destinationLat, destinationLong );
String[] lngLat = pairs[0].split(",");
// STARTING POINT
GeoPoint startGP = new GeoPoint((int) (Double.parseDouble(lngLat[1]) * 1E6), (int) (Double.parseDouble(lngLat[0]) * 1E6));
myMC = myMapView.getController();
geoPoint = startGP;
myMC.setCenter(geoPoint);
myMC.setZoom(10);
myMapView.getOverlays().add(new DirectionPathOverlay(startGP, startGP));
// NAVIGATE THE PATH
GeoPoint gp1;
GeoPoint gp2 = startGP;
for (int i = 1; i < pairs.length; i++) {
lngLat = pairs[i].split(",");
gp1 = gp2;
// watch out! For GeoPoint, first:latitude, second:longitude
gp2 = new GeoPoint((int) (Double.parseDouble(lngLat[1]) * 1E6),(int) (Double.parseDouble(lngLat[0]) * 1E6));
myMapView.getOverlays().add(new DirectionPathOverlay(gp1, gp2));
Log.d("xxx", "pair:" + pairs[i]);
}
// END POINT
myMapView.getOverlays().add(new DirectionPathOverlay(gp2, gp2));
myMapView.getController().animateTo(startGP);
myMapView.setBuiltInZoomControls(true);
myMapView.displayZoomControls(true);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
private String[] getDirectionData(String sourceLat, String sourceLong, String destinationLat, String destinationLong) {
String urlString = "http://maps.google.com/maps?f=d&hl=en&" +"saddr="+sourceLat+","+sourceLong+"&daddr="+destinationLat+","+destinationLong + "&ie=UTF8&0&om=0&output=kml";
Log.d("URL", urlString);
Document doc = null;
HttpURLConnection urlConnection = null;
URL url = null;
String pathConent = "";
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 = db.parse(urlConnection.getInputStream());
} catch (Exception e) {
}
NodeList nl = doc.getElementsByTagName("LineString");
for (int s = 0; s < nl.getLength(); s++) {
Node rootNode = nl.item(s);
NodeList configItems = rootNode.getChildNodes();
for (int x = 0; x < configItems.getLength(); x++) {
Node lineStringNode = configItems.item(x);
NodeList path = lineStringNode.getChildNodes();
pathConent = path.item(0).getNodeValue();
}
}
String[] tempContent = pathConent.split(" ");
return tempContent;
}
}
正如logcat所解释的,当您调用方法getDirectionData()时,会得到一个NullPointerException。上面的代码中没有显示此方法。你能不能加上这个方法,这样我们就可以看到问题出在哪里了?
编辑:感谢您添加getDirectionData()方法。现在还不清楚问题出在哪里,但一个问题是,您有一个空的catch块--这将吞噬异常,然后继续处理代码。例如,如果行
doc = db.parse(urlConnection.getInputStream());
抛出异常或返回null,然后尝试从null对象获取NodeList-这将给出null指针异常。我建议在这个方法的开头放置一个断点,然后逐步执行它,在执行过程中检查变量。这将确定问题的来源。
但是这个示例将敲击位置作为源位置和目标位置。但是我需要给出lat/lang值&需要计算这两个位置的距离和时间值。我怎么做呢?谢谢。
问题内容: 我正在使用JMap Viwer在Java中使用OpenStreet Maps http://wiki.openstreetmap.org/wiki/JMapViewer我可以加载地图,一切正常,但是我不知道如何从纬度在两点之间绘制一条线和经度。 任何人都知道画这种线的功能吗? 谢谢。 问题答案: 该方法对于这个作品,但默默拒绝有少于三个顶点的多边形。对于两点之间的直线,只需重复最后一个
问题内容: 我正在用Matplotlib绘制两个子图,基本上如下: 我可以在这两个子图之间画线吗?我该怎么办? 问题答案: 在许多情况下,其他答案的解决方案不是最优的(因为它们只有在计算出点数后未对图进行任何更改的情况下才会起作用)。 更好的解决方案将使用特别设计的:
我试图实现当前位置地图应用程序,我正在加载地图碎片,但它需要纬度和经度值来定位地址...如何实现当前位置地图功能...提前感谢.. 这是我的地图片段。
我有http://www.codereye.com/2010/10/how-to-calculate-distance-between-2.html的这段代码,它根据两个IP地址对应的纬度和经度计算它们之间的大致物理距离。 lat和long是有符号值。但是,此代码有时会将dist返回为NaN。 我错过什么了吗?θ需要是绝对值吗? 更新:返回南的测试值:- lat1=-23.5477,lon1=-4
问题内容: 我有两个地图,如下所示: 我想获得与每个maps.ie不同的键 它显示了在sourceRecords中可用但在targetRecords中不可用的映射键。 它显示了targetRecords中可用的映射键,而sourceRecords中没有。 我做到了如下: 编辑 我能够找到通用键,但找不到不同的键。请帮忙。 问题答案: 集允许您也 删除 元素。 如果生成“ helper”集对您来说不