这条线总是返回零,即使我已经改变了我在我的代码上的地方!
double lat= 0.0, lng= 0.0;
Geocoder geoCoder = new Geocoder(getApplicationContext(), Locale.ENGLISH);
try
{
List<Address> addresses = geoCoder.getFromLocationName("Mumbai" , 5);
Toast.makeText(MainActivity.this,""+addresses.size() , 5000).show();
if (addresses.size() >= 0)
{
GeoPoint p = new GeoPoint(
(int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude() * 1E6));
Log.d("Latitude is:", ""+lat);
Log.d("Longitude is:", ""+lng);
Toast.makeText(MainActivity.this,""+lat , 5000).show();
}
}
catch(Exception e)
{
e.printStackTrace();
}
两个吐司只返回零。 这是我的清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.wptrafficanalyzer.locationplacesautocomplete"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<permission
android:name="in.wptrafficanalyzer.locationplacesautocomplete.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="in.wptrafficanalyzer.locationplacesautocomplete.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCW_PkOeeRcL92tq5jPiSwewo6iaNwD43c" />
</application>
</manifest>
即使没有给出正确的答案,我已经把所有必要的代码都放了
答案 0 :(得分:1)
您应该更新变量“lat”和“lng”,或者使用您的GeoPoint摆脱它们,如下所示:
Geocoder geoCoder = new Geocoder(getApplicationContext(), Locale.ENGLISH);
try {
List<Address> addresses = geoCoder.getFromLocationName("Mumbai" , 5);
Toast.makeText(MainActivity.this,""+addresses.size() , Toast.SHORT).show();
if (addresses.size() >= 0) {
GeoPoint p = new GeoPoint(
(int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude() * 1E6));
Log.d("Latitude is:", ""+ p.getLatitude());
Log.d("Longitude is:", ""+ p.getLongitude());
Toast.makeText(MainActivity.this,""+p.getLatitude(), Toast.SHORT).show();
}
...