Adding Multiple Markers Google Maps - android

I am trying to add a marker all depending on what activity the user is on. For example, if the user is in the location1 activity and clicks the button to open maps, it should open Google Maps with a marker at where location1 is. Alternatively, if the user is in the location2 activity and clicks the button to open maps, it should open Google Maps with a marker at where location 2 is.
I have it working when they click on one activity, it brings them to Google Maps and has a marker at where that location is. I have simply tried to copy the code and paste it below with the edited names in it, but if I click a different activity to go to maps, it brings me to the same marker as previously.
My Code for GoogleMapsActivity is below:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// 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);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
//mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
// Add a marker at the Oval and move the camera
LatLng oval = new LatLng(53.3484013, -6.2605243);
mMap.addMarker(new MarkerOptions().position(oval).title("Oval Pub"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(oval));
/*
// Add a marker at Diceys and move the camera
LatLng diceys = new LatLng(53.3358088,-6.2636688);
mMap.addMarker(new MarkerOptions().position(diceys).title("Diceys Nightclub"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(diceys));
*/
}
public void changeType(View view)
{
if(mMap.getMapType() == GoogleMap.MAP_TYPE_NORMAL)
{
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
}
else
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}}
As you can see, I have commented out the code where I have tried adding a marker for a different location, but it seems to bring me to the same location as above.
Not sure if this is something simple or not as I am new to Google Maps in Android.
Any help would be greatly appreciated.
Thank you.

ArrayList<MarkerData> markerArray = new ArrayList<MarkerData>();
for(int i = 0 ; i < markersArray.size() ; i++ ) {
createMarker(markersArray.get(i).getLatitude(), markersArray.get(i).getLongitude(), markersArray.get(i).getTitle(), markersArray.get(i).getSnippet(), markersArray.get(i).getIconResID());
}
....
protected void createMarker(double lat, double lon, String title, String snippet, int iconResID) {
return googleMap.addMarker(new MarkerOptions()
.position(new LatLng(lat, lon))
.anchor(0.5f, 0.5f)
.title(title)
.snippet(snippet);
.icon(BitmapDescriptorFactory.fromResource(iconResID)));
}

Tying it all together:
ArrayList<LatLng> locations = new ArrayList();
locations.add(new LatLng(30.243442, -1.432320));
locations.add(new LatLng(... , ...));
.
.
.
for(LatLng location : locations){
mMap.addMarker(new MarkerOptions()
.position(location)
.title(...)
}

In First Location activity:
Intent i = new Intent(FirstLocationActivity.this, MapsActivity.class);
String keyLatitude = ""; //enter the value
String keyLongitude = ""; //enter the value
i.putExtra("latitude", keyLatitude );
i.putExtra("longitude", keyLongitude );
startActivity(i);
similarly do the same for activity two location, add its corresponding latitude and longitude in extra
In your mapsActivity,
String latitude="";
String longitude ="";
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.table);
Bundle bundle = getIntent().getExtras();
latitude = bundle.getString("latitude");
longitude = bundle.getString("longitude");
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
//mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
// Add a marker at the Oval and move the camera
LatLng oval = new LatLng(latitude,longitude);
mMap.addMarker(new MarkerOptions().position(oval).title("Oval Pub"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(oval));
}

Related

Add marker on long press in Google Maps API v3

How to implement adding a marker on long press in Google Maps API v3 for android? There are answers about this in Stack Overflow itself but they are for Google Maps API v2.
public class advertiserMap extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_advertiser_map);
// 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);
}
//for searching for a location
public void onMapSearch(View view) {
EditText locationSearch = (EditText) findViewById(R.id.editText);
String location = locationSearch.getText().toString();
List<Address> addressList = null;
if (location != null || !location.equals("")) {
Geocoder geocoder = new Geocoder(this);
try {
addressList = geocoder.getFromLocationName(location, 1);
} catch (IOException e) {
e.printStackTrace();
}
Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
As #tyczj pointed out, there's no v3 for Android Google Maps API, so you are probably using v2.
That said, to accomplish what you want, call setOnMapLongClickListener in your mMap object, and add the marker as you want inside onMapLongClick. You should do this in the onMapReady method:
#Override
public void onMapReady(GoogleMap googleMap) {
...
mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
#Override
public void onMapLongClick(LatLng latLng) {
googleMap.addMarker(new MarkerOptions()
.position(latLng)
.title("Your marker title")
.snippet("Your marker snippet"));
}
});
}
// EDIT:
If you want to keep only one marker present at a time, you should declare your marker in the global activity's scope, and then, in onMapLongClick, if marker already exists, instead of creating a new marker, just update it's position:
public class advertiserMap extends FragmentActivity implements OnMapReadyCallback {
// Declare marker globally
Marker myMarker;
...
#Override
public void onMapReady(GoogleMap googleMap) {
...
mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
#Override
public void onMapLongClick(LatLng latLng) {
// First check if myMarker is null
if (myMarker == null) {
// Marker was not set yet. Add marker:
myMarker = googleMap.addMarker(new MarkerOptions()
.position(latLng)
.title("Your marker title")
.snippet("Your marker snippet"));
} else {
// Marker already exists, just update it's position
myMarker.setPosition(latLng);
}
}
});
}
}
If done like this, remember to always check if your marker is not NULL before manipulating it in your code.
Hey guys if you all are using mMap.clear() then remove it . Mostly it work just fine.

