【问题标题】:How to display Latitude and Longitude in a Map View (Android)? [duplicate]如何在地图视图(Android)中显示纬度和经度? [复制]
【发布时间】:2021-02-23 06:14:14
【问题描述】:

我将纬度和经度存储在数据库中,但我如何在地图视图中显示它而不是每次都在浏览器上打开坐标。

我可以在原生 Android 应用程序中显示它而不是每次都重定向到谷歌地图吗?

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    您可以使用地图片段或地图活动在地图中显示地图 首先在xml中添加片段:

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/map"
    tools:context=".MapsActivity"
    android:name="com.google.android.gms.maps.SupportMapFragment" />
    

    第二次实现 OnMapReadyCallback 并覆盖 onMapReady 函数

        public class MapsActivity extends AppCompatActivity 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;
    
            // Add a marker in Sydney and move the camera
            LatLng sydney = new LatLng(-34, 151);
            mMap.addMarker(new MarkerOptions()
                    .position(sydney)
                    .title("Marker in Sydney"));
            mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
        }
    }
    

    android开发者网站here中描述的步骤

    请注意,要在您的应用程序中使用显示 Google 地图,您需要 API KEY,您可以在 Google Developer Console 中生成您的密钥,这是link

    【讨论】:

      【解决方案2】:

      在从 Firestore 中获得 lat long 之后,试试这个代码。

      MarkerOptions markerOptions = new MarkerOptions();
                          markerOptions.position(latlong);
                          markerOptions.title(latlong.latitude + " : " + latlong.longitude);
                          googleMap.clear();
                          googleMap.animateCamera(CameraUpdateFactory.newLatLng(latlong));
                          googleMap.addMarker(markerOptions);
      

      【讨论】:

      • 这里的 latlong 是什么?
      猜你喜欢
      • 1970-01-01
      • 2012-04-14
      • 1970-01-01
      • 1970-01-01
      • 2013-02-26
      • 2014-04-30
      • 2012-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多