android编程如何获取指针位置,在Android SDK中的两个位置之间的地图中获取...

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;

}

布局

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello" />

android:id="@+id/mapview"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:apiKey="034BXAeVNYe1Ob063cZUpablCB_0y7xjEGD3RhQ"

/>

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方法代码更新

打包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;

?????@覆盖

?????公共无效onCreate(捆绑保存的InstanceState){

??????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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值