I have a GEOJSON (I could convert it into Shapefile or another georeferenced file) with many points (a few hundreds) and I want to create geofences on all of them. How do I do this? I have the whole code to get a geofence of one point but how do I create geofences on many points?
When clicking long on the screen, a marker will be added which gets automatically a geofence
public void onMapLongClick(LatLng latLng) { // lange Klicken, bis Marker scheint
if (Build.VERSION.SDK_INT >= 29) {
// We need background permission (Manifest.xml)
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION) == PackageManager.PERMISSION_GRANTED) { // wenn permission granted
tryAddingGeofence(latLng);
} else {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION)){
//We show a dialog and ask for permission
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.ACCESS_BACKGROUND_LOCATION}, BACKGROUND_LOCATION_ACCESS_REQUEST_CODE);
} else {
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.ACCESS_BACKGROUND_LOCATION}, BACKGROUND_LOCATION_ACCESS_REQUEST_CODE);
}
}
} else {
tryAddingGeofence(latLng);
}
}
private void tryAddingGeofence(LatLng latLng) {
mMap.clear();
addMarker(latLng);
addCircle(latLng, GEOFENCE_RADIUS);
addGeofence(latLng, GEOFENCE_RADIUS);
}
private void addGeofence(LatLng latLng, float radius){
Geofence geofence = geofenceHelper.getGeofence(GEOFENCE_ID, latLng, radius,
Geofence.GEOFENCE_TRANSITION_ENTER |
// Geofence.GEOFENCE_TRANSITION_DWELL |
Geofence.GEOFENCE_TRANSITION_EXIT); // wann wird geofence getriggert? -> reinlaufen, darin laufen oder rausgehen
GeofencingRequest geofencingRequest = geofenceHelper.getGeofencingRequest(geofence);
PendingIntent pendingIntent = geofenceHelper.getPendingIntent();
geofencingClient.addGeofences(geofencingRequest, pendingIntent)
.addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "onSuccess: Geofence Added...");
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
String errorMessage = geofenceHelper.getErrorString(e);
Log.d(TAG, "onFailure: " + errorMessage);
}
});
}
private void addMarker(LatLng latLng) {
MarkerOptions markerOptions = new MarkerOptions().position(latLng);
mMap.addMarker(markerOptions);
}
private void addCircle(LatLng latLng, float radius){
CircleOptions circleOptions = new CircleOptions();
circleOptions.center(latLng);
circleOptions.radius(radius);
circleOptions.strokeColor(Color.argb(255,255,0,0));
circleOptions.fillColor(Color.argb(64,255,0,0));
circleOptions.strokeWidth(4);
mMap.addCircle(circleOptions);
} ```
I found an easy solution for anyone who will have this problem:
We need to create a list of all the coordinates. So at first we need to get the coordinates from the geojson file. In the followed solution I had a KML file but at the end it gets the same result.
I created a method which reads a txt file (you have to convert geojson or kml into .txt first and then copy it into src/main/res/raw. In my case I have "monitoring.txt" So dont forget to change the name into your filename.)
In the first part it reads the .txt and saves it into a String.
In the scond part it reads from it that String that is in between coordinates> and </coordinates. "(.*?)" shows that it takes everything that is between. When you have a GeoJson remember to look between what Strings the coordinates are. Then I take this String (means the coordinates), split them and parse Latitude and Longitude into double so that I can then safe it as LatLng Object. It has to be in a loop so that every coordinate in the kml/geojson/txt file will be taken and stored into the list "dangerousArea".
private List<LatLng> dangerousArea = new ArrayList<>();
private void readCoordinatesFromKml() throws IOException {
InputStream inputStream = getResources().openRawResource(R.raw.monitoring);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i;
try {
i = inputStream.read();
while (i != -1)
{
byteArrayOutputStream.write(i);
i = inputStream.read();
}
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
Pattern pattern = Pattern.compile("<coordinates>(.*?)</coordinates>", Pattern.DOTALL);
Matcher matcher = pattern.matcher(byteArrayOutputStream.toString());
while (matcher.find()) {
String[] str = matcher.group(1).split(",");
double Longitude = Double.parseDouble(str[0]);
double Latitude = Double.parseDouble(str[1]);
dangerousArea.add(new LatLng(Latitude, Longitude));
}
}
Then you can just add the coordinates which are stored in the list as Geofences. This depends on what you called your method which creates a Geofence around your points:
dangerousArea.forEach(coordinate -> {tryAddingGeofence(coordinate);});
For the last thing you have to look at your code where to do this. Normally it is under a function which already made sure that you are allowed to get the location of the user.
Related
I'm building an Android app that uses custom map tiles. I started out with a set that worked fine but only had limited zoom levels and area. Then I switched to another tile set that covered much more area but simply refuses to display in Android. I can see them in the Javascript Maps and there are no errors in my LogCat so I'm not sure what's going on.
Are there only certain image formats that the Android Map overlays support?
For reference, here are the two tile sets:
This one works both on the web and in Android:
http://mooproductions.org/vfrs/local.html
This one works on the web but simply doesn't render on Android:
http://mooproductions.org/vfrs/national.html
Here's he code I'm using to provide the tile sets:
public class VfrTileProvider extends UrlTileProvider
{
// private static final String BASE_URL = "http://www.mooproductions.org/vfrs/%d/%d/%d.png";
// private static final String BASE_URL = "http://mw1.google.com/mw-planetary/lunar/lunarmaps_v1/clem_bw/%d/%d/%d.jpg";
private static final String BASE_URL = "http://vfrmap.com/20170914/tiles/vfrc/%d/%d/%d.jpg";
public VfrTileProvider (int width, int height)
{
super(width, height);
}
#Override
public URL getTileUrl (int x, int y, int zoom)
{
int reversedY = (1 << zoom) - y - 1;
// String tileUrl = String.format(Locale.US, BASE_URL, zoom, x, reversedY);
String tileUrl = String.format(Locale.US, BASE_URL, zoom, reversedY, x);
Log.d("Tile Provider", tileUrl);
URL url = null;
try { url = new URL(tileUrl); }
catch (MalformedURLException e) { e.printStackTrace(); }
return url;
}
}
You can see commented out are the other URLs for tile sets that I use that work just fine. The creation of the tileUrl is a little different because this new tile set transposes the x and y in their url.
Here is the code I use to display the custom tiles:
#Override
public void onMapReady (GoogleMap googleMap)
{
_map = googleMap;
_map.setMapType(GoogleMap.MAP_TYPE_NONE);
VfrTileProvider tileProvider = new VfrTileProvider(256, 256);
_map.addTileOverlay(new TileOverlayOptions().tileProvider(tileProvider));
Criteria criteria = new Criteria();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION }, REQUEST_LOCATION_PERMISSION);
return;
}
Location location = _locationManager.getLastKnownLocation(_locationManager.getBestProvider(criteria, false));
if (location != null)
{
_map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 13));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(location.getLatitude(), location.getLongitude()))
.zoom(10)
.build();
_map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
This is because wrong "User-Agent" property settings of URL url object in VfrTileProvider.getTileUrl(). You can set it to correct by setRequestProperty():
URL url = null;
try {
url = new URL(tileUrl);
try {
url.openConnection().setRequestProperty("User-Agent","<correct user agent name>");
} catch (IOException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
throw new AssertionError(e);
}
or just add System.setProperty("http.agent", ""); call to your public void onMapReady ():
#Override
public void onMapReady (GoogleMap googleMap)
{
System.setProperty("http.agent", "");
_map = googleMap;
...
}
I prefer second (System.setProperty("http.agent", "");) case.
I have previously asked the similar question where I had issue with saving the data in the firebase cloud. I managed to store the latitude & longitude data in the firebase database using following code
#Override
public void onMapReady(GoogleMap map) {
mMap = map;
// Use a custom info window adapter to handle multiple lines of text in the
// info window contents.
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
#Override
// Return null here, so that getInfoContents() is called next.
public View getInfoWindow(Marker arg0) {
return null;
}
#Override
public View getInfoContents(Marker marker) {
// Inflate the layouts for the info window, title and snippet.
View infoWindow = getLayoutInflater().inflate(R.layout.custom_info_contents,
(FrameLayout)findViewById(R.id.map), false);
TextView title = ((TextView) infoWindow.findViewById(R.id.title));
title.setText(marker.getTitle());
TextView snippet = ((TextView) infoWindow.findViewById(R.id.snippet));
snippet.setText(marker.getSnippet());
return infoWindow;
}
});
// Turn on the My Location layer and the related control on the map.
updateLocationUI();
// Get the current location of the device and set the position of the map.
getDeviceLocation();
}
// Gets the current location of the device, and positions the map's camera.
private void getDeviceLocation() {
/*
* Request location permission, so that we can get the location of the
* device. The result of the permission request is handled by a callback,
* onRequestPermissionsResult.
*/
if (ContextCompat.checkSelfPermission(this.getApplicationContext(),
android.Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mLocationPermissionGranted = true;
} else {
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
}
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference databaseReference = database.getReference();
Intent intent = getIntent();
callingActivity = intent.getIntExtra( "callingActivity",0 );
//If the map has been called from ScanActivity
if (callingActivity == 1) {
// Get the best and most recent location of the device
if (mLocationPermissionGranted) {
mLastKnownLocation = LocationServices.FusedLocationApi
.getLastLocation( mGoogleApiClient );
scanString = intent.getStringArrayListExtra( "beaconList" );
nameList = intent.getStringArrayListExtra( "nameList" );
addressList = intent.getStringArrayListExtra( "addressList" );
RssiList = intent.getIntegerArrayListExtra( "RssiList" );
for ( int i=0; i < scanString.size(); i++) {
addBeacon.name = nameList.get( i );
addBeacon.address = addressList.get( i );
addBeacon.Rssi = RssiList.get( i );
addBeacon.latitude = mLastKnownLocation.getLatitude();
addBeacon.longitude = mLastKnownLocation.getLongitude();
databaseReference.child( "foo" ).child(addBeacon.address).setValue( addBeacon);
}
}
}
But the problem is when I try to retrieve the location I see is my device's location instead of the saved latlong value in the firebase data base. I have 2 calling activities i.e. 1 is for storing the lat long to firebase and 2 for retrieving it. I can successfully save the value under unique id and update it whereas I am unable to retrieve the latlong. I am using following code for retrieval
else {
DatabaseReference latlong= FirebaseDatabase.getInstance().getReference().child( "foo" ).child( "08:9E:08:B4:57:18" );
mFirebaseDatabase.keepSynced(true);
al ValueEventListener beaconListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
LatLng newLocation = new LatLng(
dataSnapshot.child("latitude").getValue(Long.class),
dataSnapshot.child("longitude").getValue(Long.class));
//LatLng mRetrieved = new LatLng(dataSnapshot.getValue(beacon.class).latitude, dataSnapshot.getValue(beacon.class).longitude);
//mLastKnownLocation.setLatitude( dataSnapshot.getValue(beacon.class).latitude );
// mLastKnownLocation.setLatitude(60.192059);
mMap.addMarker( new MarkerOptions()
.position( newLocation)
.title( dataSnapshot.getKey()));
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
};
databaseReference.addValueEventListener( beaconListener );
Here i am changing the lat long value of 1 particular child name "08:9E:08:B4:57:18" manually in database to check if i can see the marker in the map at that location but it just shows my device's current location.
I can provide further screen shot of my database n the application if required. Thanks in advance. Hope to have some valuable pointers.
Please add this code after adding marker, so that the marker will move to the location which you added
try {
CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(lat, lng));
CameraUpdate zoom = CameraUpdateFactory.zoomTo(12);
googleMap.moveCamera(center);
googleMap.animateCamera(zoom);
} catch (Exception e) {
Log.getStackTraceString(e);
}
I'm new at android development. I'm trying to add a Google marker at my App map but it won't show. I can set a Marker if the Marker's lat & lng is a double number,but when i put the API data in it it won't show.Any suggestion? Thanks a lot.
#Override
public void onMapReady(final GoogleMap googleMap) {
this.googleMap = googleMap;
// Add a marker in Sydney and move the camera
getLocation();
// This marker will show at my app screen !
LatLng latLng = new LatLng(24.9992666, 121.5082287);
googleMap.addMarker(new MarkerOptions().position(latLng)
.title("This is office Marker")
);
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
this.googleMap.setMyLocationEnabled(true);
HttpUtil.sendHttpRequest("http://113.10.198.159/appapi/getWIFIList", new HttpCallbackListener() {
#Override
public void onFinish(String response) {
Gson gson = new Gson();
JsonBean jsonBean = gson.fromJson(response, JsonBean.class);
Log.d(TAG, "【】【id】【】" + jsonBean.getResult().get(0).getId());
Log.d(TAG, "【】【merchant_id】【】" + jsonBean.getResult().get(0).getMerchant_id());
Log.d(TAG, "【】【merchant_name】【】" + jsonBean.getResult().get(0).getMerchant_name());
Log.d(TAG, "【】【city】【】" + jsonBean.getResult().get(0).getCity());
Log.d(TAG, "【】【area】【】" + jsonBean.getResult().get(0).getArea());
Log.d(TAG, "【】【address】【】" + jsonBean.getResult().get(0).getAddress());
Log.d(TAG, "【】【lat】【】" + jsonBean.getResult().get(0).getLat());
Log.d(TAG, "【】【lng】【】" + jsonBean.getResult().get(0).getLng());
Log.d(TAG, "【】【addTime】【】" + jsonBean.getResult().get(0).getAddTime());
Log.d(TAG, "【】【dlat】【】" + jsonBean.getResult().get(0).getDlat());
Log.d(TAG, "【】【dlng】【】" + jsonBean.getResult().get(0).getDlng());
Log.d(TAG, "【】【wificode】【】" + jsonBean.getResult().get(0).getWificode());
Log.d(TAG, "【】【upstream】【】" + jsonBean.getResult().get(0).getUpstream());
Log.d(TAG, "【】【downstream】【】" + jsonBean.getResult().get(0).getDownstream());
//// This marker can not show at my app screen
LatLng latLng = new LatLng(jsonBean.getResult().get(0).getDlat(), jsonBean.getResult().get(0).getDlng());
Marker marker = googleMap.addMarker(new MarkerOptions().position(latLng)
.title("This is Test Marker")
);
}
#Override
public void onError(Exception e) {
}
});
}
public class HttpUtil {
public static void sendHttpRequest(final String address, final HttpCallbackListener listener) {
new Thread(new Runnable() {
#Override
public void run() {
try {
OkHttpClient client = new OkHttpClient();
String s1 = "lat";
String s2 = "24.9992666";
String s3 = "lng";
String s4 = "121.5082287";
RequestBody requestBody = new FormBody.Builder()
.add(s1, s2)
.add(s3, s4)
.build();
Request request = new Request.Builder()
.url(address)
.post(requestBody) //post
.build();
Response response = client.newCall(request).execute();
String responseData = response.body().string();
if (listener != null) {
// onFinish() method
listener.onFinish(responseData);
}
} catch (Exception e) {
if (listener != null) {
// onError() method
listener.onError(e);
}
}
}
}).start();
}
}
Is the map shoiwing up? What is inside your
getLocation();
function, if there is not something like the following, then add it
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
Also remember to zoom in and out and pan the map around, may be the marker is plotted where you did not expect it to be.
if it still does not work, follow this tutorial, it must work.
google maps with marker
Are you sure you're getting the right value from the json? It may be returning a string. You might want to try converting it to the valueOf to get the desired type.
For a double it would be something like:
double lat = Double.valueOf(jsonBean.getResult().get(0).getDlat());
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 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;
}
});
}
}
});
}