我正在开发一个Android应用程序,它有一个名为用户位置共享的模块。尽管我能够将该位置作为消息共享,但它实际上并未显示任何坐标,即纬度或经度。救命!!谢谢我提前。
每当我尝试分享位置作为消息我得到“http://maps.google.com/maps?daddr=0.0.0.0”
使用位置管理器。
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng latlng = new LatLng(latitude,longitude);
Geocoder geocoder = new Geocoder(getApplicationContext());
try {
List<Address> addressList = geocoder.getFromLocation(latitude,longitude,1);
String str = addressList.get(0).getLocality()+",";
str += addressList.get(0).getCountryName();
mMap.addMarker(new MarkerOptions().position(latlng).title(str));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng,10.2f));
} catch (IOException e) {
e.printStackTrace();
}
}
我在工具栏中使用菜单选项格式进行共享
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.item1:
Toast.makeText(this, "Sharing Location", Toast.LENGTH_SHORT).show();
String uri = "http://maps.google.com/maps?daddr=" +latitude+","+longitude;
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT,uri);
startActivity(Intent.createChooser(share,"Share Location Via"));
return true;
}
return super.onOptionsItemSelected(item);
}
该位置未更新,因此默认情况下初始化为0.0.0.0。
所以你必须得到最后的位置: 添加这些行
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
之前
String uri = "http://maps.google.com/maps?daddr=" +latitude+","+longitude;