谷歌地图不显示标记

时间:2014-03-14 04:27:43

标签: android google-maps-android-api-2

我必须在我的应用程序中实现Google map v2,这是一个完美的工作。但我的问题是我必须将一个Marker位置从一个活动发送到另一个活动,然后将标记添加到Google地图中,但我不知道它不是。

在下面发布我的代码。

public class BasicMapActivity extends FragmentActivity{

private GoogleMap mMap;
AlertMessages messages;
String latitude;
double lat = 0, lng = 0;
String longitude ;
String title = "", city = "";
static boolean is_back_pressed = false;
ImageView img;
GPSTracker gps;
RelativeLayout rel;
Marker Contactloc;
LatLng contactLoc;
Button ib_back;
static boolean is_window_pressed = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.basic_demo_new);
    img = (ImageView) findViewById(R.id.popup_h);
    img.setVisibility(View.INVISIBLE);
    gps = new GPSTracker(getParent());
    messages = new AlertMessages(getParent());

    Bundle b = getIntent().getExtras();
    title = b.getString("title");
    city = b.getString("City");

    latitude = b.getString("latitude");
    longitude = b.getString("longitude");

    back_layout=(LinearLayout)findViewById(R.id.back_lay);
    ib_back=(Button)findViewById(R.id.ib_back_music);

    System.out.println("Lat:::" + latitude);
    System.out.println("Long:::" + longitude);

    setUpMapIfNeeded();

    ib_back.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            is_back_pressed = true;
            TabGroupActivity parentActivity = (TabGroupActivity) getParent();
            parentActivity.onBackPressed();

        }
    });

}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    try{
        System.out.println("Map Pause state");
                if(mMap!=null){
                    mMap.clear();
                    mMap=null;

                TabGroupActivity parentActivity = (TabGroupActivity) getParent();
                parentActivity.onBackPressed();

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

}

@Override
protected void onResume() {
    super.onResume();

    int status = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(getParent());

    if (status == ConnectionResult.SUCCESS) {

        setUpMapIfNeeded();
        mMap.setMyLocationEnabled(true);
    } else {
        int requestCode = 10;
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, getParent(),
                requestCode);
        dialog.show();
    }

}

protected LocationManager locationManager;
LatLng pos=null;

private void setUpMapIfNeeded() {
    System.out.println("Setup If Needed");

    // Do a null check to confirm that we have not already instantiated the
    // map.
    if (mMap == null) {

        mMap = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map1)).getMap();

        mMap.setMyLocationEnabled(true);

        boolean isInternetPresent = isConnectingToInternet();
        if (!isInternetPresent) {
            // Internet Connection is not present
            Toast.makeText(getParent(), "Internet Connection Error!",Toast.LENGTH_SHORT).show();
            //messages.showAlertDialog(getParent(),
                //  "Internet Connection Error",
                    //"Please connect to working Internet connection");
            // stop executing code by return
            // return;
        } else {

            locationManager = (LocationManager) getParent()
                    .getSystemService(LOCATION_SERVICE);

            // getting GPS status

            if (!locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                showSettingsAlert();
            }
            if (gps.canGetLocation()) {

                lat = gps.getLatitude();
                lng = gps.getLongitude();
                 pos = new LatLng(lat, lng);

                if (lat == 0.0 && lng == 0.0) {

                    Toast.makeText(getParent(), "Can't find location",
                            Toast.LENGTH_SHORT).show();

                } else {

                    mMap.addMarker(new MarkerOptions()
                            .icon(BitmapDescriptorFactory
                                    .fromResource(R.drawable.mark_blue))
                            .position(pos)
                            .title("Current Location"));

                    double trans = 0.3;
                    for (int k = 100; k <= 500;) {
                        // draw circle
                        int radiusM = k;
                        int d = k; // diameter
                        Bitmap bm = Bitmap.createBitmap(d, d,
                                Config.ARGB_8888);
                        Canvas c = new Canvas(bm);
                        Paint p = new Paint();
                        p.setColor(getResources().getColor(
                                android.R.color.darker_gray));
                        c.drawCircle(d / 2, d / 2, d / 2, p);
                        // generate BitmapDescriptor from circle
                        // Bitmap
                        BitmapDescriptor bmD = BitmapDescriptorFactory
                                .fromBitmap(bm);
                        // mapView is the GoogleMap
                        mMap.addGroundOverlay(new GroundOverlayOptions()
                                .image(bmD)
                                .position(pos, radiusM * 2,
                                        radiusM * 2)
                                .transparency((float) trans));
                        k = k + 100;
                        trans = trans + 0.1;
                    }

                }

            } else {
                Toast.makeText(getParent(), "Can't find location",
                        Toast.LENGTH_SHORT).show();

            }

        }

        mMap.getUiSettings().setCompassEnabled(true);
        mMap.setTrafficEnabled(true);
        mMap.getUiSettings().setTiltGesturesEnabled(true);
        mMap.getUiSettings().setRotateGesturesEnabled(true);
        mMap.getUiSettings().setScrollGesturesEnabled(true);
        mMap.getUiSettings().setZoomControlsEnabled(true);
        mMap.getUiSettings().setZoomGesturesEnabled(true);
        setUpMap();
    } // Check if we were successful in obtaining the map.
    if (mMap != null) {
        mMap.setMyLocationEnabled(true);
        setUpMap();
    }
}

private void setUpMap() {
    System.out.println("Setup Map");
  try{
      contactLoc=new LatLng(latitude, longitude);
    Contactloc=mMap.addMarker(new MarkerOptions()
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.mark_red))
            .position(new LatLng(latitude, longitude)).title(title)
            .snippet(city));
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
            latitude, longitude), 18.0f));
  }catch(Exception ew){
      ew.printStackTrace();
  }
}

}
请某人帮助我!

1 个答案:

答案 0 :(得分:2)

添加标记,如下所示

 Contactloc=mMap.addMarker(new MarkerOptions()
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.mark_red))
        .position(new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude)).title(title)
        .snippet(city));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
        latitude, longitude), 18.0f));

您需要将纬度和经度String值转换为Double。和AFAIK你说没有任何错误。你的应用程序使用一些logcat在这里肆无忌惮地崩溃。