【问题标题】:User location sharing用户位置分享
【发布时间】:2019-03-03 16:58:06
【问题描述】:

我正在开发一个 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);
    }

【问题讨论】:

    标签: android location sharing


    【解决方案1】:

    位置未更新,因此默认初始化为 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;
    

    【讨论】:

    • 做了所有这些,仍然得到同样的错误。还有其他解决方案吗?还是谢谢
    • 你有没有尝试把代码放在```` String uri = "maps.google.com/maps?daddr=" +latitude+","+longitude; ```` ?
    • 你明白了!!更改了 locationmanager 对象,因为我认为它与其他 locationmanager 对象发生冲突并重新检查了权限。非常感谢:)
    猜你喜欢
    • 2013-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-27
    • 1970-01-01
    相关资源
    最近更新 更多