Android - How to get latitude and longitude values from outside OnMapReady function?

I have this EventActivity where I have a map with a marker:
public class EventActivity extends AppCompatActivity implements OnMapReadyCallback {
private double latitude;
private double longitude;
private GoogleMap eventMap;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event);
// Declares map fragment.
SupportMapFragment eventMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.activity_event_map);
eventMapFragment.getMapAsync(this);
// Gets latitude and longitude values from database.
// (things happen here and I get the values correctly)
latitude = 12.4567785
longitude = 25.7773665
}
#Override
public void onMapReady(GoogleMap googleMap) {
eventMap = googleMap;
// Sets map position.
LatLng position = new LatLng(latitude, longitude);
eventMap.addMarker(new MarkerOptions().position(position));
eventMap.moveCamera(CameraUpdateFactory.newLatLng(position));
}
}
On the OnCreate() I get the pair of double values correctly. I can even make a toast and it shows them.
The problem is, when I want to set them as the position for the marker of my map on the OnMapReady() it gets nothing or null.
How can I properly pass the values from OnCreate() to OnMapReady()?
EDIT: I'm using Firebase as my database.
Perhaps initialize the map fragment after assigning the lat/long values as such:
public class EventActivity extends AppCompatActivity implements OnMapReadyCallback {
private double latitude;
private double longitude;
private GoogleMap eventMap;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event);
// Gets latitude and longitude values from database.
// (things happen here and I get the values correctly)
latitude = 12.4567785
longitude = 25.7773665
// Declares map fragment.
SupportMapFragment eventMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.activity_event_map);
eventMapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
eventMap = googleMap;
// Sets map position.
LatLng position = new LatLng(latitude, longitude);
eventMap.addMarker(new MarkerOptions().position(position));
eventMap.moveCamera(CameraUpdateFactory.newLatLng(position));
}
}
I needed to solve a similar issue and currently have this solution working in my app. I wanted to add a new polyline from my location to the marker in question so wanted setOnMarkerListener to have access to my location. Here is how I solved it:
Step 1.
I created two global doubles as follows:
private Double myLatitude = -33.865143;
private Double myLongitude = 151.209900;
I gave them a default value of Sydney to avoid any NullPointerException error as I am adding polylines to an array of polylines.
Step 2.
In my onLocationChanged method I updated the values:
#Override
public void onLocationChanged(Location location) {
myLatitude = location.getLatitude();
myLongitude = location.getLongitude();
....
Now in Step 3 I wanted to create a new line from mylocation to the selected marker so used the following code:
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
marker.showInfoWindow();
areaText.setText(marker.getTitle());
distanceText.setText("0 Meters");
markerLatitude = marker.getPosition().latitude;
markerLongitude = marker.getPosition().longitude;
lines.add(mMap.addPolyline(new PolylineOptions()
.add(new LatLng(myLatitude, myLongitude),
new LatLng(marker.getPosition().latitude, marker.getPosition().longitude))
.width(10)
.color(Color.RED)));
if (lines.size() > 1)
lines.remove((lines.size()-1));
return true;
}
});
Hope this helps someone with the same issue.

I want to replace a Marker from a map when user visits that location ( gets near a Marker )

