I'm new to programming in Android, I'm developing an application which shows some markers that I'm giving you by code, when adding a cluster, it marks me a few default points.
I would like to know how do I change those default points in the cluster and add the markers to the points I want?
private void addItems() {
double lat = 3.424014;
double lng = -76.536218;
for (int i = 0; i < 5; i++) {
double offset = i / 60d;
lat = lat + offset;
lng = lng + offset;
MyItem offsetItem = new MyItem(lat, lng);
mClusterManager.addItem(offsetItem);
}
}
#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);
btnclute=(Button)findViewById(R.id.btn_cluster);
//cluster
btnclute.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(3.422504, -76.538128), 13));
mClusterManager = new ClusterManager<MyItem>(getBaseContext(), mMap);
mMap.setOnCameraIdleListener(mClusterManager);
mMap.setOnMarkerClickListener(mClusterManager);
addItems();
}
});
}
/**
* 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.
*/
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mapa= googleMap;
// Add a marker in Sydney and move the camera
//LatLng sydney = new LatLng(-34, 151);
//Marker
//Estadio
LatLng home = new LatLng(3.429862, -76.541336);
dato=getResources().getString(R.string.estadio);
mMap.addMarker(new MarkerOptions().position(home).title(dato)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.estadio)));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(home, 13));
//Sebastian Belalcazar
LatLng home1 = new LatLng(3.449151, -76.545185);
dato1=getResources().getString(R.string.sebas);
mMap.addMarker(new MarkerOptions().position(home1).title(dato1)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.sebastian)));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(home1, 13));
//Cristo Rey
LatLng home2 = new LatLng(3.435855, -76.564840);
dato2=getResources().getString(R.string.rey);
mMap.addMarker(new MarkerOptions().position(home2).title(dato2)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.cristo)));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(home2, 13));
//Tres cruces
LatLng home3 = new LatLng(3.467800, -76.545473);
mMap.addMarker(new MarkerOptions().position(home3).title(getResources().getString(R.string.cruces))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.tres)));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(home3, 13));
//Plaza de Caicedo
LatLng home5 = new LatLng( 3.451864, -76.532479);
dato3=getResources().getString(R.string.plaza);
dato4="-" + getResources().getString(R.string.caicedo);
dato5="-" + getResources().getString(R.string.pedro);
dato6="-" + getResources().getString(R.string.palacio);
mMap.addMarker(new MarkerOptions().position(home5).title(dato3)
.snippet(dato4 + "\n" +
dato5 + "\n" +
dato6)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.plaza)));
mapa.moveCamera(CameraUpdateFactory.newLatLngZoom(home5, 13));
You need to set an algorithm so the clusters don't align in a grid, thus becoming more precise.
NonHierarchicalDistanceBasedAlgorithm<ClusterItem> algo = new NonHierarchicalDistanceBasedAlgorithm<>();
mClusterManager.setAlgorithm(algo);
//If that isn't enough you can change the latlng
Collection<ClusterItem> col = algo.getItems();
for (ClusterItem clusterItem : col) {
clusterItem.getPosition().latitude = yourNewLatitude;
clusterItem.getPosition().longitude = yourNewLongitude;
}
Related
I have designed a Marker and an Info Window. Now the location is being fetched from a Database every 2 seconds and the marker position gets updated. The Info Window is shown on Marker Click. Now whenever the marker gets updated, the Info Window disappears. If I put the Info Window inside repeat function then the Info Window keeps on showing. I need to show the Info Window on Marker Click and disappear on clicking Marker again. It should not disappear on the refreshing of Marker position.
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
Log.e(TAG,"Getting Repeated Location");
scheduleSendLocation();
myLat = Double.parseDouble(latitude);
myLng = Double.parseDouble(longitude);
Toast.makeText(MapsActivity.this, "Lat:"+myLat+"\tLng:"+myLng, Toast.LENGTH_LONG).show();
if (myLat==0 && myLng==0) {
Toast.makeText(MapsActivity.this,"Invalid Location",Toast.LENGTH_SHORT).show();
handler.removeCallbacks(runnable);
Intent dev = new Intent(MapsActivity.this,DevicesNavActivity.class);
startActivity(dev);
}
else if (myLat==null && myLng==null) {
Toast.makeText(MapsActivity.this,"No Data Found",Toast.LENGTH_SHORT).show();
handler.removeCallbacks(runnable);
Intent dev = new Intent(MapsActivity.this,DevicesNavActivity.class);
startActivity(dev);
}
else {
CustomInfoWindowActivity customInfoWindow = new CustomInfoWindowActivity(this);
mMap.setInfoWindowAdapter(customInfoWindow);
final MarkerOptions markerOptions = new MarkerOptions();
final Marker[] m = new Marker[1];
LatLng loc = new LatLng(myLat,myLng);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(loc, 17f);
mMap.moveCamera(cameraUpdate);
handler.postDelayed(runnable2 = new Runnable() {
public void run() {
LatLng location = new LatLng(myLat, myLng);
//Adding Marker to Location
markerOptions.position(location)
.title("Marker")
.snippet("My Marker")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
myLat = Double.parseDouble(latitude);
myLng = Double.parseDouble(longitude);
mMap.clear();
m[0] = mMap.addMarker(markerOptions);
LatLng coordinate = new LatLng(myLat, myLng); //Store these lat lng values somewhere. These should be constant.
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(coordinate, 17f);
mMap.animateCamera(cameraUpdate);
handler.postDelayed(this, TIME);
}
}, TIME);
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
//Displaying the Info Window
InfoWindowData info = new InfoWindowData();
info.setName("Co-Ordinates of "+myimei);
info.setLatitude(latitude);
info.setLongitude(longitude);
m[0].setTag(info);
m[0].showInfoWindow();
return true;
}
});
after you add marker after clearing marker call showInfowindow on marker
m[0] = mMap.addMarker(markerOptions);
m[0].showInfoWindow();
this should do the trick
I have this seemingly simple class in my app. All it does it add a circle to the mapView at my Location with the circle color depending on the CDMA signal Strength at the location. Or that's what it's supposed to do.
The app works fine if I only ask for one circle and the location with the cdma signal but it doesn't work in the if Statement format, even if there is only one else if present. My question is why and if It has anything to do with how many times maps can be called at once. And If anyone knows a workaround, it would help a lot.
private GoogleMap mMap;
int cdmaSignal;
double latitude;
double longitude;
BroadcastReceiver br;
#Override
protected void onResume() {
super.onResume();
if(br == null){
br = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent z) {
shoveItThrough((int) z.getExtras().get("cell signal cdma"),
(double) z.getExtras().get("latitude"), (double) z.getExtras().get("longitude"));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(latitude, longitude)) // Sets the center of the map to location user
.zoom(17) // Sets the zoom
.bearing(0) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build();
//
//mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
/*mMap.addCircle(new CircleOptions()
.center(new LatLng(latitude,longitude)).radius(80).strokeColor(Color.rgb(255,0,0)).fillColor(Color.rgb((255,0,0)));*/
//LatLng home = new LatLng(latitude, longitude);
//mMap.addMarker(new MarkerOptions().position(home));
if(cdmaSignal<=-120 && cdmaSignal>-100){
mMap.addCircle(new CircleOptions()
.center(new LatLng(latitude,longitude)).radius(80).strokeColor(Color.rgb(255,0,0)).fillColor(Color.rgb(255,0,0)));
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
/*LatLng home = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(home));
mMap.moveCamera(CameraUpdateFactory.newLatLng(home));*/
} else if(cdmaSignal<=-100 && cdmaSignal>-80){
mMap.addCircle(new CircleOptions()
.center(new LatLng(latitude,longitude)).radius(80).strokeColor(Color.rgb(255,98,12)).fillColor(Color.rgb(255,98,12)));
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
/*LatLng home = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(home));
mMap.moveCamera(CameraUpdateFactory.newLatLng(home));*/
} else if(cdmaSignal<=-80 && cdmaSignal>-60){
mMap.addCircle(new CircleOptions()
.center(new LatLng(latitude,longitude)).radius(80).strokeColor(Color.rgb(255,181,12)).fillColor(Color.rgb(255,181,12)));
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
/*LatLng home = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(home));
mMap.moveCamera(CameraUpdateFactory.newLatLng(home));*/
} else if(cdmaSignal<=-60 && cdmaSignal>-40){
mMap.addCircle(new CircleOptions()
.center(new LatLng(latitude,longitude)).radius(80).strokeColor(Color.rgb(211,255,0)).fillColor(Color.rgb(211,255,0)));
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
/*LatLng home = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(home));
mMap.moveCamera(CameraUpdateFactory.newLatLng(home));*/
} else if(cdmaSignal<=-40 && cdmaSignal>-20){
mMap.addCircle(new CircleOptions()
.center(new LatLng(latitude,longitude)).radius(80).strokeColor(Color.rgb(164,255,0)).fillColor(Color.rgb(164,255,0)));
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
/*LatLng home = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(home));
mMap.moveCamera(CameraUpdateFactory.newLatLng(home));*/
} else if(cdmaSignal<=-20 && cdmaSignal>-0){
mMap.addCircle(new CircleOptions()
.center(new LatLng(latitude,longitude)).radius(80).strokeColor(Color.rgb(0,255,0)).fillColor(Color.rgb(0,255,0)));
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
/*LatLng home = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(home));
mMap.moveCamera(CameraUpdateFactory.newLatLng(home));*/
}
}
};
}
registerReceiver(br, new IntentFilter("location_update"));
}
#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
protected void onDestroy() {
super.onDestroy();
if(br != null){
unregisterReceiver(br);
}
}
/**
* 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.
*/
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
}
public void shoveItThrough(int c, double la, double lo){
cdmaSignal = c;
latitude = la;
longitude = lo;
}
}
There are latitude and longitude of two place is given.This is my map activity:
#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;
// double array [] = {44.968046 ,-94.420307 ,44.33328,-89.132008, 33.755787,-116.359998,33.844843,-116.54911 ,44.92057 ,-93.44786};
// Add a marker in Sydney and move the camera
double lat1 = 23.781388 ;
double lon1 = 90.425500 ;
double lat2 = 23.780270 ;
double lon2 = 23.780270 ;
LatLng place1 = new LatLng( lat1, lon1);
LatLng place2 = new LatLng( lat2, lon2);
mMap.addMarker(new MarkerOptions().position(place1).title("Marker in Pran RFL"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(place1));
mMap.addMarker(new MarkerOptions().position(place2).title("Marker in Gulshan"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(place2));
}
I want to draw a path between this couple of latitude and longitude.How Can I do it?
You have to add PolyLineOptions and add new LatLng() try this code
PolylineOptions options = new PolylineOptions();
options.color( Color.parseColor( "#CC0000FF" ) );
options.width( 5 );
options.visible( true );
options.add( new LatLng( lat1 ,lon1) );
options.add( new LatLng( lat2 ,lon2) );
mMap.addPolyline( options );
I'm developing an Android App for Tablets, which shows two maps with markers in master-detail-layout.
Both maps contain different markers. When I start to move the camera in one of the maps, markers on the other map start to jump/flicker and sometimes even stay in wrong locations.
I was able to reproduce the problem with the google-maps android-samples
by simply adding some markers to two of the maps, nothing more. The flickering is actually far worse than visible in the gif.
gif displaying jumping markers
Here's the whole changed Activity for completeness:
public class MultiMapDemoActivity extends AppCompatActivity {
private static final LatLng BRISBANE = new LatLng(-27.47093, 153.0235);
private static final LatLng MELBOURNE = new LatLng(-37.81319, 144.96298);
private static final LatLng SYDNEY = new LatLng(-33.87365, 151.20689);
private static final LatLng ADELAIDE = new LatLng(-34.92873, 138.59995);
private static final LatLng PERTH = new LatLng(-31.952854, 115.857342);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.multimap_demo);
final SupportMapFragment firstMapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map1);
firstMapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
addMarkers(googleMap, multiplyMarkerPosition(BRISBANE));
addMarkers(googleMap, multiplyMarkerPosition(MELBOURNE));
addMarkers(googleMap, multiplyMarkerPosition(SYDNEY));
addMarkers(googleMap, multiplyMarkerPosition(ADELAIDE));
addMarkers(googleMap, multiplyMarkerPosition(PERTH));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(ADELAIDE, 4f));
}
});
final SupportMapFragment secondMapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map2);
secondMapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
addMarkersBlue(googleMap, multiplyMarkerPosition(new LatLng(51.51, 0)));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(51.51, 0), 4f));
}
});
}
public List<LatLng> multiplyMarkerPosition(LatLng position) {
final LinkedList<LatLng> positions = new LinkedList<>();
final double latitude = position.latitude;
final double longitude = position.longitude;
final double delta = 1;
positions.add(new LatLng(latitude, longitude + delta));
positions.add(new LatLng(latitude, longitude - delta));
positions.add(new LatLng(latitude, longitude));
positions.add(new LatLng(latitude - delta, longitude + delta));
positions.add(new LatLng(latitude - delta, longitude - delta));
positions.add(new LatLng(latitude - delta, longitude));
positions.add(new LatLng(latitude + delta, longitude + delta));
positions.add(new LatLng(latitude + delta, longitude - delta));
positions.add(new LatLng(latitude + delta, longitude));
return positions;
}
public void addMarkers(GoogleMap map, List<LatLng> positions) {
for (LatLng latLng : positions) {
map.addMarker(new MarkerOptions().position(latLng));
}
}
public void addMarkersBlue(GoogleMap map, List<LatLng> positions) {
for (LatLng latLng : positions) {
map.addMarker(
new MarkerOptions()
.position(latLng)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
}
}
}
I couldn't find out whether this is a known bug or an unsupported mode of operation. If you have any ideas or possible workarounds, please let me know. Thanks.
I m working with google map. I want to get marker pointed to the current location of user by default.How can i do this.Here is my code and is there any way to get the map of India only on google map.plz help me..
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_activity1);
MapFragment fm = (MapFragment) getFragmentManager()
.findFragmentById(R.id.fragment1);
// Getting Map for the SupportMapFragment
final GoogleMap map = fm.getMap();
if(map!=null){
map.setMyLocationEnabled(true);
}
if(map!=null){
Location myLocation = map.getMyLocation();
if(myLocation !=null){
LatLng myLatLng = new LatLng(myLocation.getLatitude(),
myLocation.getLongitude());
CameraPosition myPosition = new CameraPosition.Builder()
.target(myLatLng).zoom(17).bearing(90).tilt(30).build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(myPosition));
}
}
// Setting a click event handler for the map
map.setOnMapClickListener(new OnMapClickListener() {
#Override
public void onMapClick(LatLng latLng) {
// Creating a marker
MarkerOptions markerOptions = new MarkerOptions();
// Setting the position for the marker
markerOptions.position(latLng);
// Setting the title for the marker.
// This will be displayed on taping the marker
markerOptions.title(latLng.latitude + " : " + latLng.longitude);
// Clears the previously touched position
map.clear();
// Animating to the touched position
map.animateCamera(CameraUpdateFactory.newLatLng(latLng));
// Placing a marker on the touched position
map.addMarker(markerOptions);
double lat=latLng.latitude;
double longitude=latLng.longitude;
String LAT=String.valueOf(lat);
String LONGITUDE=String.valueOf(longitude);
Intent in = new Intent(getApplicationContext(),
LocationActivity.class);
// Sending lat/long to next activity
in.putExtra(TAG_LAT, LAT);
in.putExtra(TAG_LONG, LONGITUDE);
startActivityForResult(in, 100);
}
});
}
To get marker pointed to the current location, you can do like that:
Location myLcation = map.getMyLocation();
LatLng myPosition = new LatLng(myLcation.getLatitude(),myLcation.getLongitude());
MarkerOptions currentLocationMarker = new MarkerOptions()
.position(myPosition)
.title(/*YOUR TITLE*/)
.icon(BitmapDescriptorFactory.fromResource(R.drawable./*YOUR ICON*/));
map.addMarker(currentLocationMarker);
if you want to zoom on this position:
CameraUpdate location = CameraUpdateFactory.newLatLngZoom(myPosition, 6);
mMap.animateCamera(location);