Im trying to scrape a websites to get the lat, lng using Jsoup to place into gmaps but i get a stack trace of
FATAL EXCEPTION: main
Process: au.com.industryresponsetraining.map, PID: 18842
java.lang.NumberFormatException: Invalid double: ""
I have read it due to
you are parsing onCreate() value without putting any default value so the exception
except on my onCreate() is where my Jsoup is run. if I add something like
double myDouble;
String myString = ((EditText) findViewById(R.id.editText1)).getText().toString();
if (myString != null && !myString.equals("")) {
myDouble = Double.valueOf(myString);
} else {
myDouble = 0;
}
referenced from here my map ends out at sea.
My webpage has a valid lat, lng on it
here is my activity
package au.com.industryresponsetraining.map;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
String ln = "";
String lt = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
new Thread(new Runnable() {
@Override
public void run() {
try {
//get the Document object from the site. Enter the link of site you want to fetch
Document document = Jsoup.connect("http://www.industryresponsetraining.com.au/mdt/lat.html").get();
//Get the title of blog using title tag
//title = document.select("p").text().toString();
lt = document.select("h6").text().toString();
ln = document.select("h7").text().toString();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
double lats = Double.valueOf(lt);
double lngs = Double.valueOf(ln);
LatLng marker = new LatLng(lats, lngs);
mMap.addMarker(new MarkerOptions().position(marker).title("my Marker"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(marker));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 15));
}
}
The actual google map stuff has been referenced from the developers page