Can not resolve symbol GeoPoint in android studio - android

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

Related

Location within 100 km using Google Maps within Android

I am working on an Android application and Google Maps in which I am searching for the particular address by using the following code. I want to get the location of the searched address if it is in 100km within my location, and if it outside that limit, it will not show me.
I am trying to get the searched location within my 100km radius.
private class GeocoderTask extends AsyncTask<String, Void, List<Address>>{
#Override
protected List<Address> doInBackground(String... locationName) {
// Creating an instance of Geocoder class
Geocoder geocoder = new Geocoder(getBaseContext());
List<Address> addresses = null;
try {
//
//24.798406, 54.790448
//25.452403, 55.537519
// Getting a maximum of 3 Address that matches the input text
addresses = geocoder.getFromLocationName(locationName[0], 10,
24.861969, 54.857740,25.545368, 55.474347);//.getFromLocationName(locationName[0], 3);
} catch (IOException e) {
e.printStackTrace();
}
return addresses;
}
#Override
protected void onPostExecute(List<Address> addresses) {
if(addresses==null || addresses.size()==0){
Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show();
}
// Clears all the existing markers on the map
mMap.clear();
// Adding Markers on Google Map for each matching address
for(int i=0;i<addresses.size();i++){
Address address = (Address) addresses.get(i);
LatLng latLng;
// Creating an instance of GeoPoint, to display in Google Map
latLng = new LatLng(address.getLatitude(), address.getLongitude());
String addressText = String.format("%s, %s",
address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
address.getCountryName());
markerOptions = new MarkerOptions();
markerOptions.position(latLng);
// markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.flag));
markerOptions.title(addressText);
mMap.addMarker(markerOptions);
// Locate the first location
if(i==0)
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
}
You can calculate distance using this code; here, distance is in meters. You can call this function and check where the location is in the range or not.
private boolean checkForArea(int rad, LatLng fromPosition, LatLng toPosition) {
Location locationA = new Location("point A");
locationA.setLatitude(fromPosition.latitude);
locationA.setLongitude(fromPosition.longitude);
Location locationB = new Location("point B");
locationB.setLatitude(toPosition.latitude);
locationB.setLongitude(toPosition.longitude);
int distance = (int) locationA.distanceTo(locationB);
if (distance / 1000 <= rad)
return true;
else
return false;
}

Distance between user's location and a location entered by user

I want to find the distance between the location of the user and a place he enters. The entered location will be an address. Using location class I can find the coordinates of the user's location but how can I find those of the entered location. Couldn't find that in Google Maps API as well.
You can retrieve the location from an address like this:
public GeoPoint getLocationFromAddress(String strAddress) {
Geocoder coder = new Geocoder(this);
List<Address> address = new ArrayList<Address>();
try {
address = coder.getFromLocationName(strAddress,5);
if (address == null) {
return null;
}
Address location = address.get(0);
location.getLatitude();
location.getLongitude();
GeoPoint p1 = new GeoPoint((int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
return p1;
} catch (Exception e) {}
return null;
}
And for the distance use distanceBetween.

Cannot instantiate the type GeoPoint Android

I am trying to buid a simple app that when user enter his desired location its map appear.But i am getting an error Cannot instantiate the type GeoPoint i have also installed Google Play Services.
here is the code :
public class MainActivity extends MapActivity {
EditText location;
Geocoder geoCoder;
GeoPoint p;
MapController controller;
MapView mapView;
Button btnSearch;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
location=(EditText)findViewById(R.id.txtAddress);
mapView = (MapView) findViewById(R.id.mapView);
btnSearch.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
List<Address> addresses;
try {
addresses = geoCoder.getFromLocationName(location.getText().toString(),1);
if(addresses.size() > 0)
{
p = new GeoPoint( (int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude() * 1E6));
controller.animateTo(p);
controller.setZoom(12);
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
location.setText("");
}
else
{
AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);
adb.setTitle("Google Map");
adb.setMessage("Please Provide the Proper Place");
adb.setPositiveButton("Close",null);
adb.show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
Is there a particular reason you need to use v1 of the Google maps api, instead of the current v2? If not, try using LatLng to store the location, and map.moveCamera(LatLng, float zoom) to go to the desired location as shown here.

How to Search Address by Name on Google Map Android

I am a beginner of Android Developer. I am developing the Map Application. I have a function of searching address but I do not know how to search address by name using Google Map API V.2. Please suggest me the solution and some code to solve this. Thank you very much and Sorry with my English.
adderess = c.getString(c.getColumnIndex(SQLiteAdapter.KEY_CONTENT3));
// get address in string for used location for the map
/* get latitude and longitude from the adderress */
Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
try
{
List<Address> addresses = geoCoder.getFromLocationName(adderess, 5);
if (addresses.size() > 0)
{
Double lat = (double) (addresses.get(0).getLatitude());
Double lon = (double) (addresses.get(0).getLongitude());
Log.d("lat-long", "" + lat + "......." + lon);
final LatLng user = new LatLng(lat, lon);
/*used marker for show the location */
Marker hamburg = map.addMarker(new MarkerOptions()
.position(user)
.title(adderess)
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.marker)));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(user, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
}
catch (IOException e)
{
e.printStackTrace();
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_map);
editText = (EditText) findViewById(R.id.editText1);
mapView = (MapView) findViewById(R.id.mapView);
btngo = (Button) findViewById(R.id.btnmapsites);
btngo.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
List<Address> addresses;
try{
addresses = geoCoder.getFromLocationName(editText.getText().toString(),1);
if(addresses.size() > 0){
p = new GeoPoint( (int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude() * 1E6));
controller.animateTo(p);
controller.setZoom(12);
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
editText.setText("");
}else{
AlertDialog.Builder adb = new AlertDialog.Builder(SearchMapActivity.this);
adb.setTitle("Google Map");
adb.setMessage("Please Provide the Proper Place");
adb.setPositiveButton("Close",null);
adb.show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}

Map not works in map v2

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

Categories

Resources