问题
I am looking for a way to display a location of a place inside an android application. I have the longitude and Latitude coordinates of the place stored inside a MySQL database, now I want to retrieve them (Longitude and latitude)and display the exact location in the application. thanks
回答1:
I am assuming you want it on a map view with a marker.
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="200dp"
class="com.google.android.gms.maps.MapFragment" />
This should be added to you xml file of the activity you want the Map to be in. Then in your activity,
GoogleMap googleMap;
MapFragment mapFragment;
Initialize this in you onCreate;
mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
and then create the override method, 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);
}
I think that should give you what you want. Comment if you need anything else.
回答2:
To get the location details using latitude and longitude please use the below code
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));
来源:https://stackoverflow.com/questions/41630786/how-to-display-a-location-of-a-place-in-android-app