我正在寻找一种方法来显示Android应用程序内的位置。我有存储在MySQL数据库中的地方的经度和纬度坐标,现在我想检索它们(经度和纬度)并显示应用程序中的确切位置。谢谢
我假设你想要一个带标记的地图视图。
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="200dp"
class="com.google.android.gms.maps.MapFragment" />
这应该添加到您希望Map所在的活动的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);
}
我认为应该给你你想要的东西。如果您还需要其他任何评论。
要使用纬度和经度获取位置详细信息,请使用以下代码
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
//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);
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));