Map not works in map v2 - android

I want to get map location by name.
But map is not showing lcoation. My code is given below:
SupportMapFragment fm = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
// Getting GoogleMap object from the fragment
googleMap = fm.getMap();
if (googleMap != null)
// Enabling MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);
Geocoder geoCoder = new Geocoder(getActivity(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocationName("Ferozepur Rd Lahore", 1);
if (addresses.size() > 0) {
int lat = (int) (addresses.get(0).getLatitude() * 1E6);
int longt = (int) (addresses.get(0).getLongitude() * 1E6);
p = new GeoPoint(lat,longt);
LatLng latLng = new LatLng(lat, longt);
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.addMarker(new MarkerOptions().position(new LatLng(lat, longt)).title("Hello world"));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
}
} catch (IOException e) {
e.printStackTrace();
}
But it is showing me this result:

Latitude and longitude itself you didnt shown.code is incomplete.However Use Mapview balloons.
https://github.com/jgilfelt/android-mapviewballoons

Related

How Can i Show multiple location marker with optimize Distance and draw line to connect each others

I have already try to achieve this type of functionality using google map direction API. You can see the picture I found this in javascript code but I want to do in android.
I want to achieve like this please click here..
PolygonOptions rectOptions2;
rectOptions2= new PolygonOptions();
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick( final LatLng latLng)
{
// PolylineOptions rectOptions = new PolylineOptions()
// .add(new LatLng(latitude, longitude));
// Polyline polyline = mMap.addPolyline(rectOptions);
// Creating a marker
BitmapDescriptor icon =
BitmapDescriptorFactory.fromResource(R.drawable.ic_map);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions .icon(icon);
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
final List<Address> listAddresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1);
if (null != listAddresses && listAddresses.size() > 0) {
markerOptions.title(location);
location = listAddresses.get(0).getAddressLine(0);
latitude=latLng.latitude;
longitude=latLng.longitude;
}
} catch (IOException e) {
e.printStackTrace();
}
// mMap.clear();
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.addMarker(markerOptions);
lat=latitude;
lng=longitude;
rectOptions2.add(new LatLng(lat, lng));
polygon= mMap.addPolygon(rectOptions2);
polygon.setStrokeColor(Color.RED);
polygon.setFillColor(Color.BLUE);
//add lat and lng in arraylist here
}
});

Drawing a polyline from current location to a fixed location

Okay, so I am developing a google maps like application using android studio. And right now I am having difficulty in understanding it. So my problem is I cannot set a specific destination that I want to go to and then draw a polyline going to it. Can somebody please help me. Thanks in advance
PolygonOptions rectOptions2;
rectOptions2= new PolygonOptions();
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick( final LatLng latLng)
{
// PolylineOptions rectOptions = new PolylineOptions()
// .add(new LatLng(latitude, longitude));
// Polyline polyline = mMap.addPolyline(rectOptions);
// Creating a marker
BitmapDescriptor icon =
BitmapDescriptorFactory.fromResource(R.drawable.ic_map);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions .icon(icon);
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
final List<Address> listAddresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1);
if (null != listAddresses && listAddresses.size() > 0) {
markerOptions.title(location);
location = listAddresses.get(0).getAddressLine(0);
latitude=latLng.latitude;
longitude=latLng.longitude;
}
} catch (IOException e) {
e.printStackTrace();
}
// mMap.clear();
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.addMarker(markerOptions);
lat=latitude;
lng=longitude;
rectOptions2.add(new LatLng(lat, lng));
polygon= mMap.addPolygon(rectOptions2);
polygon.setStrokeColor(Color.RED);
polygon.setFillColor(Color.BLUE);
//add lat and lng in arraylist here
}
});

Marker taps are not always recognized android

