【问题标题】:How to display a location of a place in android app如何在android应用程序中显示一个地方的位置
【发布时间】:2017-01-13 09:00:03
【问题描述】:

我正在寻找一种在 android 应用程序中显示某个地方的位置的方法。我将地点的经度和纬度坐标存储在 MySQL 数据库中,现在我想检索它们(经度和纬度)并在应用程序中显示确切位置。谢谢

【问题讨论】:

  • 你在安卓中使用谷歌地图吗,如果是的话给我们看看代码,也许我们可以帮忙..!
  • 您能否更具体地了解您需要如何显示确切位置,例如是否在谷歌地图中作为标记
  • 你想要它在地图视图中吗?还是只是细节?

标签: android google-maps location


【解决方案1】:

我假设您希望它在带有标记的地图视图上。

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    class="com.google.android.gms.maps.MapFragment" />

这应该添加到您希望地图所在的活动的 xml 文件中。 然后在你的活动中,

GoogleMap googleMap;
MapFragment mapFragment;

在你的 onCreate 中初始化它;

mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

然后创建覆盖方法,onMapReady()

@Override
public void onMapReady(GoogleMap gMap) {
    googleMap = gMap;
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    try {
        googleMap.setMyLocationEnabled(true);
    } catch (SecurityException se) {

    }

    //Edit the following as per you needs
    googleMap.setTrafficEnabled(true);
    googleMap.setIndoorEnabled(true);
    googleMap.setBuildingsEnabled(true);
    googleMap.getUiSettings().setZoomControlsEnabled(true);
    //

    LatLng placeLocation = new LatLng(***YOUR LAT***, ***YOUR LNG***); //Make them global
    Marker placeMarker = googleMap.addMarker(new MarkerOptions().position(placeLocation)
                  .title(***NAME OF PLACE HERE***));
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(placeLocation));
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(10), 1000, null);
}

我认为这应该给你你想要的。如果您需要其他任何内容,请发表评论。

【讨论】:

  • 格式良好的分步说明。这应该是公认的答案。
  • MapReady() 上有@override 方法吗??
  • 很好的解释希望我能接受这个答案
  • @ArnoldBrown YourClassName implements OnMapReadyCallback
【解决方案2】:

要使用纬度和经度获取位置详细信息,请使用以下代码

Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());

addresses = geocoder.getFromLocation(latitude, longitude, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5

String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
String knownName = addresses.get(0).getFeatureName(); // Only if available else return NULL

【讨论】:

    【解决方案3】:
                //Add mapView  activity.xml
                <com.google.android.maps.MapView
                            android:id="@+id/map_view"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:apiKey="your api key"
    
                            android:clickable="true"
                            android:layout_below="@id/tv_location" />
    
            //Add Activity
             // Getting reference to MapView
                    mapView = (MapView) findViewById(R.id.map_view);
                    googleMap = mapView.getMap();
    
        final LatLng latLng = new LatLng(lat, lng);
                final MarkerOptions markerOptions = new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.map_pin));
                final CameraPosition cameraPosition = new CameraPosition.Builder().target(latLng).zoom(14f).tilt(70).build();
                markerOptions.position(latLng);
     googleMap.addMarker(markerOptions);
    

    【讨论】:

      【解决方案4】:
          Geocoder coder = new Geocoder(this);
          String locationName = list.get(i);
          Geocoder gc = new Geocoder(this);
          List<Address> addressList = null;
          try {
              addressList = coder.getFromLocationName(locationName, 1);
          } catch (IOException e) {
              e.printStackTrace();
          }
          Address location = addressList.get(0);
          double latitude = location.getLatitude();
          double longitude = location.getLongitude();
          LatLng helpcenter = new LatLng(latitude, longitude);
          googleMap.addMarker(new MarkerOptions().position(helpcenter)
                  .title(list.get(i)));
          mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(helpcenter, 6f));
      

      【讨论】:

      • 在这段代码中,您可以使用地名找到任何地方。这里的 list 是一个列表视图,list.get(i) 是具体的地名
      猜你喜欢
      • 1970-01-01
      • 2016-11-08
      • 1970-01-01
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-21
      相关资源
      最近更新 更多