First, I plot a Marker like this:
public void addMarker(String title,String lat,String Lng,int id,String address,int f)
{
marker= mMap.addMarker(new MarkerOptions().snippet(title)
.title(title+", "+address)
.position(new LatLng(Double.valueOf(lat), Double.valueOf(Lng)))
.icon(BitmapDescriptorFactory.fromResource(id)));
LatLng coordinate = new LatLng(Double.valueOf(lat), Double.valueOf(Lng));
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 10);
mMap.animateCamera(yourLocation);
mMarkerArray.add(marker);
}
After that I am trying to replace the Marker with another icon when ever I reached at any existing Location
#Override
public void onLocationChanged(Location location)
{
Log.d("latitude_main", "onlocation???");
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Log.e("latitude_main", "latitude--" + latitude+"longitude="+longitude);
current_lat= String.valueOf(latitude);
current_lng= String.valueOf(longitude);
Log.e("latitude_main","size-=="+salesmanlocationArrayList.size() );
for(int i=0;i<salesmanlocationArrayList.size();i++)
{
if(salesmanlocation.getLati().equals("12.9165757") && salesmanlocation.getLongi().equals("77.6101163"))
{
mMap.addMarker(new MarkerOptions()
.snippet(""+i).title(salesmanlocation.getFirm_name()+", "+salesmanlocation.getAddress())
.position(new LatLng(Double.valueOf(salesmanlocation.getLati().toString()), Double.valueOf(salesmanlocation.getLongi().toString())))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.event_events_select)));
}
mapFragment.getMapAsync(this);
}
}
I want to remove the marker from the map when the user visits that location.
You can simply define one OnMyLocationChangeListener class that performs your tasks, and set it on your GoogleMap instance, this way you can use it whenever you want in your application.
Step 1 - define your listener
public class MyMarkerLocationListener implements GoogleMap.OnMyLocationChangeListener {
List<Marker> markerList;
int MY_DISTANCE;
GoogleMap mMap;
public MyMarkerLocationListener(List<Marker> markerList, int meters, GoogleMap mMap)
{
this.markerList = markerList;
this.MY_DISTANCE = meters;
this.mMap = mMap;
}
#Override
public void onMyLocationChange(Location location) {
// your code/logic
//...
Location myNewLocation = location;
Location someMarkerLocation = new Location("some location");
//for each marker on your list
//check if you are close to it
for (Marker m : markerList) {
LatLng markerPosition = m.getPosition();
someMarkerLocation.setLatitude(markerPosition.latitude);
someMarkerLocation.setLongitude(markerPosition.longitude);
if (myNewLocation.distanceTo(someMarkerLocation) < MY_DISTANCE) {
//remove marker
m.remove();
//or if you still want to use it later
//m.setVisible(false);
// add your new marker
//mMap.addMarker(new MarkerOptions().icon()....);
}
}
}
}
After defining your class you just set the listener on your map on your fragment or activity code =)
Step 2 - instanciate the listener and set it
MyMarkerLocationListener myListener = new MyMarkerLocationListener(mMarkerArray, 100, mMap);
mMap.setOnMyLocationChangeListener(myListener);
UPDATE to answer your question in the comments:
You should initialize mMap before using it, take a look at this piece of code from this Stackoverflow question
public class MapPane extends Activity implements OnMapReadyCallback {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_activity);
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap map) {
//DO WHATEVER YOU WANT WITH GOOGLEMAP
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
map.setMyLocationEnabled(true);
map.setTrafficEnabled(true);
map.setIndoorEnabled(true);
map.setBuildingsEnabled(true);
map.getUiSettings().setZoomControlsEnabled(true);
}
}
don't forget your activity should implement the OnMapReadyCallback interface so the onMapReady method is called
you can use the map only after it is ready
Hope this helps!

Restrict android map marker drag to polyline

