Google Maps: Current Location Marker (Period updates for GMaps) - java

So I've been able to get periodic updates of my current location through the developer android page, making your app location aware. Now, whenever my location changes, I am able to get the latitude and longitude of that location. However, who do i implement this with Google Maps?
This line below implements a button on my map that finds my current location and places a blue dot/marker on it (does not receive periodic updates)
mMap.setMyLocationEnabled(true);
What should I put in my onLocationChanged() event in order for the blue dot to be updated with the new lat and long?

The blue dot and the precision circle are automatically managed by the map and you can't update it or change it's symbology. In fact, it's managed automatically using it's own LocationProvider so it gets the best location resolution available (you don't need to write code to update it, just enable it using mMap.setMyLocationEnabled(true);).
If you want to mock it's behaviour you can write something like this (you should disable the my location layer doing mMap.setMyLocationEnabled(false);):
private BitmapDescriptor markerDescriptor;
private int accuracyStrokeColor = Color.argb(255, 130, 182, 228);
private int accuracyFillColor = Color.argb(100, 130, 182, 228);
private Marker positionMarker;
private Circle accuracyCircle;
#Override
protected void onCreate(Bundle savedInstanceState) {
// ...
markerDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.yourmarkericon);
}
#Override
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
float accuracy = location.getAccuracy();
if (positionMarker != null) {
positionMarker.remove();
}
final MarkerOptions positionMarkerOptions = new MarkerOptions()
.position(new LatLng(latitude, longitude))
.icon(markerDescriptor)
.anchor(0.5f, 0.5f);
positionMarker = mMap.addMarker(positionMarkerOptions);
if (accuracyCircle != null) {
accuracyCircle.remove();
}
final CircleOptions accuracyCircleOptions = new CircleOptions()
.center(new LatLng(latitude, longitude))
.radius(accuracy)
.fillColor(accuracyFillColor)
.strokeColor(accuracyStrokeColor)
.strokeWidth(2.0f);
accuracyCircle = mMap.addCircle(accuracyCircleOptions);
}

mMap.setMyLocationEnabled(true);
this is simple trick for blue marker of current location and did the trick for me.

Related

How to make the human marker move on the map?

I have a marker humanMarker on the map. How to make it move when the user is moving?
private Marker humanMarker;
humanMarker = map.addMarker(new MarkerOptions()
.position(new LatLng(mLocation.getLatitude(), mLocation.getLongitude()))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.human)));
Try to override the onLocationChanged method. humanMarker will move when the location of the user is changed.
#Override
public void onLocationChanged(Location location) {
// Update current location of the marker
humanMarker.setPosition(new LatLng(location.getLatitude(), location.getLongitude()));
}

Using a variable value from one method in different one in Android Studio

Firstly sorry for asking so many questions here the past couple of weeks, I'm new to android studio and finding it tough to figure out a lot of the core concepts by myself.
In regards to the question, I have a project set up so whatever you type into the "1.2, -1.2" ect parameters you will find you the distance between two places and your answer will be displayed as a toast. However, I want the latitudes and longitueds to be variables.
Button button1=(Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Double distance = 1.0;
int val = 1;
Toast.makeText(getApplicationContext(), "You are " + String.valueOf(distance
(1.2, -1.2, 1.3, -2.4, "K")) + "kilometers away from the flag", Toast.LENGTH_LONG).show(); }
});
I have the following two methods and I want the values LatLng and currentLatitude and currentLongitude doubles for the parameters above.
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(53.3835,6.5996)).title("Marker"));
}
and
private void handleNewLocation(Location location) {
Log.d(TAG, location.toString());
double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();
LatLng latLng = new LatLng(currentLatitude, currentLongitude);
//mMap.addMarker(new MarkerOptions().position(new LatLng(currentLatitude, currentLongitude)).title("Current Location"));
MarkerOptions options = new MarkerOptions()
.position(latLng)
.title("I am here!");
mMap.addMarker(options);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
}
Any insight on how to do it would be much appreciated. From what I've read I know I'll need to split the LatLng variable into strings but thats all I can think of so far
If those 2 methods are in the same class then just make those 2 variables attributes of that class, else you will need to pass them to the corresponding classes. I will demonstrate in a simpler example than your code :
public class MyClass(){
private int lat;
private int longt;
//constructors ,setters and getters
public void method1(){
//affecting those attributes with values
lat = 1;
longt = 2;
}
public void method2(){
//simply access the attributes
System.out.println("lat "+lat+" longt "+longt);
}
}
Reading the comments , I think I need to further explain to you that when a variable is declared inside a method, it is local to that method, and therefor it will be "destroyed" (garbage collected or w.e) when that method is done doing it's job. But when a variable is declared outside a method, like the class's attributes, you can still refer to it whenever you need to, until the instance of that class is "destroyed".

