Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android中获取地图地址或位置地址_Java_Android_Google Maps - Fatal编程技术网

Java 在Android中获取地图地址或位置地址

Java 在Android中获取地图地址或位置地址,java,android,google-maps,Java,Android,Google Maps,我正在编写一个需要获取当前地图位置的应用程序。我的映射文件本身工作正常,但我需要从另一个活动获取地址(请参见下面按钮处的addressString)。我尝试了getAddress/setAddress(setters/getter)。它们不起作用。它们总是返回“无地址”(默认值) 这是我的密码 如何使其成为独立的Java类???或者从其他活动获取地址 非常感谢 这段代码自己工作 垫子 导入android.content.Context; 导入android.location.Address;

我正在编写一个需要获取当前地图位置的应用程序。我的映射文件本身工作正常,但我需要从另一个
活动
获取地址(请参见下面按钮处的
addressString
)。我尝试了
getAddress
/
setAddress
(setters/getter)。它们不起作用。它们总是返回“无地址”(默认值)

这是我的密码

如何使其成为独立的Java类???或者从其他活动获取地址

非常感谢

这段代码自己工作

垫子


导入android.content.Context;
导入android.location.Address;
导入android.location.Criteria;
导入android.location.Geocoder;
导入android.location.location;
导入android.location.LocationListener;
导入android.location.LocationManager;
导入android.os.Bundle;
导入android.widget.TextView;
导入java.io.IOException;
导入java.util.List;
导入java.util.Locale;
公共类GetMapAddress扩展了MapActivity{
地图控制器;
MyPositionOverlay;
地图控制器;
地质点p;
String addressString=“找不到地址”;
@凌驾
创建公共空间(捆绑冰柱){
超级冰柱;
setContentView(R.layout.map);
MapView myMapView=(MapView)findViewById(R.id.myMapView);
mapController=myMapView.getController();
//配置地图显示选项
myMapView.setSatellite(真);
myMapView.setStreetView(true);
//放大
mapController.setZoom(17);
myMapView.SetBuilTinZoomControl(真);
//添加MyPositionOverlay
positionOverlay=新的MyPositionOverlay();
List overlays=myMapView.getOverlays();
叠加。添加(位置叠加);
LocationManager LocationManager=(LocationManager)getSystemService(Context.LOCATION\u服务);
标准=新标准();
标准.设定准确度(标准.准确度/精细度);
标准。setAltitudeRequired(false);
标准。需要设置(假);
条件.setCostAllowed(true);
标准。设置功率要求(标准。功率低);
字符串提供程序=locationManager.getBestProvider(条件为true);
Location Location=locationManager.getLastKnownLocation(提供者);
updateWithNewLocation(位置);
locationManager.RequestLocationUpdate(提供者,2000,10,locationListener);
mc=myMapView.getController();
字符串坐标[]={
"1.352566007", "103.78921587"
};
double lat=double.parseDouble(坐标[0]);
double lng=double.parseDouble(坐标[1]);
p=新地质点((内部)(纬度*1E6),(内部)(液化天然气*1E6));
司马迁(p),;
mc.setZoom(17);
myMapView.invalidate();
}
私有最终位置Listener LocationListener=新位置Listener(){
已更改位置上的公共无效(位置){
updateWithNewLocation(位置);
}
公共无效onProviderDisabled(字符串提供程序){
updateWithNewLocation(空);
}
公共无效onProviderEnabled(字符串提供程序){
}
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){
}
};
/**使用新位置更新地图*/
私有void updateWithNewLocation(位置){
TextView myLocationText=(TextView)findViewById(R.id.myLocationText);
字符串latLongString;
如果(位置!=null){
//更新我的位置标记
位置叠加。设置位置(位置);
//更新地图位置。
双geoLat=location.getLatitude()*1E6;
Double geoling=location.getLongitude()*1E6;
地质点=新的地质点(geoLat.intValue(),geoling.intValue());
mapController.animateTo(点);
双纬度=location.getLatitude();
double lng=location.getLongitude();
LATLONSTRING=“Lat:+Lat+”\nLong:+lng;
Geocoder gc=新的Geocoder(这个,Locale.getDefault());
试一试{
列表地址=gc.getFromLocation(lat,lng,1);
StringBuilder sb=新的StringBuilder();
如果(地址.size()>0){
地址=地址。获取(0);
对于(int i=0;i
将地址传递给
意图中的第二个
活动
,或者如果您需要从第二个
活动
进行查找,只需从那里呼叫
地理编码器


如果您正以某种方式从第二个
活动
实例化上述类(
GetMapActivity
),并调用
getAddress
方法,那么这将不起作用。

以下操作可能会起作用[将代码从onCreate移到方法中]

public String getAddressString() {
        if (addressString.equals("default") {
          LocationManager lm = (LocationManager)act.getSystemService(Context.LOCATION_SERVICE);
          Criteria crit = new Criteria();
          crit.setAccuracy(Criteria.ACCURACY_FINE);
          String provider = lm.getBestProvider(crit, true);
          updateWithNewLocation(lm.getLastKnownLocation(provider));              
        }
        return addressString;
    }

我在我的应用程序a中使用了以下代码
public String getAddressString() {
        if (addressString.equals("default") {
          LocationManager lm = (LocationManager)act.getSystemService(Context.LOCATION_SERVICE);
          Criteria crit = new Criteria();
          crit.setAccuracy(Criteria.ACCURACY_FINE);
          String provider = lm.getBestProvider(crit, true);
          updateWithNewLocation(lm.getLastKnownLocation(provider));              
        }
        return addressString;
    }
    //
    //  Write the location name.
    //

    try {

        Geocoder geo = new Geocoder(youractivityclassname.this.getApplicationContext(), Locale.getDefault());
        List<Address> addresses = geo.getFromLocation(latitude, longitude, 1);
        if (addresses.isEmpty()) {
            yourtextfieldname.setText("Waiting for Location");
        }
        else {
            if (addresses.size() > 0) {
                yourtextfieldname.setText(addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() +", " + addresses.get(0).getAdminArea() + ", " + addresses.get(0).getCountryName());
                //Toast.makeText(getApplicationContext(), "Address:- " + addresses.get(0).getFeatureName() + addresses.get(0).getAdminArea() + addresses.get(0).getLocality(), Toast.LENGTH_LONG).show();
            }
        }
    }
    catch (Exception e) {
        e.printStackTrace(); // getFromLocation() may sometimes fail
    }