问题
I´m designing an android app that locate places, these places are in a database in a server but I only have the name and location of the place, so I need to locate it in my app and put a marker there. It's posible to get coordinates only with address or, Do I need to redo my database adding fields with latitude and longitude?
回答1:
You can use the Geocoder class to do a look-up of the addresses you have, and then populate the map with Markers using the LanLng
objects that are returned.
Note that the Geocoder
class will not be able to geocode every address, but it will be successful for most of them if they are in the correct format.
Taking code from this question as a guide, I just got this simple example working.
I created a custom class that stores location name, location address, and a LatLng
object to store the lat/lon.
For this simple example, I just used three addresses.
Here is the full class code:
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class MapsActivity extends AppCompatActivity {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.setMyLocationEnabled(true);
List<CustomLocation> custLocs = new ArrayList<CustomLocation>();
//Testing with three addresses
custLocs.add(new CustomLocation("location 1", "100 market street san francisco ca"));
custLocs.add(new CustomLocation("location 2", "200 market street san francisco ca"));
custLocs.add(new CustomLocation("location 3", "300 market street san francisco ca"));
//set the location for each item in the list
for (CustomLocation custLoc : custLocs){
custLoc.setLocation(getSingleLocationFromAddress(custLoc.address));
}
//draw the Marker for each item in the list
for (CustomLocation custLoc : custLocs){
mMap.addMarker(new MarkerOptions().position(custLoc.latLng)
.title(custLoc.name).icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
}
}
//method to do a lookup on the address
public LatLng getSingleLocationFromAddress(String strAddress)
{
Geocoder coder = new Geocoder(this, Locale.getDefault());
List<Address> address = null;
Address location = null;
LatLng temp = null;
String strAddresNew = strAddress.replace(",", " ");
try
{
address = coder.getFromLocationName(strAddresNew, 1);
if (!address.isEmpty())
{
location = address.get(0);
location.getLatitude();
location.getLongitude();
temp = new LatLng(location.getLatitude(), location.getLongitude());
Log.d("Latlng : ", temp + "");
}
} catch (IOException e)
{
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (Exception e)
{
e.printStackTrace();
}
return temp;
}
//class to hold the name and address and location
public static class CustomLocation{
public String name;
public String address;
public LatLng latLng;
public CustomLocation(String n, String a){
name = n;
address = a;
}
public void setLocation(LatLng ll){
latLng = ll;
}
}
}
Result:
回答2:
You can make a request to google maps API to get possible addresses by a address string. Check this link.
来源:https://stackoverflow.com/questions/30740306/how-to-get-geographical-coordinates-in-google-maps-api-v2-android-using-an-strin