Google Map Lag on Android - android

I've been developing an app that reads a .csv file, takes out approximately 16 latitude/longitude points, and plots them on a Google Map.
However, it seems that the points don't like to plot until I touch the screen after about a minute (I have no listeners established) [this is a separate problem that maybe someone can answer as well].
The task of getting the latitude/longitude points and plotting them is put into an AsyncTask doInBackground method. Refreshing the map's drawable state is done in the AsyncTask's onPostExecute method.
I figured that I would eliminate lag since I have the AsyncTask method working for me. When the program starts, it shows a blank map, I wait about 15 seconds and, if I touch the map, the points will plug in. However, the map is unbearably laggy at this point! It takes the app at least 5 seconds to respond to my interaction (i.e. zoom, scroll), and even then it does the action really slow...
Does anything think they know the cause of this?
Here's most of my code:
package net.learn2develop.GoogleMaps;
-- imports here
public class MapsActivity extends MapActivity
{
MapView mapView;
MapController mc;
//GeoPoint p;
//GeoPoint p2;
GeoPoint[] p99 = new GeoPoint[16];
public static String[][] bump = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapView);
LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);
#SuppressWarnings("deprecation")
View zoomView = mapView.getZoomControls();
mapView.setStreetView(true);
mapView.setSatellite(false);
zoomLayout.addView(zoomView,
new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mapView.displayZoomControls(true);
mc = mapView.getController();
// Add points from ReadCsv.java
/** try/catch to-> async was cut from here */
new MapPoint().execute(bump);
mapView.invalidate();
}
public String[][] getArray(BufferedReader bufRdr) {
-- my method that I know works
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
class MapOverlay extends com.google.android.maps.Overlay {
#Override
public boolean draw(Canvas canvas, MapView mapView,
boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
// -- array of points---
for(int i = 0; i < bump.length; i++) {
Point screenPts99 = new Point();
mapView.getProjection().toPixels(p99[i], screenPts99);
Bitmap bmp99 = BitmapFactory.decodeResource(
getResources(), R.drawable.redpin);
canvas.drawBitmap(bmp99, screenPts99.x, screenPts99.y-44, null);
}
return true;
}
}
private class MapPoint extends AsyncTask <String[][], String, String> {
#Override
protected String doInBackground(String[][]... voidThisArray) {
String voidThisString = null;
try {
InputStream is = getAssets().open("Training4.csv");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
bump = getArray(reader);
if(bump == null){
setContentView(R.layout.deleteme);
} else {
for(int i = 0; i < bump.length; i++) {
String coordinates99[] = {bump[i][0], bump[i][1]};
double lat99 = Double.parseDouble(coordinates99[0]);
double lng99 = Double.parseDouble(coordinates99[1]);
p99[i] = new GeoPoint(
(int) (lat99 * 1E6),
(int) (lng99 * 1E6));
MapOverlay mapOverlay99 = new MapOverlay();
List<Overlay> listOfOverlays99 = mapView.getOverlays();
listOfOverlays99.add(mapOverlay99);
mapView.refreshDrawableState();
}
}
} catch (IOException e) {
e.printStackTrace();
}
return voidThisString;
}
#Override
protected void onPostExecute(String voidThisString) {
super.onPostExecute(voidThisString);
mapView.refreshDrawableState();
mapView.invalidate();
}
}
}

in onPostExecute use the mapView.invalidate(); method it'll refresh it and whatever you done in inBackground() that will be refreshing by this method

you add the MapOverlay in for loop so what happen each overlay contain bump.length points.
so it will getting slow add dupplication of point.
first get all the lat/lng and store in p99 array and then add the mapoverlay
implement code
try {
InputStream is = getAssets().open("Training4.csv");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
bump = getArray(reader);
if(bump == null){
setContentView(R.layout.deleteme);
} else {
for(int i = 0; i < bump.length; i++) {
String coordinates99[] = {bump[i][0], bump[i][1]};
double lat99 = Double.parseDouble(coordinates99[0]);
double lng99 = Double.parseDouble(coordinates99[1]);
p99[i] = new GeoPoint(
(int) (lat99 * 1E6),
(int) (lng99 * 1E6));
}
MapOverlay mapOverlay99 = new MapOverlay();
List<Overlay> listOfOverlays99 = mapView.getOverlays();
listOfOverlays99.add(mapOverlay99);
mapView.refreshDrawableState();
}
} catch (IOException e) {
e.printStackTrace();
}
your draw()
public boolean draw(Canvas canvas, MapView mapView,
boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
// -- array of points---
if(bump!=null){
for(int i = 0; i < bump.length; i++) {
Point screenPts99 = new Point();
mapView.getProjection().toPixels(p99[i], screenPts99);
Bitmap bmp99 = BitmapFactory.decodeResource(
getResources(), R.drawable.redpin);
canvas.drawBitmap(bmp99, screenPts99.x, screenPts99.y-44, null);
}
}
return true;
}