I'm looking for some suggestions on how I could approach a problem I'm facing with an android SupportMapFragment. I am drawing a polyline on a SupportMapFragment using LatLng co-ordinance stored in my apps db. I am also adding a map marker at the first and last LatLng co-ordinance to signify the start and end of the route. I would like to provide my users with the ability to trim the route by dragging the start and end markers to their desired points on the polyline. The problem I am facing is restricting the path the markers can be dragged across so they can only be moved along the polyline.
This questions is a little bit old, but I hope others would find this answer useful.
boolean PolyUtil.isLocationOnEdge will help you determine wether a point overlaps a Polygon or Polyline on the map.
public class RestrictedMarkerDragActivity extends FragmentActivity implements
OnMapReadyCallback,
GoogleMap.OnMarkerDragListener {
private static final float WIDTH = 4;
private static final double TOLERANCE_IN_METERS = 3.0;
private GoogleMap map;
private SupportMapFragment mapFragment;
private Polyline polyline;
private Marker marker;
private LatLng positionOnPolyline;
#Override
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(R.layout.activity_maps);
mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
this.map = googleMap;
List<LatLng> points = new ArrayList<>();
points.add(new LatLng(19.35853391311947, -99.15182696733749));
points.add(new LatLng(19.34384275931999, -99.1546209261358));
PolylineOptions polylineOptions = new PolylineOptions();
polylineOptions.color(Color.CYAN);
polylineOptions.width(WIDTH);
polyline = map.addPolyline(polylineOptions);
polyline.setPoints(points);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.draggable(true);
markerOptions.position(points.get(0));
marker = map.addMarker(markerOptions);
positionOnPolyline = new LatLng(marker.getPosition().latitude, marker.getPosition().longitude);
map.setOnMarkerDragListener(this);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(points.get(0), 15));
}
#Override
public void onMarkerDragStart(Marker marker) {
//Nothing to do here
}
#Override
public void onMarkerDrag(Marker marker) {
//If the marker overlaps the polyline, polyline width gets bigger and marker position gets updated,
//else, polyline width remains the same
if(PolyUtil.isLocationOnEdge(marker.getPosition(), polyline.getPoints(), true, TOLERANCE_IN_METERS)) {
polyline.setWidth(WIDTH * 3);
positionOnPolyline = new LatLng(marker.getPosition().latitude, marker.getPosition().longitude);
} else {
polyline.setWidth(WIDTH);
}
}
#Override
public void onMarkerDragEnd(Marker marker) {
//We set the marker to its last known position over the polyline
marker.setPosition(positionOnPolyline);
//We set polyline width to its original
polyline.setWidth(WIDTH);
}
}

Show markers near the device in Android

