我正在设计一个定位地点的Android应用程序,这些地方位于服务器的数据库中,但我只有地点的名称和位置,所以我需要在我的应用程序中找到它并放置标记那里。仅使用地址获取坐标是否可行,或者我是否需要重新添加具有纬度和经度的数据库?
答案 0 :(得分:2)
您可以使用Geocoder类查找您拥有的地址,然后使用返回的LanLng
对象使用Markers填充地图。
请注意,Geocoder
类无法对每个地址进行地理编码,但如果格式正确,大多数地址都会成功。
以this question的代码为指导,我只是将这个简单的例子运用起来。
我创建了一个自定义类,用于存储位置名称,位置地址和用于存储纬度/经度的LatLng
对象。
对于这个简单的例子,我只使用了三个地址。
以下是完整的类代码:
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;
}
}
}
结果:
答案 1 :(得分:0)
您可以向Google Maps API发出请求,以通过地址字符串获取可能的地址。检查此link。
答案 2 :(得分:0)
您可以使用Geocoder类,该类将以指定格式返回给定地址的纬度和经度,但是地址名称必须遵循指定格式。 从以下链接中查看有关此内容的更多信息:- https://developer.android.com/reference/android/location/Geocoder