如何从500多个地址获取纬度经度? [英] How to get Latitude Longitude from more than 500 addresses?
问题描述
我想将它们绘制成区域明智的。
我已经实现了这个代码:
$ $ p $ $ $ $ $ c $ public $ Lat $ getSingleLocationFromAddress(String strAddress)
{
Geocoder coder = new Geocoder(this,Locale.getDefault());
列表<地址> address = null;
地址位置= null;
GeoPoint p1 = null;
LatLng temp = null; // new LatLng(26.0000,121.0000);
String strAddresNew = strAddress.replace(,,);
尝试
{
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 +);
} else
{
arrTemp.add(strAddress);
System.out.println(arrTemp);
}
} catch(IOException e)
{
Toast.makeText(mContext,e.toString(),Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch(Exception e)
{
e.printStackTrace();
}
return temp;
}
但问题是当我像这样循环调用这个方法时:
Marker TP1 = googleMap.addMarker(new MarkerOptions()。position(getSingleLocationFromAddress(fullAddress)));
coder.getFromLocationName(strAddresNew,1);
为某些地址返回null,例如,如果我有一个 ArrayList
的7个地址,那么我只能得到4-5个LatLong值。
使用以下代码行......它可能对您有所帮助
在您的 Oncreate 或 OncreatView 上声明这些变量
List< ;地址> From_geocode = null;
私人双人sLat,sLong;
私人Geocoder地理编码器;在您的 oncreate 或 oncreateVIew 内使用
这些代码行
geocoder = new Geocoder(getActivity());
尝试{
From_geocode = geocoder.getFromLocationName(PUT THE ADDRESS HERE,1);
} catch(IOException e1){
// TODO自动生成的catch块
e1.printStackTrace(); (From_geocode.size()> 0){
System.out.println(2);
}
if
From_geocode.get(0).getLatitude(); //获取
//纬度
From_geocode.get(0).getLongitude();
sLat = From_geocode.get(0).getLatitude();
sLong = From_geocode.get(0).getLongitude();
System.out.println(LATITUTE =====+ sLat +LONGITUTE =====+ sLong);
$ b $ / code>
然后在清单 file
< uses-permission android:name =android.permission.ACCESS_COARSE_LOCATION/>
< uses-permission android:name =android.permission.ACCESS_FINE_LOCATION/>
< uses-permission android:name =android.permission.INTERNET/>
I have more than 700 addresses in database. I want to plot them region wise. I have implement this code :
public LatLng getSingleLocationFromAddress(String strAddress)
{
Geocoder coder = new Geocoder(this, Locale.getDefault());
List<Address> address = null;
Address location = null;
GeoPoint p1 = null;
LatLng temp = null;//new LatLng(26.0000, 121.0000);
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 + "");
} else
{
arrTemp.add(strAddress);
System.out.println(arrTemp);
}
} catch (IOException e)
{
Toast.makeText(mContext, e.toString(), Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (Exception e)
{
e.printStackTrace();
}
return temp;
}
But the problem is when I call this method in loop like this:
Marker TP1 = googleMap.addMarker(new MarkerOptions().position(getSingleLocationFromAddress(fullAddress)));
The line coder.getFromLocationName(strAddresNew, 1);
returns null for some addresses, for example if I have an ArrayList
of 7 addresses then I get only 4-5 LatLong values.
Use below lines of code...It can be helpful for you
Declare these variables above yours Oncreate or OncreatView
List<Address> From_geocode = null;
private double sLat, sLong;
private Geocoder geocoder;
Than inside yours oncreate or oncreateVIew use these lines of code
geocoder = new Geocoder(getActivity());
try {
From_geocode = geocoder.getFromLocationName("PUT THE ADDRESS HERE", 1);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (From_geocode.size() > 0) {
System.out.println("2");
From_geocode.get(0).getLatitude(); // getting
// latitude
From_geocode.get(0).getLongitude();
sLat = From_geocode.get(0).getLatitude();
sLong = From_geocode.get(0).getLongitude();
System.out.println("LATITUTE====="+sLat+" LONGITUTE====="+sLong);
}
And provide the entry inside the manifest file
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
这篇关于如何从500多个地址获取纬度经度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!