I'm developing an android app which is having several kinds of markers.
Here is my MapsActivity.java file.
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMyLocationChangeListener {
GoogleMap googleMap;
List<MapLocation> restaurantList;
List<MapLocation> hotelList;
List<Marker> restaurantMarkers = new ArrayList<>();
List<Marker> hotelMarkers = new ArrayList<>();
MapLocation r1 = new MapLocation(6.9192,79.8950, "Mnhatten Fish Market" );
MapLocation r2 = new MapLocation(6.9017,79.9192, "Dinemore" );
MapLocation r3 = new MapLocation(6.9147,79.8778, "KFC" );
MapLocation r4 = new MapLocation(6.9036,79.9547, "McDonalds" );
MapLocation r5 = new MapLocation(6.8397,79.8758, "Dominos" );
MapLocation h1 = new MapLocation(6.9006,79.8533, "Hilton" );
MapLocation h2 = new MapLocation(6.8889,79.8567, "Galadari" );
MapLocation h3 = new MapLocation(6.8756,79.8608, "Hotel Lagoon Dining" );
MapLocation h4 = new MapLocation(6.7991,79.8767, "Aqua Pearl Lake Resort " );
MapLocation h5 = new MapLocation(6.5833,79.1667, "KZ Resort" );
//Buttons
private final LatLng LOCATION_COLOMBO = new LatLng(6.9270786,79.861243);
private final LatLng LOCATION_GALLE = new LatLng(6.0334009,80.218384);
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
restaurantList = new ArrayList<>();
hotelList = new ArrayList<>();
restaurantList.add(r1);
restaurantList.add(r2);
restaurantList.add(r3);
restaurantList.add(r4);
restaurantList.add(r5);
hotelList.add(h1);
hotelList.add(h2);
hotelList.add(h3);
hotelList.add(h4);
hotelList.add(h5);
// Give text to buttons
Button buttonloc1 = (Button)findViewById(R.id.btnLoc1);
buttonloc1.setText("Colombo");
Button buttonloc2 = (Button)findViewById(R.id.btnLoc2);
buttonloc2.setText("Galle");
Button buttoncity = (Button)findViewById(R.id.btnCity);
buttoncity.setText("My Location");
Button buttonremove = (Button) findViewById(R.id.removeMarker);
buttonremove.setText("Remove");
CheckBox checkRestaurants = (CheckBox) findViewById(R.id.checkRestaurants);
checkRestaurants.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
showRestaurants();
} else {
hideRestaurants();
}
}
});
CheckBox checkHotels = (CheckBox) findViewById(R.id.checkHotels);
checkHotels.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
showHotels();
} else {
hideHotels();
}
}
});
// Marker to Dinemore
mMap.addMarker(new MarkerOptions()
.position(LOCATION_COLOMBO)
.title("I'm in Colombo :D")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
);
// Marker to Barista
mMap.addMarker(new MarkerOptions()
.position(LOCATION_GALLE)
.title("I'm in Galle :D")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
);
//When touch again on the map marker title will hide
mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
}
});
}
#Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMyLocationChange(Location location) {
Location target = new Location("target");
for(LatLng point : new LatLng[]{}) {
target.setLatitude(point.latitude);
target.setLongitude(point.longitude);
if(location.distanceTo(target) < 100) {
// bingo!
}
}
}
#Override
public void onMapReady(GoogleMap map) {
googleMap = map;
setUpMap();
}
public void showRestaurants() {
restaurantMarkers.clear();
for (MapLocation loc : restaurantList){
Marker marker = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(loc.lat, loc.lon))
.title(loc.title)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(loc.lat, loc.lon)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
restaurantMarkers.add(marker);
}
}
public void showHotels() {
hotelMarkers.clear();
for (MapLocation loc : hotelList){
Marker marker = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(loc.lat, loc.lon))
.title(loc.title)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(loc.lat, loc.lon)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
hotelMarkers.add(marker);
}
}
public void hideRestaurants(){
for (Marker marker : restaurantMarkers){
marker.remove();
}
}
public void hideHotels(){
for (Marker marker : hotelMarkers){
marker.remove();
}
}
public void onClick_City(View v){
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationChangeListener(this);
}
// When click on this button, Map shows the place of Dinemore
public void onClick_Loc1(View v) {
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_COLOMBO,10);
mMap.animateCamera(update);
}
// When click on this button, Map shows the place of Barista
public void onClick_Loc2(View v) {
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_GALLE,10);
mMap.animateCamera(update);
}
// To rmove All the Markers
public void onClick_Remove(View v){
mMap.clear();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
public class MapLocation {
public MapLocation(double lt, double ln, String t){
lat = lt;
lon = ln;
title = t;
}
public double lat;
public double lon;
public String title;
}
}
Now, what I need to do is, I need to show only markers which is near the device.
Here are the steps what I need to do.
Identify a circle round of the device area (500m)
Identify the Markers inside this circle
Show the identified Markers
Re-identify the circle when the device is moving
Need to change the circle and markers when the device is traveling.
Can I do this in Android? If can please help me to do this or post articles that might help me.
Thanks in advance.
okay i can help you with two links.
first of all i will accept that you know how to get your location from maps.after that for drawing a circle follow this guide
How to replace a circle
after that for the radius i can show you a method by the link so you can follow it and modify it for yourself
Detect nearby places
so follow this and you will get your answers.
EDIT
change your locations to latlng as follows;
for r1:
LatLng POINTA = new LatLng(6.9192,79.8950);
//do the same for the others and put it into array belove.it is simple please do not expect direct answer and work for it.
for(LatLng point : new LatLng[]{POINTA, POINTB, POINTC, POINTD}) {
target.setLatitude(point.latitude);
target.setLongitude(point.longitude);
if(location.distanceTo(target) < METERS_100) {
// bingo!
}
}
The best approach here is to simply have a limit to the distance you need the markers visible:
In my previous project, I was syncing down from a server n number of locations within 100 miles for instance. You see, this makes sure that you ONLY get what you need as opposed to having a lot of locations that you won't need.
So, I sync down say 15 nearby locations and add the markers to the map. Then when a user moves, I sync again to make sure I get ONLY the nearby business locations. This worked for me without any problems.
I hope this helps you; so in short:
Sync down n number of locations assuming you know the locations ahead of time in your server (remotely) - you can get the nearest locations using a SQL query (statement). To do this, you will need both latitude and longitude for each location.
Once the syncing is done, simply loop through the data and set the markers - based on the latitude and longitudes of the locations!
That is all you really need!
Good luck!
Surely You can do this.
You can check distance of your device from marker onLocationChanged . Here you can also re-identify circle.
Try this example. It will help you.
http://danielgultom.blogspot.com/2011/01/create-radius-around-point-in-google.html?showComment=1444634924438

Categories

Resources