【问题标题】:Get lat-lng from google map intent从谷歌地图意图获取 lat-lng
【发布时间】:2016-04-04 23:52:43
【问题描述】:

我是安卓世界的新人。试图从谷歌地图获取当前坐标。我已经完成了一个示例应用程序,它以意图方式打开谷歌地图(来自 android 网站)。我想做的是从那个意图中得到 lat-lng。到目前为止我已经完成了-

   public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
       // mMap = googleMap;
        Uri uri = Uri.parse("geo:latitude,longitude?z=17");
        Intent intent = new Intent(android.content.Intent.ACTION_VIEW, uri);
        startActivity(intent);
    }
}

更新: 我知道我可以在android中获得当前坐标。 但我想从谷歌地图意图中获取坐标(当前显示标记位置)?

【问题讨论】:

  • 从起始意图获取 lat-lng 是什么意思.. 你想将 lat 和 long 设置为起始意图还是什么?
  • @bharat - 我已经更新了我的问题。
  • 我不认为您可以将 Google 地图应用程序标记/s 详细信息添加到您的应用程序中......

标签: android google-maps-android-api-2


【解决方案1】:

请看GoogleMap class documentation

公共最终位置 getMyLocation ()
...

返回当前 显示的用户位置,如果没有位置数据,则返回 null 可用。

返回

  • 当前显示的用户位置。

投掷

  • 如果未启用 my-location 层,则会出现 IllegalStateException。

然后它又说:

此方法已弃用。利用 com.google.android.gms.location.FusedLocationProviderApi 代替。

因此,如果您真的需要,可以使用getMyLocation(),但不建议这样做。它可能会返回 null。或者抛出异常。

代码将类似于:

    @Override
    public void onMapReady(GoogleMap googleMap) {
        // mMap = googleMap;
        Location myLocation;             

        try {
            myLocation = googleMap.getMyLocation();
        }
        catch (IllegalStateException e) {
            // Handle the exception.
        }

        if (!myLocation == null) {
            // Do something with the location if it's not null...
        }
        else {
            // Handle the null location.
        }

        Uri uri = Uri.parse("geo:latitude,longitude?z=17");
        Intent intent = new Intent(android.content.Intent.ACTION_VIEW, uri);
        startActivity(intent);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    相关资源
    最近更新 更多