在Android中的谷歌地图上显示当前位置和其他位置

时间:2013-04-04 20:39:46

标签: android google-maps android-intent google-maps-api-3 gps

我的代码是单独的,但是当我整合时会崩溃!

这部分工作,如果我硬编码当前的值

Intent is = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://maps.googleapis.com/maps/api/staticmapcenter="+current+"&zoom=11&size=600000x400000&maptype=roadmap&markers=color:blue%7Clabel:A%7C49.264081,-123.170326&markers=color:green%7Clabel:B%7C49.227741,-122.997605&markers=color:yellow%7Clabel:C%7C49.322642,-123.099007&markers=color:purple%7Clabel:E%7C49.267501,-123.000693&markers=color:red%7Ccolor:red%7Clabel:D%7C49.193767,-123.083017&sensor=false"));
startActivity(is);

此部分有效并提供当前位置

 googlemap.setMyLocationEnabled(true);
             LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
             String provider = lm.getBestProvider(new Criteria(), true);
             Log.v(provider, "this is provider");
             if (provider == null){
                 onProviderDisabled(provider);
             }
             Location loc = lm.getLastKnownLocation(provider);
             Log.v(loc.toString(),"this is loc");
             if(loc != null) {
                 onLocationChanged(loc);

但是这一个是集成,崩溃

     googlemap.setMyLocationEnabled(true);
     LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
     String provider = lm.getBestProvider(new Criteria(), true);
     Log.v(provider, "this is provider");
     if (provider == null){
         onProviderDisabled(provider);
     }
     Location loc = lm.getLastKnownLocation(provider);
     Log.v(loc.toString(),"this is loc");
     if(loc != null) {
         onLocationChanged(loc);
         String current= loc.toString();
                Intent is = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.googleapis.com/maps/api/staticmap?center="+current+"&zoom=11&size=600000x400000&maptype=roadmap&markers=color:blue%7Clabel:A%7C49.264081,-123.170326&markers=color:green%7Clabel:B%7C49.227741,-122.997605&markers=color:yellow%7Clabel:C%7C49.322642,-123.099007&markers=color:purple%7Clabel:E%7C49.267501,-123.000693&markers=color:red%7Ccolor:red%7Clabel:D%7C49.193767,-123.083017&sensor=false"));
                startActivity(is);

logcat的: 04-04 20:58:34.765:W / dalvikvm(1082):threadid = 11:线程退出未捕获异常(组= 0x40a71930) 04-04 20:58:34.805:E / AndroidRuntime(1082):致命异常:AsyncTask#1 04-04 20:58:34.805:E / AndroidRuntime(1082):java.lang.RuntimeException:执行doInBackground()时发生错误 04-04 20:58:34.805:E / AndroidRuntime(1082):在android.os.AsyncTask $ 3.done(AsyncTask.java:299) 04-04 20:58:34.805:E / AndroidRuntime(1082):at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 04-04 20:58:34.805:E / AndroidRuntime(1082):at java.util.concurrent.FutureTask.setException(FutureTask.java:219) 04-04 20:58:34.805:E / AndroidRuntime(1082):at java.util.concurrent.FutureTask.run(FutureTask.java:239) 04-04 20:58:34.805:E / AndroidRuntime(1082):在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:230) 04-04 20:58:34.805:E / AndroidRuntime(1082):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 04-04 20:58:34.805:E / AndroidRuntime(1082):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:573) 04-04 20:58:34.805:E / AndroidRuntime(1082):at java.lang.Thread.run(Thread.java:856) 04-04 20:58:34.805:E / AndroidRuntime(1082):引起:java.lang.NullPointerException 04-04 20:58:34.805:E / AndroidRuntime(1082):at com.example.distancemap.MainActivity.ShowMap(MainActivity.java:94) 04-04 20:58:34.805:E / AndroidRuntime(1082):at com.example.distancemap.MainActivity $ TestGoogleMaps.doInBackground(MainActivity.java:67) 04-04 20:58:34.805:E / AndroidRuntime(1082):at com.example.distancemap.MainActivity $ TestGoogleMaps.doInBackground(MainActivity.java:1) 04-04 20:58:34.805:E / AndroidRuntime(1082):在android.os.AsyncTask $ 2.call(AsyncTask.java:287) 04-04 20:58:34.805:E / AndroidRuntime(1082):at java.util.concurrent.FutureTask.run(FutureTask.java:234) 04-04 20:58:34.805:E / AndroidRuntime(1082):... 4更多

1 个答案:

答案 0 :(得分:0)

问题很明显。你的代码中有NPE:

E/AndroidRuntime(1082): Caused by: java.lang.NullPointerException 04-04 
20:58:34.805: E/AndroidRuntime(1082): at 
com.example.distancemap.MainActivity.ShowMap(MainActivity.java:94)

猜猜这是你的第94行:

Log.v(loc.toString(),"this is loc");