Sunday, October 27, 2019

Getting Issue drawing Line in Map between Two locations in 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;
}


Layout






    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 Details



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



Updated With getDirection Method code



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 );
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;
}

}

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...