Android cluster map change marker

I was having some troubles when trying to customize the clsutered markers on Google Map. I am using Google Map Android API Library and reference from GoogleMap Documentation. Here are my codes:
private void setUpClusterer() {
googleBasemap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
1.379348, 103.849876), 11.0f));
mClusterManager = new ClusterManager<EventClusterItem>(this,
googleBasemap);
googleBasemap.setOnCameraChangeListener(mClusterManager);
googleBasemap.setOnMarkerClickListener(mClusterManager);
addItems();
}
private void addItems() {
for (int i = 0; i < convertedGeomList.size(); i++) {
EventClusterItem offsetItem = new EventClusterItem(convertedGeomList.get(i)
.getY(), convertedGeomList.get(i).getX());
mClusterManager.addItem(offsetItem);
}
}
And my EventClusterItem class:
public class EventClusterItem implements ClusterItem {
private final LatLng mPosition;
public EventClusterItem(double lat, double lng) {
mPosition = new LatLng(lat, lng);
}
public LatLng getPosition() {
return mPosition;
}
}
So basically with these codes, it only shows up a red color marker onto the map. I wonder how can I customize the marker with my own image. I know you can do this to customize the marker on the map:
Marker melbourne = mMap.addMarker(new MarkerOptions()
.position(MELBOURNE)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
But I not sure how to implement it in this case. Any guides?
Thanks in advance.
Use answers from these posts:
How to set my own icon for markers in clusterer in Google Maps
How to add title, snippet and icon to ClusterItem?
There is no any guide about this API. You should find the code for this library and try to understand it - it is easy, the code is perfect (but a little bit slow in fact).

FragmentActivity - Zoom to show streets

I'm having some issues using FragmentActivity and SupportMapFragment. The map's zoom is all wonky.
Here's the code:
public class GoogleMapActivity extends FragmentActivity{
Double longitude, latitude;
static LatLng koordinate;
GoogleMap supportMap;
String title, address;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_googlemaps);
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
supportMap = fm.getMap();
longitude = 13.597651720046997;
latitude = 45.22456639103469;
title = "Turistička zajednica Općine Vrsar";
address = "ul. Rade Končara 46";
koordinate = new LatLng(longitude, latitude);
Marker marker = supportMap.addMarker(new MarkerOptions().position(koordinate).title(title).snippet(address)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_star)));
//supportMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
supportMap.moveCamera(CameraUpdateFactory.newLatLngZoom(koordinate, 15));
supportMap.animateCamera(CameraUpdateFactory.zoomTo(12), 2000, null);
}}
For some reason, I get following thing in google maps, which is not zoomed enough:
http://imageshack.us/f/812/3yl.png/
When I click on the zoom, the map goes into this state:
http://imageshack.us/f/825/k8np.png/
I need the maps to zoom enough so that the street names are shown.This works just fine in my previous application, but I used regular MapFragment there. For this app, I need SupportMapFragment. Maybe that's causing this issue?
Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
OR
LatLng latLng = new LatLng(latitude, longitude);
// Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
the maximum zoom allowed is 17 i guess.
i hope this must work...

Getting Marker Coordinates passed to variable

I'm building an app that allows a user to set a proximity alarm by selecting a marker and clicking on the info window to confirm. I need to be able to get the latitude and longitude from the marker so I can use the coordinates to set up the radius.
googleMap.setOnInfoWindowClickListener(
new OnInfoWindowClickListener(){
public void onInfoWindowClick(Marker marker) {
//click function
Toast.makeText(getBaseContext(),
"Info Window clicked#" + marker.getPosition(),
Toast.LENGTH_SHORT).show();
LocationManager lm;
double lat=0;
double long1=0; //Defining Latitude & Longitude
float radius=3000;
lm=(LocationManager) getSystemService(LOCATION_SERVICE);
Intent i= new Intent("com.example.sleepertrain5.proximityalert"); //Custom Action
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), -1, i, 0);
lm.addProximityAlert(lat, long1, radius, -1, pi);
So far, I've only been able to find marker.getLocation() which doesn't allow direct variable setting. Is there a way to do this?
Have you tried this:
map.setInfoWindowAdapter(new InfoWindowAdapter() {
// Use default InfoWindow frame
#Override
public View getInfoWindow(Marker args) {
return null;
}
// Defines the contents of the InfoWindow
#Override
public View getInfoContents(Marker args)
{
LatLng clickedMarkerLatLng = args.getPosition();
double latitude = clickedMarkerLatLng.latitude;
double longitude = clickedMarkerLatLng.longitude;
....
}
I hope this code helps you :
#Override
public void onMapLongClick(LatLng point) {
Latitude lat = point.getLatitude();
Longitude lng = point.getLongitude();
}

Categories