简体   繁体   English

通过意图将地址传递到Google地图活动:

[英]Passing a address via intent to Google map activity:

I am working on an app that gets the address of an event the user selects. 我正在开发一个应用程序,它可以获取用户选择的事件的地址。 The address is a String and is passed from one activity to a Google Map activity. 地址是一个字符串,从一个活动传递到Google Map活动。 I know the string arrives in the map activity as I have used a Toast to display it. 我知道字符串到达​​地图活动,因为我使用Toast来显示它。 However, when I try to get the longitude and latitude of the address it doesn't work. 但是,当我尝试获取地址的经度和纬度时,它不起作用。 The try catch block doesn't seem to execute unless I hard code and address into the addresses = geocoder.getFromLocationName('Dublin,Ireland', 1); try catch块似乎没有执行,除非我硬编码并寻址到addresses = geocoder.getFromLocationName('Dublin,Ireland', 1);

Any help would be greatly appreciated. 任何帮助将不胜感激。

public class EventMapActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
private Intent MyTournamentsActivityIntent;
private String eventAddress = "";
double latitude = 0;
double longitude = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_event_map);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    MyTournamentsActivityIntent = getIntent();
    eventAddress = MyTournamentsActivityIntent.getStringExtra("event_location");
    Toast.makeText(getApplicationContext(),"Address from intent" + eventAddress,Toast.LENGTH_SHORT).show();
}//end onCreate


@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    Geocoder geocoder = new Geocoder(this);
    List<Address> addresses;

    try{

        /*
        This doesnt execute unless I hard code the eventAddress
         */
        addresses = geocoder.getFromLocationName(eventAddress, 1);
        latitude= addresses.get(0).getLatitude();
        longitude= addresses.get(0).getLongitude();
        Toast.makeText(getApplicationContext(),String.valueOf(latitude) + " " + String.valueOf(longitude) ,Toast.LENGTH_SHORT).show();
    }catch (Exception e)
    {
        e.printStackTrace();
    }

    LatLng eventLocation = new LatLng(latitude, longitude);
    mMap.addMarker(new MarkerOptions().position(eventLocation).title("Your Event"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(eventLocation));
}//end onMapReady

public void setEventAddress(String eventAddress) {
    this.eventAddress = eventAddress;
}

}//end Class } //结束类

Change this : 改变这个:

 eventAddress = MyTournamentsActivityIntent.getStringExtra("event_location");

To this: 对此:

 eventAddress = getIntent().getStringExtra("event_location");

And call mapFragment.getMapAsync(this); 并调用mapFragment.getMapAsync(this); :

 eventAddress = getIntent().getStringExtra("event_location");
 mapFragment.getMapAsync(this);

After you get the eventAddress 获得eventAddress后

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM