我是 android 新手,我正在使用与 LocationListener 和谷歌地图 API v2 关联的位置管理器,我试图获取用户当前位置与另一个位置之间的距离,但我总是在 map.getMyLocation() 上得到空指针.
这是我的代码:
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());