I do use the Google Maps API v2 to get my location on the map. Is it possible to write the correct address in the information of the marker?
I didn't find anything useful in the internet..
Here is my code. Thanks in advance!
#SuppressLint("NewApi")
public class MainActivity extends Activity implements LocationListener {
GoogleMap map;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
}
#Override
public void onLocationChanged(Location location) {
map.clear();
MarkerOptions mp = new MarkerOptions();
mp.position(new LatLng(location.getLatitude(), location.getLongitude()));
mp.title("my position");
map.addMarker(mp);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(location.getLatitude(), location.getLongitude()), 16));
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
you want to add extra information on the marker? If i understood correctly then you can add a snipper.
LatLng mark = new LatLng(Double.parseDouble(lat), Double.parseDouble(longi));
map.addMarker(new MarkerOptions().title("MY Position").snippet("Extra info").position(mark));
Related
As you can read in the title I have a problem with the method setOnMarkerClickListener in the map which is in a fragment.
The markers appear but when I click on them, their title simply appears instead of the Toast that I show below.
I have tried different solutions but none have worked for me. I leave my code here. Any help is appreciated!
public class DashboardFragment extends Fragment implements OnMapReadyCallback{
private GoogleMap mMap;
public LocationManager locationManager;
public LocationListener locationListener;
public LatLng userLatLong,mLatLong1,mLatLong2;
public boolean isNetworkEnabled = false;
public boolean isGPSEnabled = false;
public MarkerOptions mPosicion,m1,m2;
public Marker marcadorPosicion,marcador1,marcador2;
SupportMapFragment fragment;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
fragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map2);
fragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
//REAL TIME POSITION NTW
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
userLatLong = new LatLng(location.getLatitude(), location.getLongitude());
marcadorPosicion.remove();
mPosicion=new MarkerOptions().position(userLatLong).title("Estas aqui");
marcadorPosicion = mMap.addMarker(mPosicion);
mMap.moveCamera(CameraUpdateFactory.newLatLng(userLatLong));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(userLatLong, 15));
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
};
//Marcadores
cargarMapa();
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
Toast.makeText(getActivity(), "Marker Clicked", Toast.LENGTH_SHORT).show();
return false;
}
});
}
public void cargarMapa(){
//Marcadores
//Metemos las coordenadas de los monumentos
mLatLong1= new LatLng(43.3233106, -3.1215395);//Foto_002
mLatLong2= new LatLng(43.3483518, -3.1189326);//Foto_00
m1=new MarkerOptions().position(mLatLong1).title("Foto_002").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));//Foto_002
marcador1 = mMap.addMarker(m1);
m2=new MarkerOptions().position(mLatLong2).title("Foto_005").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));//Foto_005
marcador2 = mMap.addMarker(m2);
}
This is caused due to the fact you are returning false on the onclicklistener. According to the documentation returning false makes the default option of opening the info window.
So You could do something like
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(final Marker marker) {
if (marker.getTitle().equals("Foto_002") || marker.getTitle().equals("Foto_005")) {
Toast.makeText(getActivity(), "Marker Clicked", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
}
Problem solved. For some reason when performing the addLayerMap method to add a kml to the map, the onMarkerClick method gets totally blocked. If someone has a similar problem in the future, what I'm doing is creating a route with Polyline with their respective coordinate points.
//do focus on the last else block of the code and the OnLocationChanged method(which is inside of the
OnMapReady() method)
this else block(contains the code fo rthe lastknown location method) and the onLocationChanged method
contains the code for the location which is being changed (here by passing from the extended controls
avaialable with the emulator,ie the box opens on clicking the downmost option in the emulator.)
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
LocationManager locationManager;
LocationListener locationListener;
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(final GoogleMap googleMap) {
mMap = googleMap;
locationManager=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
locationListener=new LocationListener() {
#Override
public void onLocationChanged(Location location) {
mMap.clear();
LatLng place = new LatLng(location.getLatitude(), location.getLongitude());
mMap.addMarker(new MarkerOptions().position(place).title("Marker in Sydney").icon(BitmapDescriptorFactory.defaultMarker(HUE_YELLOW)));
mMap.moveCamera(CameraUpdateFactory.newLatLng(place));
mMap.animateCamera(CameraUpdateFactory.zoomTo(3));
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
};
// Add a marker in Sydney and move the camera
if(Build.VERSION.SDK_INT<23)
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
}else
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
}
else
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
Location lastknownlocation=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
LatLng position=new LatLng(lastknownlocation.getLatitude(),lastknownlocation.getLongitude());
mMap.addMarker(new MarkerOptions().position(position).title("The Location is"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(position));
}
}
}
Add the yellow icon to your second marker just like you did with the first one, like this:
} else {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Location lastknownlocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
LatLng position = new LatLng(lastknownlocation.getLatitude(), lastknownlocation.getLongitude());
mMap.addMarker(new MarkerOptions().position(position).title("The Location is").icon(BitmapDescriptorFactory.defaultMarker(HUE_YELLOW)));
mMap.moveCamera(CameraUpdateFactory.newLatLng(position));
}
Screenshot:
Hope this helps!
I have been debugging this code for long, but could not make it. I am getting correct Longitude & Latitude in listener, but could not update the Map accordingly, Can any one help me on this.
public class MapActivity extends FragmentActivity implements OnMapReadyCallback {
protected GoogleMap mMap;
protected LocationManager locationManager;
protected LocationListener locationListener;
private double latitude, longitude;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
getLocation();
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Toast.makeText(getApplicationContext(),"Latitude:" + latitude + ", Longitude:" + longitude,Toast.LENGTH_SHORT).show();
LatLng delhiIndia = new LatLng(latitude,longitude);
mMap.addMarker(new MarkerOptions().position(delhiIndia).title("Delhi--India"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(delhiIndia));
}
public void getLocation(){
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
latitude=location.getLatitude();
longitude =location.getLongitude();
Toast.makeText(getApplicationContext(),"Latitude:" + latitude + ", Longitude:" + longitude,Toast.LENGTH_SHORT).show();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("Latitude","status");
}
#Override
public void onProviderEnabled(String provider) {
Log.d("Latitude","enable");
}
#Override
public void onProviderDisabled(String provider) {
Log.d("Latitude", "disable");
}
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
//locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
}
}
Can any one help me track the issue, please.
for Add new marker
mMap.clear();
MarkerOptions marker = new MarkerOptions()
.position(new LatLng(latitude, longitude))
.title("My Position")
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
mMap.addMarker(marker);
#Override
public void onLocationChanged(Location location) {
latitude=location.getLatitude();
longitude =location.getLongitude();
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(latitude, longitude)).zoom(3.3f).build();
mMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
Toast.makeText(getApplicationContext(),"Latitude:" + latitude + ", Longitude:" + longitude,Toast.LENGTH_SHORT).show();
}
I am developing an app that whenever you click on the get location (gpsBtn), GPS will be enabled and the Location of the user will be shown in a TextView. I get java.lang.NullPointerException error,the app has stops unexpectedly. How do I fix this?
public class MainActivity extends AppCompatActivity {
public LocationManager lm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, new MyLocationListener());
Button gpsBtn = (Button)findViewById(R.id.getLocation);
gpsBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showCurrentLocation();
}
});
public void showCurrentLocation() {
// TODO Auto-generated method stub
final TextView textView=(TextView)findViewById(R.id.textView);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
textView.setText(String.valueOf(location.getLatitude()));
}
class MyLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
Android manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mehrdad.myapplication" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
try.....move your final TextView textView=(TextView)findViewById(R.id.textView); to the onCreate() method. Also add textView.setText(String.valueOf(location.getLatitude())); line inside the try and catch block.
try{
textView.setText(String.valueOf(location.getLatitude()));
}catch(Exception e){
// exception occurs......
}
FULL CODE
public class MainActivity extends AppCompatActivity {
public LocationManager lm;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, new MyLocationListener());
textView = (TextView)findViewById(R.id.textView);
Button gpsBtn = (Button)findViewById(R.id.getLocation);
gpsBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showCurrentLocation();
}
});
public void showCurrentLocation() {
// TODO Auto-generated method stub
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
try{
textView.setText(String.valueOf(location.getLatitude()));
}catch(Exception e){
// exception occurs......
}
}
class MyLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
i finally reached an app that get's the GPS position of the user, but i reached it implementing LocationListener. it works fine, but i need to do it without implementing it, because i have to do a class that doesn't implement methods.
I searched for a lot of tutorials and check a lot of websites and i try to transform my code to not implement LocationListener but i can't do it, every thing i tested broken my app and stop getting the GPS position of the user.
Please, if someone expert on this can transform my code for not be using "implements LocationListener" i'll be grated to him
this is the code to transform:
public class GpsMiniActivity extends Activity implements LocationListener{
private LocationManager mLocMgr;
private TextView tv1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout rl = new FrameLayout(this.getApplicationContext());
LinearLayout ll= new LinearLayout(this.getApplicationContext());
ll.setOrientation(LinearLayout.VERTICAL);
setContentView(rl);
rl.addView(ll);
tv1=new TextView(getApplicationContext());
ll.addView(tv1);
//setContentView(R.layout.main);
mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,
500, 0, this);
}
#Override
public void onLocationChanged(Location location) {
tv1.setText("Lat " + location.getLatitude() + " Long " + location.getLongitude());
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
public class GpsMiniActivity extends Activity {
private LocationManager mLocMgr;
private TextView tv1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout rl = new FrameLayout(this.getApplicationContext());
LinearLayout ll= new LinearLayout(this.getApplicationContext());
ll.setOrientation(LinearLayout.VERTICAL);
setContentView(rl);
rl.addView(ll);
tv1=new TextView(getApplicationContext());
ll.addView(tv1);
//setContentView(R.layout.main);
mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,
500, 0, ll);
}
}
private LocationListener ll = new LocationListener(){
public void onLocationChanged(Location location) {
tv1.setText("Lat " + location.getLatitude() + " Long " + location.getLongitude());
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
}
There you go.
For that you will have to create a seperate LocationListener outside the onCreate() and give the reference of it to the LocationManager's requestLocationUpdates like this
LocationListener mLocationListener = new LocationListener() {
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
#Override
public void onProviderEnabled(String provider) {}
#Override
public void onProviderDisabled(String provider) {}
#Override
public void onLocationChanged(Location location) {
tv1.setText("Lat " + location.getLatitude() + " Long " + location.getLongitude());
}
};
And after that you will have to reference this LocationListener like this inside the onCreate()
mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,
500, 0, mLocationListener);