百度地图加了PopupOverlay运行崩溃?

package com.example.baidumaptest;

import java.util.List;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.map.LocationData;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationOverlay;
import com.baidu.mapapi.map.PopupClickListener;
import com.baidu.mapapi.map.PopupOverlay;
import com.baidu.platform.comapi.basestruct.GeoPoint;

import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {

private BMapManager manager;
private MapView mapView;
private LocationManager locationManager;
private String provider;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    manager=new BMapManager(this);
    manager.init("zaXCIUaapBhmytPznnUBnBAjg4bpYi8a", null);
    setContentView(R.layout.activity_main);
    mapView=(MapView)findViewById(R.id.map_view);
    mapView.setBuiltInZoomControls(true);
    locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
    List<String>providerList=locationManager.getProviders(true);
    if(providerList.contains(LocationManager.GPS_PROVIDER)){
        provider=LocationManager.GPS_PROVIDER;
    }else if(providerList.contains(LocationManager.NETWORK_PROVIDER)){
        provider=LocationManager.NETWORK_PROVIDER;
    }else{
        Toast.makeText(this, "NO location provider to use", Toast.LENGTH_SHORT).show();
        return;
    }
    Location location=locationManager.getLastKnownLocation(provider);
    if(location!=null){
        navigateTo(location);
    }
}

private void navigateTo(Location location){
    MapController controller=mapView.getController();
    controller.setZoom(16);
    GeoPoint point=new GeoPoint((int)(location.getLatitude() * 1E6), (int)(location.getLongitude() * 1E6));
    controller.setCenter(point);
    MyLocationOverlay myLocationOverlay=new MyLocationOverlay(mapView);
    LocationData locationData=new LocationData();
    locationData.latitude=location.getLatitude();
    locationData.longitude=location.getLongitude();
    myLocationOverlay.setData(locationData);
    mapView.getOverlays().add(myLocationOverlay);
    PopupOverlay pop=new PopupOverlay(mapView, new PopupClickListener() {

        @Override
        public void onClickedPopup(int index) {
            // TODO Auto-generated method stub
            Toast.makeText(MainActivity.this, "you clicked button"+index, Toast.LENGTH_SHORT).show();
        }
    });
    Bitmap[] bitmaps=new Bitmap[3];
    try{
        bitmaps[0]=BitmapFactory.decodeResource(getResources(), R.drawable.left);
        bitmaps[1]=BitmapFactory.decodeResource(getResources(), R.drawable.middle);
        bitmaps[2]=BitmapFactory.decodeResource(getResources(), R.drawable.right);
    }catch(Exception e){
        e.printStackTrace();
    }
    pop.showPopup(bitmaps, point, 18);
    mapView.refresh();
}

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    mapView.destroy();
    if(manager!=null){
        manager.destroy();
        manager=null;
    }
    super.onDestroy();
}



@Override
protected void onPause() {
    // TODO Auto-generated method stub
    mapView.onPause();
    if(manager!=null){
        manager.stop();
    }
    super.onPause();
}



@Override
protected void onResume() {
    // TODO Auto-generated method stub
    mapView.onResume();
    if(manager!=null){
        manager.start();
    }
    super.onResume();
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

查看全部
wuzg1020
wuzg1020
2016/08/13 07:58
  • 点赞
  • 收藏
  • 回答
    私信

1个回复