gpt4 book ai didi

android - 如何使用字符串地址在 google maps API V2 android 中获取地理坐标?

转载 作者:行者123 更新时间:2023-11-29 20:43:05 26 4
gpt4 key购买 nike

我正在设计一个定位地点的 Android 应用程序,这些地点位于服务器的数据库中,但我只有地点的名称和位置,所以我需要在我的应用程序中找到它并放置一个 标记在那里。仅通过地址获取坐标是可能的,或者,我是否需要重做我的数据库,添加带有纬度和经度的字段?

最佳答案

您可以使用 Geocoder类查找您拥有的地址,然后使用返回的 LanLng 对象用标记填充 map 。

请注意,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;
}
}
}

结果:

enter image description here

关于android - 如何使用字符串地址在 google maps API V2 android 中获取地理坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30740306/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com