Related

Overlay not drawing on the right spot

I have an extremely weird issue.
First of all, when I zoom in and out of a MapView, the marker (overlay) moves to incorrect places (doesn't work well with scaling).
Secondly, the market is being drawn at the wrong position!
I'll post code below but first listen to this:
I'm in Islamabad. The GeoCoder also tells me I'm in Islamabad. But when I draw a circle around my location using overlays, it draws it in a city that's 100s of kilometers away.
Kindly help me with both the problems.
Here's the activity:
public class LocatePickup extends MapActivity implements LocationListener {
Geocoder gc;
MapView mapView;
MapController mc;
GeoPoint p;
double lat = 0;
double lng = 0;
LocationManager lm;
WebMethods wm;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.locate_pickup);
mapView = (MapView) findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
mapView.setStreetView(true);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000L, 500.0f, this);
Location l = new Location(LocationManager.NETWORK_PROVIDER);
l = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
mc = mapView.getController();
p = new GeoPoint((int) (l.getLatitude()), (int) (l.getLongitude()));
mc.setCenter(p);
mc.setZoom(14);
MapOverlay myLocationOverlay = new MapOverlay();
List<Overlay> list = mapView.getOverlays();
list.add(myLocationOverlay);
gc = new Geocoder(this);
try {
List<Address> address = gc.getFromLocation(l.getLatitude(), l.getLongitude(), 1);
Toast.makeText(this, ""+address.get(0).getAddressLine(1), 1).show();
}
catch (IOException e) {
e.printStackTrace();
}
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
class MapOverlay extends com.google.android.maps.Overlay {
Paint paint = new Paint();
#Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
super.draw(canvas, mapView, shadow);
Point myScreenCoords = new Point();
mapView.getProjection().toPixels(p, myScreenCoords);
paint.setStrokeWidth(1);
paint.setARGB(255, 218, 28, 28);
paint.setStyle(Paint.Style.STROKE);
//Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pin);
canvas.drawCircle(myScreenCoords.x, myScreenCoords.y, 15, paint);
//(bmp, myScreenCoords.x, myScreenCoords.y - 256, paint);
canvas.drawText("Hey!", myScreenCoords.x, myScreenCoords.y, paint);
return true;
}
}
#Override
public void onLocationChanged(Location arg0) {
if(arg0 != null) {
Log.d("New Location: ", arg0.getLatitude() + " - " + arg0.getLongitude());
lat = arg0.getLatitude();
lng = arg0.getLongitude();
p = new GeoPoint((int) lat * 1000000, (int) lng * 1000000);
mc.animateTo(p);
}
}
#Override
public void onProviderDisabled(String provider) {
// empty
}
#Override
public void onProviderEnabled(String provider) {
// empty
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
//empty
}
}
Once the code is complete, I'd like the user to be able to tap a spot on the map and get its address (the marker should move to that spot as well). But it should be accurate.
Since no one answered this question correctly..
For some reason, the normal Overlay class wasn't working for me the way it should have. I switched to ItemizedOverlay and now everything is perfect.
I'm no Android expert so I'd like someone with better experience to comment on this but, I feel that ItemizedOverlay is much better than the simple Overlay. With itemizedoverlay, zooming in and out of the mapview with an object drawn at a fixed point works the way it should, the pin (object) stays exactly where it should stay (that wasn't the case previously).
Here's some code for those who are looking for it. This is of course incomplete, and for a specific purpose, but you get the general idea.
`class CustomOverlay extends com.google.android.maps.ItemizedOverlay
{
private ArrayList mOverlays = new ArrayList();
Context mContext;
public CustomOverlay(Drawable defaultMarker)
{
super(defaultMarker);
}
public void addOverlay(OverlayItem overlay)
{
mOverlays.add(overlay);
populate();
}
#Override
protected OverlayItem createItem(int i)
{
return mOverlays.get(i);
}
#Override
public int size()
{
return mOverlays.size();
}
public CustomOverlay(Drawable defaultMarker, Context context)
{
super(boundCenterBottom(defaultMarker));
mContext = context;
}
#Override
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
if (event.getAction() == 1)
{
p = mapView.getProjection().fromPixels((int) event.getX(),
(int) event.getY());
gc = new Geocoder(getBaseContext(), Locale.getDefault());
try
{
address = gc.getFromLocation(
(double) p.getLatitudeE6() / 1E6,
(double) p.getLongitudeE6() / 1E6, 1);
addressfound = true;
}
catch (IOException e)
{
addressfound = false;
e.printStackTrace();
}
if (address.size() != 0)
{
l1 = address.get(0).getAddressLine(0);
l2 = address.get(0).getAddressLine(1);
l3 = address.get(0).getAddressLine(2);
}
OverlayItem point = new OverlayItem(p, "Location", l1 + ", "
+ l2 + ", " + l3);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = getBaseContext().getResources()
.getDrawable(R.drawable.androidmarker);
CustomOverlay itemizedoverlay = new CustomOverlay(drawable,
getBaseContext());
itemizedoverlay.addOverlay(point);
mapOverlays.clear();
mapOverlays.add(itemizedoverlay);
mapView.invalidate();
}`

Can't animate to a valid geopoint on button click

One part of the application I am making requires google map. I want the user to be able to type into edittext some search string and then press a search button and the map will animate to the first result.
When I press the button the application searches and takes the first result and puts it into a geopoint, so that part of the program is working. But when I try to animate to that point the application crashes.
Here is the onCreate function where I successfully navigate to the location "Dalvík, Iceland".
public class LocationPicker extends MapActivity {
static GeoPoint point;
static MapController mc;
static MapView mapView;
private EditText location;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location_picker);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
location = (EditText)findViewById(R.id.locationString);
mc = mapView.getController();
String tmp = "Dalvík, Iceland";
try {
point = searchLocation(tmp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mc.animateTo(point);
mc.setZoom(14);
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
}
And my code for the button click is, and to be clear the System.out.println(point) prints valid point. But still when I click the button my application crashes.
public void search(View v) throws IOException{
GeoPoint tmpPoint = searchLocation(location.getText().toString());
System.out.println(tmpPoint);
if( tmpPoint != null){
mc.animateTo(tmpPoint);
mapView.invalidate();
}
}
And the searchLocation functions is as follows:
public GeoPoint searchLocation (String searchString) throws IOException{
Geocoder geo = new Geocoder(this);
List<Address> addr;
addr = geo.getFromLocationName(searchString, 10);
if(!addr.isEmpty()){
Address loc = addr.get(0);
GeoPoint point = new GeoPoint((int) (loc.getLatitude() * 1E6),
(int) (loc.getLongitude() * 1E6));
return point;
}
else {
return null;
}
}
So to summerize I am clearly doing something wrong in the onclick "search" function.
Any ideas what is wrong?
You are not checking the right variable for null value:
if( point != null){
mc.animateTo(tmpPoint);
mapView.invalidate();
}
should be
if( tmpPoint!= null){
mc.animateTo(tmpPoint);
mapView.invalidate();
}
Additionnally, you want to make the call to the geoCoder.getFromLocationName method outside the UI thread as this can be time consumming. Use an AsyncTask for that for instance.
Finally, even if you get a list of addresses, they are not guaranted to have latitude and longitude. Use the hasLatitude and hasLongitude functions to pick the first address of the result list with coordinates.
I found the answer.
The problem causing this was this line
mapView.invalidate();

Multiple marker in google map using JSON

I have to make a multiple marker google map using JSON parsing. Any one having helping code of it. So that i can gain an idea on it. The activity i have made is not working exactly what i want.
This is my activity
public class MapsActivity extends MapActivity{
private MapController mapControll;
private GeoPoint geoPoint=null;
private MapView mapview;
private MyItemizedOverlay userPicOverlay;
private MyItemizedOverlay nearPicOverlay;
private Drawable userPic,atmPic;
private OverlayItem nearatms[] = new OverlayItem[50];
public static Context context;
public double latitude;
public double longitude;
private List<String> lat = new ArrayList<String>();
private List<String> lon = new ArrayList<String>();
private String[] get_lati;
private String[] get_long;
#Override
protected void onCreate(Bundle icicle) {
// TODO Auto-generated method stub
super.onCreate(icicle);
context = getApplicationContext();
setContentView(R.layout.map);
lat.clear();
lon.clear();
Intent in = getIntent();
if(in!=null){
get_lati = in.getStringArrayExtra("LAT_KEY");
get_long = in.getStringArrayExtra("LONG_KEY");
System.out.println("lat===="+get_lati.length+" lon==="+get_long.length);
}
for(int i = 0;i<get_lati.length;i++){
String lt = null;
String ln = null;
lt =get_lati[i];
ln =get_long[i];
System.out.println("lt=="+lt+"==ln=="+ln);
}
showMap();
}
public void showMap() {
// TODO Auto-generated method stub
try {
double lat_dbl = Double.parseDouble(get_lati[0]);
double lon_dbl = Double.parseDouble(get_long[0]);
geoPoint = new GeoPoint((int)(lat_dbl * 1E6),(int)(lon_dbl * 1E6));
mapview = (MapView)findViewById(R.id.mapView);
mapControll= mapview.getController();
mapview.setBuiltInZoomControls(true);
mapview.setStreetView(true);
mapControll.setZoom(16);
mapControll.animateTo(geoPoint);
userPic = this.getResources().getDrawable(R.drawable.pin);
userPicOverlay = new MyItemizedOverlay(userPic);
OverlayItem overlayItem = new OverlayItem(geoPoint, "I'm Here!!!", null);
userPicOverlay.addOverlay(overlayItem);
mapview.getOverlays().add(userPicOverlay);
atmPic = this.getResources().getDrawable(R.drawable.pin);
nearPicOverlay = new MyItemizedOverlay(atmPic);
for (int i = 0; i < 5; i++) {
nearatms[i] = new OverlayItem(new GeoPoint((int)((latitude) * 1E6), i),"Name", null);//just check the brackets i just made change here so....
nearPicOverlay.addOverlay(nearatms[i]);
}
mapview.getOverlays().add(nearPicOverlay);
//Added symbols will be displayed when map is redrawn so force redraw now
mapview.postInvalidate();
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> myOverlays ;
public MyItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
myOverlays = new ArrayList<OverlayItem>();
populate();
}
public void addOverlay(OverlayItem overlay){
myOverlays.add(overlay);
populate();
}
#Override
protected OverlayItem createItem(int i) {
return myOverlays.get(i);
}
// Removes overlay item i
public void removeItem(int i){
myOverlays.remove(i);
populate();
}
// Returns present number of items in list
#Override
public int size() {
return myOverlays.size();
}
public void addOverlayItem(OverlayItem overlayItem) {
myOverlays.add(overlayItem);
populate();
}
public void addOverlayItem(int lat, int lon, String title) {
try {
GeoPoint point = new GeoPoint(lat, lon);
OverlayItem overlayItem = new OverlayItem(point, title, null);
addOverlayItem(overlayItem);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
#Override
protected boolean onTap(int index) {
// TODO Auto-generated method stub
String title = myOverlays.get(index).getTitle();
//Toast.makeText(MapActivity.this, title, Toast.LENGTH_LONG).show();
return super.onTap(index);
}
}
}
You are creating a geopoint with latitude but 'i' instead of longitude.
new GeoPoint((int)((latitude) * 1E6), i)
instead of:
new GeoPoint((int)((latitude) * 1E6), (int)((longitude) * 1E6)) // + some variation based on i
Thus all the points fall very near to each other.
I assume that you're using 'i' in order to create separate point near your location, and that in the final product you would use an ATM point DB. I suggest that you create a function that is supposed to retrieve ATM locations, and for testing purposes either return fixed (but real) locations, or randomize around your lat/lng. (This is also a better coding practice.)
For debug purposes, log the actual points you are creating, after you created them. if all lat/lng numbers look the same, you have a clue at what's going on. if only two points are logged... that's a different problem. :-)

Bubble pop up box

Google map, it allows us to pin-point any location we want. After pin-pointing, there will be this "Bubble" pop up box at the top of the push-pin. May i know how to do the pop up window box? Any codes to show? I am using alert dialog box now, but i want it to be "Bubble" pop up window instead.
List<Overlay> mapOverlays = mapView.getOverlays();
MapOverlay mapOverlay = new MapOverlay();
mapOverlays.add(mapOverlay);
// obtain gps location
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
lm.requestLocationUpdates(
// LocationManager.GPS_PROVIDER,
LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
private class MyLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location loc) {
k = new GeoPoint((int) (loc.getLatitude() * 1E6),
(int) (loc.getLongitude() * 1E6));
mc.animateTo(k);
mc.setZoom(18);
// Add a location marker
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listofOverlays = mapView.getOverlays();
listofOverlays.clear();
listofOverlays.add(mapOverlay);
// invalidate() method forces the MapView to be redrawn
mapView.invalidate();
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
class MapOverlay extends com.google.android.maps.Overlay {
#Override
public boolean onTap(final GeoPoint p, MapView mapView) {
// TODO Auto-generated method stub
k = p;
mc = mapView.getController();
mc.animateTo(p);
mapView.invalidate();
Geocoder geoCoder = new Geocoder(getBaseContext(),
Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(
p.getLatitudeE6() / 1E6, p.getLongitudeE6() / 1E6, 1);
String add = "";
if (addresses.size() > 0) {
for (int i = 0; i < addresses.get(0)
.getMaxAddressLineIndex(); i++)
add += addresses.get(0).getAddressLine(i) + "\n";
txtAddress.setText("Address: " + add);
}
#Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
super.draw(canvas, mapView, shadow);
if (k != null) {
// ---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(k, screenPts);
// ---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.passenger);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
}
return true;
}
}
}
I'm currently working on a mapping application where I also needed to display 'Bubble' popups and the following blog posts helped me quite a bit.
This first post shows how to use a 9-patch image embedded in a view to produce a popup. The code is ok but does leave a number of questions unanswered, some commenters have requested some additional source code for clarification.
This post from the Android Developers blog explains what a 9 patch image is an how to create one.
Using these two posts I was able to pull some code together which works well so hopefully you will be able to manipulate it to fit your needs too.
Sounds like you are looking for the InfoWindow. By the way, in newer versions of the API, they do not have rounded corners (they look more like boxes and less like bubbles). You can restore the old ones by requesting a previous version of the API in the javascript URL that loads the maps API.
Google doesn't provide an API to do this, take a look at this. Victor's answer gives a link to open source code which fits the bill.

Show marker on google map position obtained from xml

I want to show a marker on google map position obtained from xml after parsing. So I store latitude and longitude in an arrylist. I want to show but it forse close
I pass lat and long from geopoint so please can you take a look where is wrong in my code
public class XMLParsingExample1 extends MapActivity {
/**
* Create Object For SiteList Class
*/
SitesList sitesList = null;
private MapController mapController;
private MapView mapView;
private LocationManager locationManager;
private String ss1;
private String ss2;
GeoPoint p;
class MapOverlay extends com.google.android.maps.Overlay {
#Override
public boolean draw(Canvas canvas, MapView mapView,
boolean shadow, long when) {
super.draw(canvas, mapView, shadow);
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.marker);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
return true;
}
}
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** Create a new layout to display the view */
try {
/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
/** Send URL to parse XML Tags */
URL sourceUrl = new URL(
"http://site4demo.com/artealdiaonline/output.php?lat=-34.6394879&lng=-58.3617837kkj");
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
/** Get result from MyXMLHandler SitlesList Object */
sitesList = MyXMLHandler.sitesList;
ArrayList<Integer> Latitude = new ArrayList<Integer>();
ArrayList<Integer> Longtitude = new ArrayList<Integer>();
for (int i = 0; i < sitesList.getName().size(); i++) {
// **Is this correct??**
ss1 = sitesList.getName().get(i);
}
for (int i = 0; i < sitesList.getWebsite().size(); i++) {
ss2 = sitesList.getWebsite().get(i);
}
RelativeLayout linearLayout = (RelativeLayout) findViewById(R.id.mainlayout);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
//mapView.setStreetView(true);
mapView.setSatellite(true);
mapController = mapView.getController();
mapController.setZoom(14); // Zoon 1 is world view
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, new GeoUpdateHandler());
//Here i pass latitude and longitude value for displaying in map
String coordinates[] = {ss1, ss2};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
mapController.animateTo(p);
mapController.setZoom(17);
//---Add a location marker---
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
public class GeoUpdateHandler implements LocationListener {
#Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude() * 1E6);
int lng = (int) (location.getLongitude() * 1E6);
GeoPoint point = new GeoPoint(lat, lng);
mapController.animateTo(point); // mapController.setCenter(point);
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}
sites list is
public class SitesList {
/** Variables */
private ArrayList<String> Latitude = new ArrayList<String>();
private ArrayList<String> Longitude = new ArrayList<String>();
private ArrayList<String> category = new ArrayList<String>();
/** In Setter method default it will return arraylist
* change that to add */
public ArrayList<String> getName() {
return Latitude;
}
public void setName(String name) {
this.Latitude.add(name);
}
public ArrayList<String> getWebsite() {
return Longitude;
}
public void setWebsite(String website) {
this.Longitude.add(website);
}
public ArrayList<String> getCategory() {
return category;
}
public void setCategory(String category) {
this.category.add(category);
}
}
I can see at least one case, where exception can be thrown.
If you have no any elements in Latitude and/or Longitude lists in SitesList class, or String values in those lists can't be parsed to double, the following lines will throw NumberFormatException
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);

Categories

Resources