Reputation: 337
I have a problem on how to display/plot the locations in map. I want to put a marker in every specific locations. I created sqlite database to store my data(MallName, address, latitude and longitude). Everything works fine. It returns values from my sqlite. I tested it using Log. See MallMarkers.java
Maybe I made a mistake in using MarkerOptions. Please help me. Here's my Code.
Class GoogleMapsApiActivity.java
public class GoogleMapsApiActivity extends FragmentActivity implements LocationListener{
private ArrayList<MallMarkers> malls = new ArrayList<MallMarkers>();
private LocationManager locationManager;
private GoogleMap googleMap;
private static final String DEBUG_TAG = "GoogleMapsApiActivity";
private Location myLocation = null;
private UiSettings mapInterface;
private boolean zoom = true;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_TIME_BW_UPDATES = 1000*60*1;
private DataSource datasource;
private SQLiteDatabase database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_api);
zoom = true;
GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
}
private void setUpMapIfNeeded() {
if (googleMap == null) {
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
if (googleMap != null) {
setUpMapSetings();
}
}
}
private void setUpMapSetings(){
SharedPreferences sharedMapSettings = PreferenceManager.getDefaultSharedPreferences(this);
String mapViewLayout = sharedMapSettings.getString("map_layer", "Normal");
Boolean mapTraffic = sharedMapSettings.getBoolean("key_mapTraffic", false);
Boolean mapCompass = sharedMapSettings.getBoolean("key_mapCompass", true);
boolean mapMyLocation = sharedMapSettings.getBoolean("key_mapMyLocation", true);
boolean mapButtonZoom = sharedMapSettings.getBoolean("key_mapButtonZoom", true);
mapInterface = googleMap.getUiSettings();
//plotMalls();
if(mapViewLayout.equals("Normal")){
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
else if(mapViewLayout.equals("Hybrid")){
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
}
else if(mapViewLayout.equals("Satellite")){
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
}
else if(mapViewLayout.equals("Terrain")){
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
}
googleMap.setTrafficEnabled(mapTraffic);
mapInterface.setCompassEnabled(mapCompass);
mapInterface.setMyLocationButtonEnabled(mapMyLocation);
mapInterface.setZoomControlsEnabled(mapButtonZoom);
googleMap.getUiSettings().setAllGesturesEnabled(true);
googleMap.setMyLocationEnabled(true);
}
//I used cursor to query my data from sqlite (Works fine)
private void plotLocationMallMapsUI(){
database=openOrCreateDatabase(com.example.sqlite.MySQLiteHelper.DATABASE_NAME, Context.MODE_PRIVATE, null);
String sqlcommand = "SELECT MallName, MallStreetAddress, MallCityAddress, MallContactNumber, MallLatitude, MallLongitude FROM mallsinfo";
Cursor cursor = database.rawQuery(sqlcommand, null);
malls = new ArrayList<MallMarkers>();
if(cursor.moveToFirst()){
do{
MallMarkers markers = new MallMarkers(cursor.getString(0),
cursor.getString(1),
cursor.getString(2),
cursor.getString(3),
cursor.getString(4),
cursor.getString(5), this);
malls.add(markers);
}while(cursor.moveToNext());
}
cursor.close();
database.close();
}
@Override
public void onRestart() {
super.onRestart();
setUpMapSetings();
}
@Override
public void onResume() {
super.onResume();
setUpMapIfNeeded();
plotLocationMallMapsUI();
}
@Override
public void onPause() {
locationManager.removeUpdates(this);
super.onPause();
}
@Override
public void onLocationChanged(Location location) {
LatLng latlng = new LatLng(location.getLatitude(), location.getLongitude());
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
location = locationManager.getLastKnownLocation(provider);
double lat = (double) (location.getLatitude());
double lng = (double) (location.getLongitude());
Log.d(DEBUG_TAG, "Latitude: " + String.valueOf(lat));
Log.d(DEBUG_TAG, "Longitude: " + String.valueOf(lng));
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
It returns values from sqlite. I tested it using Log. But it doesnt display the Plots. Help me please. How to display it using markers?
Class MallMarkers.java
public class MallMarkers {
private String mMallId = "";
private String mMallName = "";
private String mMallStreetAddress = "";
private String mMallCityAddress = "";
private String mMallContactNumber = "";
private String mMallLatitude = "";
private String mMallLongitude = "";
private Context mContext;
private LatLng mMallCoordinates = null;
private MarkerOptions mMallMarkerOptions=null;
private Double mLatitude;
private Double mLongitude;
private GoogleMap googlemap;
public MallMarkers(String mallname, String mallstreetaddress, String mallcityaddress, String mallcontactnumber, String malllatitude, String malllongitude, Context context){
mContext = context;
mMallName = mallname;
mMallStreetAddress = mallstreetaddress;
mMallCityAddress = mallcityaddress;
mMallContactNumber = mallcontactnumber;
mMallLatitude = malllatitude;
mMallLongitude = malllongitude;
mLatitude = Double.parseDouble(mMallLatitude);
mLongitude = Double.parseDouble(mMallLongitude);
String FilterMallName;
String FilterMallStreetAddress;
String FilterMallCityAddress;
String FilterMallContacts;
if(mMallName.equals("")){
FilterMallName = "Blank";
}else{
FilterMallName = mMallName;
}
if(mMallStreetAddress.equals("")){
FilterMallStreetAddress = "Blank";
}else{
FilterMallStreetAddress = mMallStreetAddress;
}
if(mMallCityAddress.equals("")){
FilterMallCityAddress = "Blank";
}else{
FilterMallCityAddress = mMallCityAddress;
}
if(mMallContactNumber.equals("")){
FilterMallContacts = "Blank";
}else{
FilterMallContacts = mMallContactNumber;
}
Log.d("TAG", FilterMallName + ", \n"
+ FilterMallStreetAddress + ", \n"
+ FilterMallCityAddress+ ", \n"
+ FilterMallContacts + "\n"
+ String.valueOf(mLatitude) + " ," + String.valueOf(mLongitude)
+ "\n------------------------\n");
mMallCoordinates = new LatLng(mLatitude, mLongitude);
mMallMarkerOptions = new MarkerOptions();
mMallMarkerOptions.position(mMallCoordinates);
mMallMarkerOptions.title(FilterMallName);
mMallMarkerOptions.snippet(FilterMallStreetAddress + ", " + FilterMallCityAddress + ", " + FilterMallContacts);
mMallMarkerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
}
}
Upvotes: 1
Views: 1752
Reputation: 22028
Try something like this in your plotLocationMallMapsUI()
after database.close()
for (MallMarkers mm : malls) {
Marker marker = googlemap.addMarker(new MarkerOptions().position(mm.getmalllatitude, mm.getmalllongitude));
}
Upvotes: 0
Reputation: 41099
here is a loop where I set markers on the map and it works great for me:
for (Task tempTask : TasksListAppObj.getInstance().tasksRepository.getTasksRepository())
{
LatLng latlng = new LatLng(tempTask.getLatitude(), tempTask.getLongtitude());
if (tempTask.getStatus().contentEquals(TasksListActivity.STATUS_WAITING))
{
newmarker = map.addMarker(new MarkerOptions().position(latlng).title(tempTask.getTitle()).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_blue)));
}
else if (tempTask.getStatus().contentEquals(TasksListActivity.STATUS_IN_PROGRESS))
{
newmarker = map.addMarker(new MarkerOptions().position(latlng).title(tempTask.getTitle()).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_bordo)));
}
else if (tempTask.getStatus().contentEquals(TasksListActivity.STATUS_ON_THE_WAY))
{
newmarker = map.addMarker(new MarkerOptions().position(latlng).title(tempTask.getTitle()).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_turkiz)));
}
else if (tempTask.getStatus().contentEquals(TasksListActivity.STATUS_COMPLETE))
{
newmarker = map.addMarker(new MarkerOptions().position(latlng).title(tempTask.getTitle()).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_orange)));
}
else if (tempTask.getStatus().contentEquals(TasksListActivity.STATUS_FAILED))
{
newmarker = map.addMarker(new MarkerOptions().position(latlng).title(tempTask.getTitle()).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_purpul)));
}
}
See if it's helps you.
The if statements are for changing the marker icon.
What you are missing is the addMarker
method applied to the map to actually add the Marker
object to GoogleMap
object.
Upvotes: 1