获取LatLng错误以获取方向?

时间:2015-02-20 07:52:51

标签: java android google-maps-markers geocoding direction

我尝试获取当前位置的方向以及我喜欢的地方 我使用了this 教程我有两个按钮标记和方向当我点击标记按钮我的应用程序将粉碎

我的主要Java类

public class MainActivity extends Activity {
EditText addressEditText, finalAddressEditText;
LatLng addressPos, finalAddressPos;
Marker addressMarker;
private GoogleMap googleMap;
//static final LatLng pos = new LatLng(40, -79);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    addressEditText = (EditText) findViewById(R.id.startAddresseditText);
    finalAddressEditText = (EditText) findViewById(R.id.finalAddresseditText);

    try {
        if (googleMap==null){
            googleMap =((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
        }
        googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        googleMap.setMyLocationEnabled(true);
        googleMap.setTrafficEnabled(true);
        googleMap.setBuildingsEnabled(true);
        googleMap.getUiSettings().setZoomControlsEnabled(true);
       // Marker marker = googleMap.addMarker(new MarkerOptions().position(pos).title("Hamel"));
    }catch (Exception e){
        e.printStackTrace();
    }
}
public void showAddressMarker(View view) {
    String newAddress = addressEditText.getText().toString();

    if (newAddress!=null) {
        new PlaceAMarker().execute(newAddress);
    }
}
public void getDirection(View view) {
    String startingAddress = addressEditText.getText().toString();
    String finalAddress = finalAddressEditText.getText().toString();
    if ((startingAddress.equals("")) || (finalAddress.equals(""))) {
        Toast.makeText(this,
                "Enter a Starting & Ending Address", Toast.LENGTH_SHORT).show();
    } else {
        new GetDirection().execute(startingAddress, finalAddress);
    }
}
class GetDirection extends AsyncTask<String,String,String>{

    @Override
    protected String doInBackground(String... params) {
        String startAddress = params[0];
        startAddress = startAddress.replaceAll(" ","%20");
        getLatLng(startAddress, false);
        String endingAddress = params[1];
        endingAddress = endingAddress.replaceAll(" ","%20");
        getLatLng(endingAddress,true);
        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        String geoUriString ="http://maps.google.com/maps?addr="+
                addressPos.latitude +","+
                addressPos.longitude+"&daddr="+
                finalAddressPos.latitude+","+
                finalAddressPos.longitude;

        Intent mapcall = new Intent(Intent.ACTION_VIEW, Uri.parse(geoUriString));
        startActivity(mapcall);
    }
}
class PlaceAMarker extends AsyncTask<String,String,String>{

    @Override
    protected String doInBackground(String... params) {
        String startAddress = params[0];
        startAddress = startAddress.replaceAll(" ","%20");

        getLatLng(startAddress,false);
        return null;
    }

    @Override
    protected void onPostExecute(String s) {

        addressMarker = googleMap.addMarker(new MarkerOptions()
                .position(addressPos).title("Address"));
        super.onPostExecute(s);
    }
}

protected void getLatLng(String address, boolean setDestination){
    String uri = "http://maps.google.com/maps/api/geocode/json?address="+
            address +"&sensor=false";
    HttpGet httpGet= new HttpGet(uri);
    HttpClient client = new DefaultHttpClient();
    HttpResponse response;

    StringBuilder stringBuilder = new StringBuilder();
    try {
        response = client.execute(httpGet);
        HttpEntity entity = response.getEntity();
        InputStream stream = entity.getContent();
        int byteData;
        while ((byteData = stream.read()) != -1) {
            stringBuilder.append((char)byteData);
        }
    } catch (ClientProtocolException e){
        e.printStackTrace();
    }catch (IOException e) {
        e.printStackTrace();
    }

    double lat =0.0, lng=0.0;
    JSONObject jsonObject;

    try {
        jsonObject = new JSONObject(stringBuilder.toString());

        lng = ((JSONArray)jsonObject.get("result")).getJSONObject(0)
                .getJSONObject("geometry").getJSONObject("location")
                .getDouble("lng");

        lat = ((JSONArray)jsonObject.get("result")).getJSONObject(0)
                .getJSONObject("geometry").getJSONObject("location")
                .getDouble("lat");
        if (setDestination) {
            finalAddressPos = new LatLng(lat, lng);
        } else {
            addressPos = new LatLng(lat, lng);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

}

LogCat错误

02-20 13:29:43.578  11232-11232/googlemap.com E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalArgumentException: no position in marker options
        at com.google.k.a.cl.a(Unknown Source)
        at com.google.maps.api.android.lib6.c.al.a(Unknown Source)
        at com.google.maps.api.android.lib6.c.el.a(Unknown Source)
        at com.google.android.gms.maps.internal.l.onTransact(SourceFile:167)
        at android.os.Binder.transact(Binder.java)
        at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.addMarker(Unknown Source)
        at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source)
        at googlemap.com.MainActivity$PlaceAMarker.onPostExecute(MainActivity.java:122)
        at googlemap.com.MainActivity$PlaceAMarker.onPostExecute(MainActivity.java:108)
        at android.os.AsyncTask.finish(AsyncTask.java)
        at android.os.AsyncTask.access$600(AsyncTask.java)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java)
        at android.os.Handler.dispatchMessage(Handler.java)
        at android.os.Looper.loop(Looper.java)
        at android.app.ActivityThread.main(ActivityThread.java)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
        at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案