I have included Google Map in my app and added 4 markers in the map by using a for loop. I also managed to get the zip code, the name of the city and the adress of these added markers by using Geocoder.
It all works, but the problem is that clicks on the markers do not always seem to work. Sometimes I have to make double taps to see the title of the markers. I really don't know why. Here is my full code
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
String city, adress, zip;
Marker marker;
LatLngBounds.Builder builder = new LatLngBounds.Builder();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng[] point_new = new LatLng[4];
point_new[0] = new LatLng(52.4788535, 13.32730760000004);
point_new[1] = new LatLng(52.4794297, 13.313520799999992);
point_new[2] = new LatLng(52.5272885, 13.458033200000045);
point_new[3] = new LatLng(52.52603999999999, 13.488159999999993);
for (int i = 0; i < point_new.length; i++) {
drawMarker(point_new[i]);
marker = mMap.addMarker(new MarkerOptions().position(point_new[i]));
mMap.moveCamera(CameraUpdateFactory.newLatLng(point_new[i]));
}
getAdress(52.4788535, 13.32730760000004 );
marker = mMap.addMarker(new MarkerOptions().position(point_new[0]).title(adress+"," + zip + "" + city));
getAdress(52.4794297, 13.313520799999992);
marker = mMap.addMarker(new MarkerOptions().position(point_new[1]).title(adress+"," + zip + "" + city));
getAdress(52.5272885, 13.458033200000045);
marker = mMap.addMarker(new MarkerOptions().position(point_new[2]).title(adress+"," + zip + "" + city));
getAdress(52.52603999999999, 13.488159999999993);
marker = mMap.addMarker(new MarkerOptions().position(point_new[3]).title(adress+"," + zip + "" + city));
for (int j = 0; j < point_new.length; j++) {
builder.include(point_new[j]);
}
LatLngBounds bounds = builder.build();
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 20));
}
public void drawMarker(LatLng point) {
// Creating an instance of MarkerOptions
MarkerOptions markerOptions = new MarkerOptions();
// Setting latitude and longitude for the marker
markerOptions.position(point);
// Adding marker on the Google Map
mMap.addMarker(markerOptions);
}
public void getAdress(double lat, double lng){
Geocoder geocoder = new Geocoder(MapsActivity.this, Locale.getDefault());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(lat,lng,1);
} catch (IOException e) {
e.printStackTrace();
}
city = addresses.get(0).getLocality();
adress = addresses.get(0).getAddressLine(0);
zip = addresses.get(0).getPostalCode();
}
}
try this code, i have tested, its working
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);
}
/**
* 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;
List<MarkerDesc> markerDescList = new ArrayList<>();
markerDescList.add(new MarkerDesc(new LatLng(52.4788535, 13.32730760000004), getAdress(52.4788535, 13.32730760000004)));
markerDescList.add(new MarkerDesc(new LatLng(52.4794297, 13.313520799999992), getAdress(52.4794297, 13.313520799999992)));
markerDescList.add(new MarkerDesc(new LatLng(52.5272885, 13.458033200000045), getAdress(52.5272885, 13.458033200000045)));
markerDescList.add(new MarkerDesc(new LatLng(52.52603999999999, 13.488159999999993), getAdress(52.52603999999999, 13.488159999999993)));
for(int i=0; i<markerDescList.size(); i++){
MarkerDesc markerDesc = markerDescList.get(i);
mMap.addMarker(new MarkerOptions()
.position(markerDesc.getLatLng())
.title(markerDesc.getAddresses().get(0).getLocality())
.snippet(markerDesc.getAddresses().get(0).getAddressLine(0)+"\n"+markerDesc.getAddresses().get(0).getPostalCode()+"\n"+markerDesc.getAddresses().get(0).getLocality())
.icon(BitmapDescriptorFactory.defaultMarker()));
}
LatLng latLng = markerDescList.get(markerDescList.size()-1).getLatLng();
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder().target(latLng).zoom(15).bearing(0).tilt(25).build()));
}
public List<Address> getAdress(double lat, double lng){
Geocoder geocoder = new Geocoder(MapsActivity.this, Locale.getDefault());
try {
return geocoder.getFromLocation(lat,lng,1);
} catch (IOException e) {
e.printStackTrace();
}
/*String city = addresses.get(0).getLocality();
String adress = addresses.get(0).getAddressLine(0);
String zip = addresses.get(0).getPostalCode();*/
return null;
}
private class MarkerDesc{
LatLng latLng;
List<Address> addresses;
private MarkerDesc(LatLng ltLng, List<Address> addr){
this.latLng=ltLng;
this.addresses = addr;
}
private LatLng getLatLng() {
return latLng;
}
private List<Address> getAddresses() {
return addresses;
}
}
}

How to know marker latitude and longitude position and draw a polyline

I am developing an android google map application, that is showing current location on map starting.
I have an search bar in the application, when user enter any area name, then the second marker will be placed on that location.
Now my problem is, how to get second marker longitude and latitude position and make a route between the two markers.
my MainActivity.java code as follows:
public class MainActivity 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);
}
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.addMarker(new MarkerOptions().position(latLng).title("Marker"));
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(27.746974, 85.301582);
mMap.addMarker(new MarkerOptions().position(sydney).title("Kathmandu, Nepal"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
// Enable MyLocation Button in the Map
mMap.setMyLocationEnabled(true);
}
}
Please Help me.
In your program you have used the code to get the LatLng of the second marker.
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng).title("Marker"));
To make a polyline, refer to this link. By the this question has already been answered.
https://www.simplifiedcoding.net/google-maps-distance-calculator-google-maps-api/
Set Google Places API for search EdiText. Get the value from that and pass it as address..
Here is the code(address denoting strAddress here)
List<Address> address = null;
Geocoder coder = new Geocoder(getApplicationContext());
try {
address = coder.getFromLocationName(strAddress, 1);
} catch (IOException e) {
e.printStackTrace();
}
if (address != null) {
Address location = address.get(0);
double lat = location.getLatitude();
double log = location.getLongitude();
}

Can not resolve symbol GeoPoint in android studio

I am using android studio 0.8.9. i am using google map v2 and google services 5.2.08.i want to display marker from given address and also generated SHA Key using key-tool also enabled and get my API Key from google console. But when i am using GeoPoint for getting Lat and Long android studio does not recognize GeoPoint class.
My Java Activity code:
public class WelcomeUser extends ActionBarActivity {
GoogleMap googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome_user);
try {
if(null == googleMap){
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.mapView)).getMap();
/**
* If the map is still null after attempted initialisation,
* show an error to the user
*/
if(null == googleMap) {
Toast.makeText(getApplicationContext(),
"Error creating map", Toast.LENGTH_SHORT).show();
}
}
} catch (NullPointerException exception){
Log.e("mapApp", exception.toString());
}
double lat= 0.0, lng= 0.0;
String address = "Ahmedabad, Gujarat";
Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
try
{
List<Address> addresses = geoCoder.getFromLocationName(address , 1);
if (addresses.size() > 0)
{
GeoPoint p = new GeoPoint(
(int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude() * 1E6));
lat=p.getLatitudeE6()/1E6;
lng=p.getLongitudeE6()/1E6;
Log.d("Latitude", ""+lat);
Log.d("Longitude", ""+lng);
}
}
catch(Exception e)
{
e.printStackTrace();
}
if(null != googleMap){
googleMap.addMarker(new MarkerOptions()
.position(p)
.title("Marker")
.draggable(true)
);
}
}
Please help me guys.Thanks in advance
Geocoder no longer provided latitude-longitude from addresses,use Reverse Geocoding API to get latitude-longitude from address.
Check Example here : Get latitude,longitude from address in Android

Categories

Resources