如何在点击的图像上打开地图活动?

时间:2016-10-19 18:58:42

标签: android

我创建了一个地图活动。我点击按钮调用地图激活。但它显示错误。这是我的代码: `map_icon.setOnClickListener(new OnClickListener(){

     @Override
     public void onClick(View v) {
     Intent i = new Intent(DepartmentUpdate_Detail.this,
     MapActivity.class);
    i.putExtra("lat", lat);
    i.putExtra("lng", lng);
    startActivity(i);
     }
     });`

`

//我的地图活动如下:

public class MapActivity扩展Activity实现OnInfoWindowClickListener {

private GoogleMap map;

private int zoomLevel = 7;

String latitude,longitude,address,city,country;


protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.map_activity);

    Bundle extras = getIntent().getExtras();
    if (extras != null)
    {   
        latitude = extras.getString("lat");
        longitude = extras.getString("lng");

    }   
    LatLng defaultLatLng = new LatLng(Double.valueOf(latitude), Double.valueOf(longitude));
    //Log.e("Latitude:Longitude",":"+Double.valueOf(latitude)+":"+ Double.valueOf(longitude));

     Geocoder geocoder;
         List<Address> addresses;
         geocoder = new Geocoder(this, Locale.getDefault());
         try {
        addresses = geocoder.getFromLocation(Double.valueOf(latitude), Double.valueOf(longitude), 1);
          address = addresses.get(0).getAddressLine(0);
          city = addresses.get(0).getAddressLine(1);
          country = addresses.get(0).getAddressLine(2);

    } catch (NumberFormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {

        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

        if (map!=null){

            map.getUiSettings().setCompassEnabled(true);

            map.setTrafficEnabled(true);

            map.setMyLocationEnabled(true);

            // Move the camera instantly to defaultLatLng.

            map.moveCamera(CameraUpdateFactory.newLatLngZoom(defaultLatLng, zoomLevel));

            map.addMarker(new MarkerOptions().position(defaultLatLng)

                    .title("Location:")

                    .snippet(address+", "+city+", "+country)

                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));

            map.setOnInfoWindowClickListener(this);
        }

    }
    catch (NullPointerException e)
    {
        e.printStackTrace();
    }

}

@Override

public void onPause() {

    if (map != null){
        map.setMyLocationEnabled(false);
        map.setTrafficEnabled(false);
    }
    super.onPause();
}

@Override

public void onInfoWindowClick(Marker marker) {
}

} `

2 个答案:

答案 0 :(得分:0)

@Aks,你在哪里声明了onClickListener?它应该在onCreate()方法中声明,这样一旦活动膨胀,clickListener就可以监听了。

答案 1 :(得分:0)

只需将您的数据放在Uri.parse中,此代码放在onClick方法

Uri gmmIntentUri = Uri.parse("google.navigation:q=Taronga+Zoo,+Sydney+Australia&mode=b");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);