I want to load images from Parse Cloud. I have created custom marker with the photo loaded from Parse Cloud. The photo marker icons are working perfectly fine. But I want to start and pass the intent when I click on the marker icon. But no matter which options I select to view in intent it just views only the first object in my Parse Class .Check the screenshot of the map here.When on i click on any image,the same image i.e the first object in the class gets loaded.
super.onCreate(savedInstanceState);
setContentView(R.layout.map_fragment);
mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mHandler.postDelayed(new Runnable() {
public void run() {
onResume();
}
}, 3000);
mylocation = MainActivity.mlastlocation;
// Gets to GoogleMap from the MapView and does initialization stuff
mapFragment.getMap().setMyLocationEnabled(true);
// Enable the current location "blue dot"
mapFragment.getMap().setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
public void onCameraChange(CameraPosition position) {
if (mylocation == null) {
Toast.makeText(MapFeedsFragment.this, "null", Toast.LENGTH_SHORT).show();
}
if (mylocation != null)
Toast.makeText(MapFeedsFragment.this, "not null", Toast.LENGTH_SHORT).show();
ParseMap aroundposts = new ParseMap();
doMapQuery();
}
});
}
private ParseGeoPoint geoPointFromLocation(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
ParseGeoPoint myloc = new ParseGeoPoint(latitude, longitude);
return myloc;
}
private void doMapQuery() {
final int myUpdateNumber = ++mostRecentMapUpdate;
//Location myLoc = (currentLocation == null) ? lastLocation : currentLocation;
// If location info isn't available, clean up any existing markers
if (mylocation == null) {
cleanUpMarkers(new HashSet<String>());
return;
}
final ParseGeoPoint myPoint = geoPointFromLocation(mylocation);
// Create the map Parse query
ParseQuery<ParseMap> mapQuery = ParseMap.getQuery();
// Set up additional query filters
//mapQuery.whereWithinKilometers("location", myPoint, MAX_POST_SEARCH_DISTANCE);
mapQuery.include("user");
mapQuery.orderByDescending("createdAt");
//mapQuery.setLimit(MAX_POST_SEARCH_RESULTS);
// Kick off the query in the background
mapQuery.findInBackground(new FindCallback<ParseMap>() {
#Override
public void done(List<ParseMap> objects, ParseException e) {
if (e != null) {
//if (Application.APPDEBUG) {
// Log.d(Application.APPTAG, "An error occurred while querying for map posts.", e);
//}
return;
}
/*
* Make sure we're processing results from
* the most recent update, in case there
* may be more than one in progress.
*/
if (myUpdateNumber != mostRecentMapUpdate) {
return;
}
// Posts to show on the map
Set<String> toKeep = new HashSet<String>();
// Loop through the results of the search
for (final ParseMap post : objects) {
// Add this post to the list of map pins to keep
toKeep.add(post.getObjectId());
// Check for an existing marker for this post
Marker oldMarker = mapMarkers.get(post.getObjectId());
// Set up the map marker's location
String uri = post.getPhotoThumb().getUrl();
// final MarkerOptions finalMarkerOpts = markerOpts;
Picasso.with(MapFeedsFragment.this)
.load(uri)
.resize(100,100)
.centerCrop()
.into(new Target() {
#Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
/* Save the bitmap or do something with it here */
final MarkerOptions markerOpts =
new MarkerOptions().position(new LatLng(post.getLocation().getLatitude(), post
.getLocation().getLongitude())).icon(BitmapDescriptorFactory.fromBitmap(bitmap)).title(post.getUser());
// Add a new marker
Marker marker = mapFragment.getMap().addMarker(markerOpts);
mapMarkers.put(post.getObjectId(), marker);
mapMarkers.put(post.getVideo().getUrl(),marker);
mapMarkers.put(post.getUserId(),marker);
mapMarkers.put(String.valueOf(post.getViews()),marker);
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
mapFragment.getMap().setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
Intent intent = new Intent (MapFeeds.this,PhotoView.class);
intent.putExtra("objectId",post.getObjectId());
intent.putExtra("photo", String.valueOf(post.getPhoto().getUrl()));
intent.putExtra("userId",post.getUserId());
intent.putExtra("objectId",post.getObjectId());
startActivity(intent);
return false;
}
});
}
}
});
}
Related
i am trying to run a geoquery everytime someone select a place from PlaceAutoComplete fragment and show the markers on the map. It is working fine for the first.When i start the app the icons are are all fine the geoquery is running fine, but when i enter a location second time the app crashes showing an error llegalArgumentException: Unmanaged descriptor below is what i am trying to do.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
SupportPlaceAutocompleteFragment autocompleteFragment =
(SupportPlaceAutocompleteFragment)
getChildFragmentManager().findFragmentById
(R.id.place_autocomplete_fragment);
autocompleteFragment.setOnPlaceSelectedListener(new
PlaceSelectionListener() {
#Override
public void onPlaceSelected(Place place) {
// TODO: Get info about the selected place.
Toast.makeText(getContext(),place.getAddress(),Toast.LENGTH_LONG).show();
Log.i(TAG, "Place: " + place.getName());
Double latitude1 = place.getLatLng().latitude;
Double longitude1 =place.getLatLng().longitude;
LatLng latLng = new LatLng(latitude1,longitude1);
getPeople(latLng); // method to call geofire query
}
#Override
public void onError(Status status) {
// TODO: Handle the error.
Log.i(TAG, "An error occurred: " + status);
}
});
return mMainView;
}
public void getPeople(LatLng latLng1){
mMap.clear();
mMap.addCircle(new CircleOptions()
.center(latLng1)
.radius(2000)
.strokeColor(Color.BLACK)
.fillColor(0x220000FF)
.strokeWidth(1)
);
DatabaseReference ref =
FirebaseDatabase.getInstance().getReference().child("Location");
GeoFire geoFire = new GeoFire(ref);
GeoQuery geoQuery = geoFire.queryAtLocation(new
GeoLocation(latLng1.latitude, latLng1.longitude), 2);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
#Override
public void onKeyEntered(final String key, GeoLocation location) {
UIDLocation.put(key,location);
marker.setIcon(BitmapDescriptorFactory.fromResource
(R.drawable.ic_mapmarker2));
markers.put(key, marker);
for (Map.Entry<String,GeoLocation> entry : UIDLocation.entrySet())
{
final Marker marker = markers.get(entry.getKey());
if (marker != null) {
DatabaseReference mUser =
FirebaseDatabase.getInstance().getReference().child("People")
.child(string);
mUser.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot)
{
String display_name =
dataSnapshot.child("name").getValue().toString();
String status =
dataSnapshot.child("status").getValue().toString();
String image =
dataSnapshot.child("image").getValue().toString();
PeopleInfo info = new PeopleInfo();
info.setName(display_name);
info.setStatus(status);
info.setImage(image);
marker.setTag(info);
String iconName = dataSnapshot.child("iconName")
.getValue().toString();
Context context = getContext();
int id = context.getResources().getIdentifier(iconName, "drawable",
context.getPackageName());
String s = String.valueOf(id);
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),id);
marker.setIcon(BitmapDescriptorFactory.fromResource(id));
}
#Override
public void onCancelled(DatabaseError
databaseError) {
}
});
}
}
#Override
public void onMapReady(final GoogleMap googleMap) {
mMap = googleMap;
mUiSettings = mMap.getUiSettings();
mUiSettings.setZoomControlsEnabled(true);
mFusedLocationClient =
LocationServices.getFusedLocationProviderClient(getContext());
Task task= mFusedLocationClient.getLastLocation()
.addOnSuccessListener(getActivity(), new
OnSuccessListener<Location>() {
#Override
public void onSuccess(Location location) {
if (location != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
myPosition = new LatLng(latitude, longitude);
markeroptn = new MarkerOptions();
markeroptn.position(myPosition);
markeroptn.title("You are Here");
mMap.moveCamera(CameraUpdateFactory.newLatLng(myPosition));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(myPosition,10));
getWorkMen(myPosition);
}
}
});
So far what i have learned from the posts on SO that its because the program is trying to set the icon on marker which is already there. I tried clear() map in the begining of getPeople() but still it is showing the same error.It works fine first time. i also tried remove() also but its also not working.
The problem may come from the way you manage the Marker variables. In the code, you store a marker variable as global outside of the method scope. When calling map.clear(), it makes the marker variable invalid and if somehow you still use this variable to set something, it may cause the exception. The same thing happens with the markers Map you use to map key and Marker, it's not being cleared when map.clear().
Try to manage your map elements more carefully, clear each map element dependently and avoid using map.clear().
Suggestion approach:
Create new marker
private void addMarker(String key, LatLng latLng) {
// Clear the current marker before add the new one
if (marker != null) {
marker.remove();
marker = null;
}
// Store new marker to the variable
marker = mMap.addCircle(new CircleOptions()
.center(latLng)
.radius(2000)
.strokeColor(Color.BLACK)
.fillColor(0x220000FF)
.strokeWidth(1)
);
// Add to markers map if needed
markers.put(key, marker);
}
Clear all markers (clear manually every marker variable available, don't use map.clear:
public synchronized void clear() {
// markers is the marker map
for (Marker marker : markers.values()) {
try {
marker.remove();
} catch (IllegalArgumentException ex) {
ex.printStackTrace();
}
}
// Clear all the marker map
markers.clear();
// Marker is the your global marker variable
marker.remove();
}
I want the previously drawn circle to be removed. When I am searching for a new place, the first circle remains there and the new circle is also drawn. Basically every time when the location gets changed, circle keeps getting drawn(overlapping each other) without deleting the previous circle.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_geocoding_maps);
toolbar=(Toolbar)findViewById(R.id.mapToolbar);if (!checkPlayServices()) {
Toast.makeText(this, "Device not compatible", Toast.LENGTH_SHORT).show();
finish();
}
Bundle extras=getIntent().getExtras();
if(extras==null)
{
itemAction=-1;
Log.w(TAG, "Extras are null");
finish();
}
else
{
try{
item=extras.getParcelable(GeofenceUtils.P_KEY);
}catch (Exception e)
{
Log.w(GeofenceUtils.getTag(), e.getMessage());
}
if (item != null) {
if(item.getLatitude()==0.0&&item.getLongitude()==0.0) {
//New item
itemAction=0;
}
else {
//Update item
Log.w(TAG, "Title : "+item.getTitle());
itemAction=1;
}
Log.w(TAG, "Item Action: "+itemAction);
}
else {
Log.w(TAG, "Item null: ");
}
}
dbHelper=GeofenceDBHelper.getInstance(this);
assert toolbar != null;
toolbar.setTitle("Pick a location");
toolbar.setTitleTextColor(Color.WHITE);
setSupportActionBar(toolbar);
address="";
addressText = (EditText) findViewById(R.id.addressText);
if(itemAction==1) {
address=item.getAddress();
if (addressText != null) {
addressText.setText(address);
}
}
addressText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
Log.w(TAG, "onEditorAction");
address = addressText.getText().toString();
LatLng latLng = getLocationFromAddress(GeocodingMapsActivity.this, address);
if (latLng != null) {
mlatitude = latLng.latitude;
mlongitude = latLng.longitude;
marker.setPosition(latLng);//match this behavior to your 'Send' (or Confirm) button
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 18);
mMap.animateCamera(cameraUpdate);
circle = drawCircle(new LatLng(mlatitude, mlongitude));
} else {
Log.w(TAG, "onEditorAction: address null");
}
}
return false;
}
});
if(savedInstanceState!=null)
{
//For handling screen rotations
mlatitude=savedInstanceState.getDouble("Latitude");
mlongitude=savedInstanceState.getDouble("Longitude");
Log.w(TAG, "itemAction savedInstanceState: "+itemAction);
}
else
{
mlatitude=item.getLatitude();
mlongitude=item.getLongitude();
}
clearButton=(ImageButton)findViewById(R.id.clear_text);
clearButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addressText.setText("");
}
});
MyLocationButton=(FloatingActionButton)findViewById(R.id.fab);
MyLocationButton.setBackgroundTintList(ColorStateList.valueOf(Color.WHITE));
MyLocationButton.setRippleColor(Color.parseColor("#f5f5f5"));
MyLocationButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mGoogleApiClient.isConnected()) {
LatLng latLng = getCurrentLocation();
if (latLng != null) {
setAddress();
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 18);
mMap.animateCamera(cameraUpdate);
// circle = drawCircle(new LatLng(mlatitude, mlongitude));
}
}
}
});
MyLocationButton.setVisibility(View.GONE);
mGeofenceList=new ArrayList<>();
buildGoogleApiClient();
createLocationRequest();
buildLocationSettingsRequest();
checkLocationSettings();
// 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);
Log.w(TAG, "onCreate");
}
private void removeEverything(){
marker.remove();
marker = null;
circle.remove();
circle = null;
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
Circle circle;
#Override
public void onMapReady(GoogleMap googleMap) {
Log.w(TAG, "onMapReady1");
mMap = googleMap;
// mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
enableMyLocation();
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng latLng) {
marker.setPosition(latLng);
mlatitude=latLng.latitude;
mlongitude=latLng.longitude;
setAddress();
}
});
Log.w(TAG, "onMapReady2");
LatLng mLocation=null;
if(mGoogleApiClient.isConnected()&&itemAction==0)
mLocation = getCurrentLocation();
Log.w(TAG, "onMapReady3");
if (itemAction==0&&mLocation != null) {
mlatitude = mLocation.latitude;
mlongitude = mLocation.longitude;
}
else if(itemAction==1) {
mLocation=new LatLng(mlatitude, mlongitude);
}
else
mLocation = new LatLng(0.0, 0.0);
Log.w(TAG, "onMapReady3");
if(mGoogleApiClient.isConnected())
setAddress();
Log.w(TAG, "onMapReady4");
marker = mMap.addMarker(new MarkerOptions().position(mLocation).draggable(true));
Log.w(GeofenceUtils.getTag(), item.getMarkerColor()+"");
marker.setIcon(BitmapDescriptorFactory.fromResource(GeofenceUtils.getMarker(item.getMarkerColor())));
mMap.setOnMarkerDragListener(this);
if(itemAction==1)
{
CameraUpdate cameraUpdate=CameraUpdateFactory.newLatLngZoom(new LatLng(item.getLatitude(), item.getLongitude()), 18);
CameraUpdateFactory.newLatLngZoom(mLocation, 18);
mMap.animateCamera(cameraUpdate);
// circle = drawCircle(new LatLng(mlatitude, mlongitude));
}
Log.w(TAG, "onMapReady5");
}
private Circle drawCircle(LatLng latLng) {
CircleOptions options = new CircleOptions()
.center(latLng)
.radius(50)
.strokeColor(Color.BLACK)
.fillColor(Color.rgb( 229, 204, 229))
.strokeWidth(3);
return mMap.addCircle(options);
}
There are two circles drawn, i want first circle to be removed as soon as the second circle is drawn.
private Circle circle;
Before drawing a circle to the map. Check first if circle is drawn. Just remove the circle then draw new circle to the map.
if(circle!=null){
circle.remove();
}
circle = drawCircle(new LatLng(mlatitude, mlongitude));
1.In My app Gps LatLong is Getting from Server Every OneMinute. Saved in Shared Pref ,then getting the LatLong From shared pref Showing the Marker on the Map.
2.Every One Minute I want to Move the Marker based on the LatLong.
3.But While Changing the Marker Location. Getting Duplicates.
Please Help me to Solve this Issue.
Inside Oncreate method i Called below Snippet in Every 60 Secs for Calling a Method.
try
{
Thread t = new Thread()
{
#Override
public void run()
{
try
{
while (!isInterrupted())
{
Thread.sleep(60*1000);
getActivity().runOnUiThread(new Runnable()
{
#Override
public void run()
{
display_Location();
Log.i("Every 60 Second","Current Called..");
}
});
}
} catch (Exception e)
{
e.printStackTrace();
}
}
};
t.start();
}
catch (Exception e)
{
e.printStackTrace();
}
Method Iam USing:
private void display_Location()
{
try
{
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null)
{
/*For Current Location ping Starts Here*/
// get user data from session
HashMap<String, String> user = session.getGPSPING();
// UserLat
String LatLongUser = "";
LatLongUser = user.get(SessionManagerFor_Register.KEY_LATLONG);
if (!LatLongUser.equals("") || LatLongUser != null)
{
Log.i(" PING on MAP LatLong", LatLongUser);
String[] LanlongArr = LatLongUser.split("//");
List<String> Lanlonglist1 = Arrays.asList(LanlongArr);
int length = Lanlonglist1.size();
/*ArrayList For adding All ArrayList items in Single(Concating)*/
arraylist_DetailLineWalker = new ArrayList<String>(length);
for (int i = 0; i < length; i++) {
arraylist_DetailLineWalker.add(Lanlonglist1.get(i)
);
}
if (arraylist_DetailLineWalker != null)
{
// Initializing
LineWalkermMarkers_arr = new ArrayList<Marker>();
// just Remove Older Line Wlaker
if (LineWalkermMarkers_arr != null) {
// LineWalker_marker1.remove();
RemoveLineWalkerMarkers();
Log.i(TAG, "LineWalker REMOVED.............................");
}
for (int i = 0; i < arraylist_DetailLineWalker.size(); i++)
{
try
{
String Val = arraylist_DetailLineWalker.get(i).toString();
//Log.i(" Validation Id",Val);
VALUE_ARRAY_STRING = Val.toString().split("::");
LatLong_DataSaveTable = VALUE_ARRAY_STRING[0].toString();
System.out.println("checking STarted LatLong::" + LatLong_DataSaveTable);
String[] latlong = LatLong_DataSaveTable.split(",");
double latitude1 = Double.parseDouble(latlong[0]);
double longitude2 = Double.parseDouble(latlong[1]);
//To hold location
LatLng latLng1 = new LatLng(latitude1, longitude2);
//To create marker in map
MarkerOptions markerOptionsLineWalker = new MarkerOptions();
markerOptionsLineWalker.position(latLng1); //setting position
markerOptionsLineWalker.draggable(true); //Making the marker draggable
markerOptionsLineWalker.title("USER LOCAITON");
markerOptionsLineWalker.icon(BitmapDescriptorFactory.fromResource(R.drawable.walker_outof_fence_icon_red));
//adding marker to the map
// googleMap.addMarker(markerOptionsLineWalker);
LineWalker_marker1 = googleMap.addMarker(markerOptionsLineWalker);
LineWalkermMarkers_arr.add(LineWalker_marker1);
// LineWalker_marker1.setPosition(latLng1);
Log.i(TAG, " NEW Line Walkers PING Added.............................");
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
} else {
Log.i("MAP NEwLatLong", "TOTAL ARRY LIST NULLL");
}
}
else
{
Log.i("MAP NEwLatLong", "Null Not LatLong");
Toast.makeText(getActivity(), "Lat Long Not Available..!", Toast.LENGTH_SHORT).show();
}
}
else
{
Log.i("Location EXception", "Couldn't get the location. Make sure location is enabled on the device");
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
/*Remove the Linewalker*/
private void RemoveLineWalkerMarkers()
{
for (Marker marker: LineWalkermMarkers_arr)
{
marker.remove();
}
LineWalkermMarkers_arr.clear();
}
if(arraylist_DetailLineWalker != null && arraylist_DetailLineWalker.size()>0){
arraylist_DetailLineWalker.clear()
mMap.clear();
showMarker();
}
You are calling RemoveLineWalkerMarkers() after initializing LineWalkermMarkers_arr doing LineWalkermMarkers_arr = new ArrayList<Marker>();, so you are never removing your markers.
Just initialize your LineWalkermMarkers_arr after removing the markers:
if (LineWalkermMarkers_arr != null) {
RemoveLineWalkerMarkers();
Log.i(TAG, "LineWalker REMOVED.............................");
}
LineWalkermMarkers_arr = new ArrayList<Marker>();
As a side note, you should follow the Java code conventions (variables and method names should start with lowercase). You can find good guides here and here
Solution is just a logic change
Initialize Marker only once , either onCreate or some other method according to your logic
If the markers are multiple, then re-initialization should be done once data is received.
Created markers can be cleared with below logic
if(mGoogleMap != null) {
mGoogleMap.clear();
}
Either reuse this marker to move from last location to new location. Or recreate all the markers, once data is received
//With your logic , this check should be done
if(arraylist_DetailLineWalker.size()>0){
RemoveLineWalkerMarkers();
}
LineWalkermMarkers_arr = new ArrayList<Marker>();
for (int i = 0; i < arraylist_DetailLineWalker.size(); i++)
{
}
Alternative easy method to move single marker , to show live driving direction kind of feature
private Marker mCurrentMarker;
private float ZOOMLEVEL=18.0f;
private LatLng previousLatLon;
private Handler mLocalHandler;
private GoogleMap mGoogleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLocalHandler = new Handler();
previousLatLon=new LatLng(45.320372, 2.460161);
//Initialize Marker once Google Map object is created
mMarkerOptions = new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.custom_marker_icon));
mMarkerOptions.position(previousLatLon);
mCurrentMarker = mGoogleMap.addMarker(mMarkerOptions);
}
/**
* Call this method to move marker in map to new location in map with updated location
* #param marker
* #param toPosition
* #param fromPosition
*/
public void animateMarker(final Marker marker, final LatLng toPosition,final LatLng fromPosition) {
final long duration = 500;
final Interpolator interpolator = new LinearInterpolator();
mLocalHandler.post(new Runnable() {
#Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - mStartTime;
float t = interpolator.getInterpolation((float) elapsed
/ duration);
marker.setPosition(toPosition);
marker.setAnchor(Constants.MAPANCHOR, Constants.MAPANCHOR);
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(toPosition, ZOOMLEVEL));
if (t < 1.0) {
// Post again 16ms later.
mLocalHandler.postDelayed(this, 16);
} else {
marker.setVisible(true);
}
}
}
});
previousLatLon=toPosition;// reassign the previous location to current location
}
I'm using Indoor Atlas for an educational project based on Android.
I'd like to implement search functionality into a map that I loaded in Indoor Atlas environment, how I can this?
For example I'd like to search a room of specific, apartment, how I can implement this searching functionality?
Thanks for your time
You should mapping the location firstly.
And do test path for making your map more precise.
There is official app of IndoorAtlas, so you can use it.
First you need to map your floor using indooratlas app i.e IndoorAtlas MapCreator 2. Then you can record the coordinates using that app and then you can save the coordinates of the rooms/areas in your server and then can fetch and point it on maps after search.
/*
gimal.adobe.appsbee.com.gimbaladobe.Indooratlasmap
16/12/16
sumit-chakraborty
*/
public class IndoorAtlasMap implements OnMapReadyCallback, GoogleMap.InfoWindowAdapter, GoogleMap.OnInfoWindowClickListener {
private static final String TAG = "IndoorAtlasExample";
private IndoorAtlasCallBack mIndoorAtlasCallBack;
SupportMapFragment mapFragment;
private static final float HUE_IABLUE = 200.0f;
/* used to decide when bitmap should be downscaled */
private static final int MAX_DIMENSION = 1048;
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
private Marker mMarker;
private Marker mMarkerLoc;
private GroundOverlay mGroundOverlay;
private IALocationManager mIALocationManager;
private IAResourceManager mResourceManager;
private IATask<IAFloorPlan> mFetchFloorPlanTask;
private static LatLng latlongfromlistner;
private Target mLoadTarget;
private boolean mCameraPositionNeedsUpdating;
private ArrayList<ListLatlong> latlong;
LayoutInflater inflater = null;
private TextView textViewTitle;
private RelativeLayout rl_custominfo;
public IndoorAtlasMap(IndoorAtlasCallBack mIndoorAtlasCallBack, LayoutInflater inflater) {
this.mIndoorAtlasCallBack = mIndoorAtlasCallBack;
this.inflater = inflater;
}
/**
* Listener that handles location change events.
*/
private IALocationListener mListener = new IALocationListenerSupport() {
/**
* Location changed, move marker and camera position.
*/
#Override
public void onLocationChanged(IALocation location) {
Log.d(TAG, "new location received with coordinates: " + location.getLatitude()
+ "," + location.getLongitude());
System.out.println("!!!!!!aaa" + location.toString());
if (mMap == null) {
// location received before map is initialized, ignoring update here
return;
}
latlongfromlistner = new LatLng(location.getLatitude(), location.getLongitude());
if (mMarker == null) {
// first location, add marker
mMarker = mMap.addMarker(new MarkerOptions().position(latlongfromlistner)
.icon(BitmapDescriptorFactory.defaultMarker(HUE_IABLUE)));
} else {
// move existing markers position to received location
mMarker.setPosition(latlongfromlistner);
}
// our camera position needs updating if location has significantly changed
if (mCameraPositionNeedsUpdating) {
//Create a new CameraPosition
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latlongfromlistner, 19.0f));
mCameraPositionNeedsUpdating = false;
}
mMarker.hideInfoWindow();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status) {
case IALocationManager.STATUS_CALIBRATION_CHANGED:
String quality = "unknown";
switch (extras.getInt("quality")) {
case IALocationManager.CALIBRATION_POOR:
quality = "Poor";
break;
case IALocationManager.CALIBRATION_GOOD:
quality = "Good";
break;
case IALocationManager.CALIBRATION_EXCELLENT:
quality = "Excellent";
break;
}
MyLog.d(TAG, "Calibration change. Quality: " + quality);
break;
case IALocationManager.STATUS_AVAILABLE:
MyLog.d(TAG, "onStatusChanged: Available");
break;
case IALocationManager.STATUS_LIMITED:
MyLog.d(TAG, "onStatusChanged: Limited");
break;
case IALocationManager.STATUS_OUT_OF_SERVICE:
MyLog.d(TAG, "onStatusChanged: Out of service");
break;
case IALocationManager.STATUS_TEMPORARILY_UNAVAILABLE:
MyLog.d(TAG, "onStatusChanged: Temporarily unavailable");
}
}
};
/**
* Region listener that when:
* <ul>
* <li>region has entered; marks need to move camera and starts
* loading floor plan bitmap</li>
* <li>region has existed; clears marker</li>
* </ul>.
*/
private IARegion.Listener mRegionListener = new IARegion.Listener() {
#Override
public void onEnterRegion(IARegion region) {
System.out.println("!!!!!!aaa enter" + region.toString());
mIndoorAtlasCallBack.hideLoading();
if (region.getType() == IARegion.TYPE_UNKNOWN) {
mIndoorAtlasCallBack.showToast("Moved out of map");
return;
}
// entering new region, mark need to move camera
mCameraPositionNeedsUpdating = true;
final String newId = region.getId();
mIndoorAtlasCallBack.showToast(newId);
fetchFloorPlan(newId);
}
#Override
public void onExitRegion(IARegion region) {
System.out.println("!!!!!!aaa exit" + region.toString());
if (mMarker != null) {
mMarker.remove();
mMarker = null;
}
}
};
public void onCreatePoints(ArrayList<ListLatlong> latlong) {
this.latlong = latlong;
if (null != latlong) {
for (int i = 0; i < latlong.size(); i++) {
String title = latlong.get(i).getLoc() != null ? latlong.get(i).getLoc() : "adobe";
mMarkerLoc = mMap.addMarker(new MarkerOptions().position(latlong.get(i).getLatLng())
.title(title)
.snippet(String.valueOf(latlong.get(i).getId()))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
}
}
}
/**
* Sets bitmap of floor plan as ground overlay on Google Maps
*/
private void setupGroundOverlay(IAFloorPlan floorPlan, Bitmap bitmap) {
if (mGroundOverlay != null) {
mGroundOverlay.remove();
}
if (mMap != null) {
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(bitmap);
IALatLng iaLatLng = floorPlan.getCenter();
LatLng center = new LatLng(iaLatLng.latitude, iaLatLng.longitude);
GroundOverlayOptions fpOverlay = new GroundOverlayOptions()
.image(bitmapDescriptor)
.position(center, floorPlan.getWidthMeters(), floorPlan.getHeightMeters())
.bearing(floorPlan.getBearing());
mGroundOverlay = mMap.addGroundOverlay(fpOverlay);
}
}
/**
* Download floor plan using Picasso library.
*/
private void fetchFloorPlanBitmap(final IAFloorPlan floorPlan) {
final String url = floorPlan.getUrl();
//saving floor level and floor name
App.getInstance().getAppPreferences().setFloorId(floorPlan.getFloorLevel());
App.getInstance().getAppPreferences().setFloorName(floorPlan.getName());
if (mLoadTarget == null) {
mLoadTarget = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Log.d(TAG, "onBitmap loaded with dimensions: " + bitmap.getWidth() + "x"
+ bitmap.getHeight());
setupGroundOverlay(floorPlan, bitmap);
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
// N/A
}
#Override
public void onBitmapFailed(Drawable placeHolderDraweble) {
mIndoorAtlasCallBack.showToast("Failed to load bitmap");
mIndoorAtlasCallBack.hideLoading();
}
};
}
if (null != mIndoorAtlasCallBack.getContext()) {
RequestCreator request = Picasso.with(mIndoorAtlasCallBack.getContext()).load(url);
final int bitmapWidth = floorPlan.getBitmapWidth();
final int bitmapHeight = floorPlan.getBitmapHeight();
if (bitmapHeight > MAX_DIMENSION) {
request.resize(0, MAX_DIMENSION);
} else if (bitmapWidth > MAX_DIMENSION) {
request.resize(MAX_DIMENSION, 0);
}
request.into(mLoadTarget);
}
mIndoorAtlasCallBack.onSuccessIndoorMapLoad();
}
/**
* Fetches floor plan data from IndoorAtlas server.
*/
private void fetchFloorPlan(String id) {
// if there is already running task, cancel it
cancelPendingNetworkCalls();
final IATask<IAFloorPlan> task = mResourceManager.fetchFloorPlanWithId(id);
task.setCallback(new IAResultCallback<IAFloorPlan>() {
#Override
public void onResult(IAResult<IAFloorPlan> result) {
if (result.isSuccess() && result.getResult() != null) {
// retrieve bitmap for this floor plan metadata
fetchFloorPlanBitmap(result.getResult());
} else {
// ignore errors if this task was already canceled
if (!task.isCancelled()) {
// do something with error
mIndoorAtlasCallBack.showToast("loading floor plan failed: ");
// remove current ground overlay
if (mGroundOverlay != null) {
mGroundOverlay.remove();
mGroundOverlay = null;
}
}
}
}
}, Looper.getMainLooper()); // deliver callbacks using main looper
// keep reference to task so that it can be canceled if needed
mFetchFloorPlanTask = task;
}
/**
* Helper method to cancel current task if any.
*/
private void cancelPendingNetworkCalls() {
if (mFetchFloorPlanTask != null && !mFetchFloorPlanTask.isCancelled()) {
mFetchFloorPlanTask.cancel();
}
}
#Override
public void onMapReady(GoogleMap mMap) {
this.mMap = mMap;
mMap.setInfoWindowAdapter(this);
mMap.setOnInfoWindowClickListener(this);
}
public void onDestroyView() {
mIALocationManager.removeLocationUpdates(mListener);
mIALocationManager.registerRegionListener(mRegionListener);
onDestroy();
}
public void onDestroy() {
mIALocationManager.destroy();
System.out.println("!!!!!!aaa" + "onDestroyView");
if (null != mMarker) {
mMarker.remove();
}
if (null != mMarkerLoc) {
mMarkerLoc.remove();
}
mIndoorAtlasCallBack.hideLoading();
}
public void onPause() {
mIndoorAtlasCallBack.hideLoading();
}
public void onResume() {
mIALocationManager.requestLocationUpdates(IALocationRequest.create(), mListener);
mCameraPositionNeedsUpdating = true;
}
public void onIndoorAtlasInit(int map_container) {
mIndoorAtlasCallBack.showLoading(mIndoorAtlasCallBack.getContext().getResources().getString(R.string.loading_msg));
FragmentManager fm = mIndoorAtlasCallBack.getChildFragmentManager();
mapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map_container);
if (mapFragment == null) {
mapFragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(map_container, mapFragment).commit();
}
mapFragment.getMapAsync(this);
Bundle extras = new Bundle(2);
extras.putString(IALocationManager.EXTRA_API_KEY,
App.getInstance().getAppPreferences().getApiKey());
extras.putString(IALocationManager.EXTRA_API_SECRET,
App.getInstance().getAppPreferences().getSecretkey());
mIALocationManager = IALocationManager.create(mIndoorAtlasCallBack.getContext(), extras);
final String floorPlanId = App.getInstance().getAppPreferences().getFloorPlanId();
if (!TextUtils.isEmpty(floorPlanId)) {
final IALocation location = IALocation.from(IARegion.floorPlan(floorPlanId));
mIALocationManager.setLocation(location);
}
mResourceManager = IAResourceManager.create(mIndoorAtlasCallBack.getContext(), extras);
mIALocationManager.requestLocationUpdates(IALocationRequest.create(), mListener);
mIALocationManager.registerRegionListener(mRegionListener);
}
#Override
public View getInfoWindow(final Marker marker) {
View v = inflater.inflate(R.layout.mapinfowindow, null);
if (marker != null && (marker.getTitle() != null && marker.getTitle().length() > 0)) {
MyLog.sout("!!!!oninfowindow", marker.getSnippet());
textViewTitle = (TextView) v.findViewById(R.id.textViewTitle);
rl_custominfo = (RelativeLayout) v.findViewById(R.id.rl_custominfo);
textViewTitle.setText(marker.getTitle());
textViewTitle.setVisibility(View.VISIBLE);
rl_custominfo.setVisibility(View.VISIBLE);
} else{
return null;
}
return (v);
}
#Override
public View getInfoContents(Marker marker) {
return null;
}
#Override
public void onInfoWindowClick(Marker marker) {
MyLog.sout("!!!!oninfo", marker.getSnippet());
LatLng latlng = marker.getPosition();
Double distance = CalculateDistancex.CalculationByDistance(latlng,latlongfromlistner) ;
distance = distance*100;
distance = Double.valueOf(Math.round(distance));
distance = distance /100;
if(null !=marker) {
mIndoorAtlasCallBack.onClickFromInfoWindow(marker.getSnippet(), distance);
}
}
}
I have a timer.it is running every 5 minute.ı write a method called konumlarıAl in timer run method. this method get locations data from database.When konumlarıAl run,HaritaKonumGoster is method calling.I want to delete all markers and show new location data marker on map without refreshing page.
my code
private void HaritaKonumGoster() {
// TODO Auto-generated method stub
if (googleHarita == null) {
googleHarita = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.haritafragment))
.getMap();
if (googleHarita != null) {
googleHarita.clear();
if(mrks.size()!=0)
{
for (Marker marker: mrks) {
marker.remove();
}
mrks.clear();
}
googleHarita.setMyLocationEnabled(true);
LocationManager locationManager=(LocationManager)getSystemService(LOCATION_SERVICE);
Criteria criteria=new Criteria();
String provider =locationManager.getBestProvider(criteria, true);
Location mylocation=locationManager.getLastKnownLocation(provider);
double latitude=0;
double longitude=0;
double mylatitude=0;
double myLongtitude=0;
//double latitude=enlem;
//double longitude=boylam;
if (mylocation != null){
mylatitude=mylocation.getLatitude();
myLongtitude=mylocation.getLongitude();
}
BitmapDescriptor bitmapDescriptor
= BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN);
try{
for (int i = 0; i < degiskenler.taksici.size(); i++) {
latitude=Double.parseDouble(degiskenler.taksici.get(i).enlem.trim());
longitude=Double.parseDouble(degiskenler.taksici.get(i).boylam.trim());
LatLng istanbulKoordinat = new LatLng(latitude,longitude);
Marker m= googleHarita.addMarker(new MarkerOptions().position(istanbulKoordinat).title("Kız Kulesi").icon(bitmapDescriptor));
googleHarita.moveCamera(CameraUpdateFactory.newLatLngZoom(istanbulKoordinat, 7));
mrks.add(m);
// Toast.makeText(getBaseContext(),"harita :)",Toast.LENGTH_LONG).show();
}
}
catch(Exception exception) {
Toast.makeText(getBaseContext(),"harita olmadı",Toast.LENGTH_LONG).show();
}
googleHarita.addMarker(new MarkerOptions().position(new LatLng(mylatitude, myLongtitude)).title("you hereeee"));
}
}
}
location update every 5 minute method
private void LocationUpdateEvery5minute() {
// TODO Auto-generated method stub
zamanlayici = new Timer();
yardimci = new Handler(Looper.getMainLooper());
zamanlayici.scheduleAtFixedRate(new TimerTask()
{
#Override
public void run(){
yardimci.post(new Runnable()
{
public void run()
{ Toast.makeText(getBaseContext(), "timera girdi",Toast.LENGTH_SHORT).show();
konumlarıAl();
}
});
}
}, 0, ZAMAN);
}
Delete markers using
googleHarita.clear()
https://developer.android.com/reference/com/google/android/gms/maps/GoogleMap.html#clear()
Just one call to this method should be enough to remove all the markers on your map.
Then, and add markers using
googleHarita.addMarker()
http://developer.android.com/reference/com/google/android/gms/maps/model/Marker.html
example here
I don't think you need to refresh any pages.
You have to use map.clear(); which will help you to removes all markers, polylines, polygons, overlays, etc from the map.