我是新來的android和我使用位置管理器與LocationListener和谷歌地圖API v2相關聯,我試圖得到用戶的當前位置和另一個位置之間的距離,但我總是在map.getMyLocation()上得到空指針。distanceTo(map.getMyLoctation())返回null
這裏是我的代碼:
LocationManager locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
MyLocationListener locListener = new MyLocationListener(this);
jager =(Button)findViewById(R.id.button1);
rotebuhlplatz =(Button)findViewById(R.id.button2);
rotebuhlst =(Button)findViewById(R.id.button3);
if(locListener.canGetLocation){
double mLat=locListener.getLatitude();
double mLong=locListener.getLongitude();
}else{
// can't get the location
}
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locListener);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
Marker jager = map.addMarker(new MarkerOptions().position(DHBWJager56)
.title("DHBW Jägerstraße 56")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
allMarkersMap.put(jager, Jager56.class);
map.setOnInfoWindowClickListener(this);
Marker jager2 = map.addMarker(new MarkerOptions().position(DHBWJager58)
.title("DHBW Jägerstraße 58")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
allMarkersMap.put(jager, Jager58.class);
map.setOnInfoWindowClickListener(this);
Marker rotebuhl = map.addMarker(new MarkerOptions()
.position(DHBWRotebuhl)
.title("DHBW Rotebühlplatz 41/1").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
allMarkersMap.put(rotebuhl, Rotebuhl.class);
map.setOnInfoWindowClickListener(this);
// .icon(BitmapDescriptorFactory
// .fromResource(R.drawable.ic_launcher)));
Marker rts = map.addMarker(new MarkerOptions().position(DHBWRotebuhlstrasse)
.title("DHBW Rotebühlstraße 131"));
allMarkersMap.put(rts, SocialWork.class);
map.setOnInfoWindowClickListener(this);
// Move the camera instantly to Jagerstrasse with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(DHBWRotebuhl, 17.0f));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(14), 2000, null);
map.setMyLocationEnabled(true);
Location l1=new Location("source");
l1.setLatitude(DHBWJager56.latitude);
l1.setLongitude(DHBWJager56.longitude);
float f=l1.distanceTo(map.getMyLocation());