如何在按钮点击时将纬度和经度传递给Google Maps API?

时间:2018-03-15 12:29:45

标签: java android xml google-maps

我有一个名为MapsActivity.java的类和名为activity_maps.xml的关联xml文件。我还有另一个名为Sigiriya.java的类,其中我有一个onClick()方法,其中包含从Sigirya.java移动的代码到MapsActivity.java。我想要做的是在按钮点击上传递所需位置的经度和纬度,因此我在MapsActivity.Java中创建了一个方法,它获取三个参数并将其传递给onMapReady()方法。在Sigiriya.java上我创建了一个MapsActivity.java对象并调用了方法setMapInfo()并传递了值。问题是这些值没有传递到地图上,它只是在赤道上显示了一些海洋和标记请帮助我。

Sigiriya.java

public class Sigiriya extends AppCompatActivity{

TextView sigiriyaoverview;
Button readmorebtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sigiriya);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    Button mapbtn = (Button)findViewById(R.id.mapsbtn);

    mapbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MapsActivity map = new MapsActivity();
            map.setMapInfo(7,80,"Sigiriya");
            startActivity(new Intent(Sigiriya.this, MapsActivity.class));
        }
    });
}

@Override
public boolean onSupportNavigateUp() {
    onBackPressed();
    return true;
}

}

MapsActivity.java

public class MapsActivity extends FragmentActivity implements 
OnMapReadyCallback {

private GoogleMap mMap;
private double latitude;
private double longitude;
private String locname;

public void setMapInfo(double lat,double lon, String loc)
{
    latitude = lat;
    longitude = lon;
    locname = loc;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // 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);
}

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

    // Add a marker in Sydney and move the camera
    LatLng location = new LatLng(latitude, longitude);
    mMap.addMarker(new MarkerOptions().position(location).title(locname));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(location));
}

}

请帮我解决此问题

0 个答案:

没有答案