我想创建一个搜索预定义标记并在地图上显示的应用。我试图使用MVC,SINGLETON和CALLBACK模式。我在单例中创建了一个方法,它将在地图上显示标记:
问题是它在尝试创建IconGenerator时会给我一个NullPointerException。是否有可能在这里创建地图?我看到主要活动扩展了FragmentActivity并实现了OnMapReadyCallback。
public class Controller {
GoogleMap mMap;
private Controller() {
}
public static Controller getInstance() {
return new Controller();
}
public Map<String, List<Marker>> adaugaRuta(ShowTheRute theRoute) {
Map<String, List<Marker>> objectMap = new HashMap<>();
Context context = MapsActivity.getAppContext();
IconGenerator iconita = new IconGenerator(context);
iconita.setContentPadding(5, -5, 5, -5);
iconita.setRotation(-270);
iconita.setContentRotation(270);
Bitmap iconbit = iconita.makeIcon("test2");
final Marker mae = mMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(iconbit)).position(new LatLng(46.762035, 23.597659)).anchor(0f, 0.5f));
mae.isVisible();
List<Marker> marker1 = new ArrayList<>();
marker1.add(mae);
objectMap.put("UK", marker1);
theRoute.getTheRute(objectMap);
return objectMap;
}}
界面是:
public interface ShowTheRute {
void getTheRute(Map<String, List<Marker>> rute);}
主要活动是:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, ShowTheRute {
private GoogleMap mMap;
private static Context appContext;
private ArrayList<Polyline> mPolyline;
AutoCompleteTextView autoCompleteTextView;
Map<String, List<Marker>> mObjectMap = new HashMap<>();
String countries[] = {"INDIA", "ITALY", "JAPAN", "USA", "ICELAND",
"INDONESIA", "UK", "IRAN", "IRAQ"};
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
appContext=this;
// 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);
autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
autoCompleteTextView.setThreshold(1);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, countries);
autoCompleteTextView.setAdapter(adapter);
Controller.getInstance().adaugaRuta(MapsActivity.this);
autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String s = parent.getItemAtPosition(position).toString();
if (s.equals("INDIA")) {
PolylineOptions ruta1 = new PolylineOptions()
.add(new LatLng(46.760464, 23.561524))
.add(new LatLng(46.760795, 23.564249))
.add(new LatLng(46.761853, 23.568144))
.add(new LatLng(46.763139, 23.572757))
.add(new LatLng(46.764719, 23.575010))
.add(new LatLng(46.767137, 23.581737))
.add(new LatLng(46.770459, 23.588861))
.add(new LatLng(46.774594, 23.587409))
.add(new LatLng(46.774594, 23.587409));
Polyline polyline = mMap.addPolyline(ruta1);
} else for (String key : mObjectMap.keySet()) {
if (s.equals(key)) {
mMap.clear();
PolylineOptions ruta1 = new PolylineOptions()
.add(new LatLng(46.756706, 23.597723))
.add(new LatLng(46.762035, 23.597659))
.add(new LatLng(46.766731, 23.598421))
.add(new LatLng(46.772441, 23.595556));
Polyline polyline1 = mMap.addPolyline(ruta1);
}
} /*else {
Toast toast = Toast.makeText(getApplicationContext(), s + " is clicked", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}*/
}
});
}
public static Context getAppContext() {
return appContext;
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng medias = new LatLng(46.767108, 23.581757);
mMap.addMarker(new MarkerOptions().position(medias).title("medias"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(medias, 9));
mMap.setMyLocationEnabled(true);
}
@Override
public void getTheRute(Map<String, List<Marker>> rute) {
mObjectMap.putAll(rute);
}
}