Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9240491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:11:56+00:00 2026-06-18T08:11:56+00:00

I am currently developing an app for Android that uses the Google Maps API

  • 0

I am currently developing an app for Android that uses the Google Maps API v2. I am showing custom markers from an external XML file: http://dds.orgfree.com/DDS/landmarks_genxmlv2.php , which will show some landmarks in the map.

As you can see in my code below, I am using StrictMode to avoid NetworkOnMainThreadException while testing this app in an actual phone. I know that it is not advisable to used this method. It should be done in a separate thread and in background through AsyncTask. I tried implementing AsyncTask in this app but I have no luck. Can anyone help me to furnish my code or give some good suggestions that will help me with this? Thanks a lot in advance.

public class MapViewActivity extends android.support.v4.app.FragmentActivity {

    static final LatLng CITYHALL = new LatLng(07.0644444, 125.6077778);
    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_mapview);

        setUpMapIfNeeded();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.mapview_options_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.normal_mapstyle) {
            mMap.setMapType(MAP_TYPE_NORMAL);
        } else if (item.getItemId() == R.id.hybrid_mapstyle) {
            mMap.setMapType(MAP_TYPE_HYBRID);
        } else if (item.getItemId() == R.id.satellite_mapstyle) {
            mMap.setMapType(MAP_TYPE_SATELLITE);
        } else if (item.getItemId() == R.id.terrain_mapstyle) {
            mMap.setMapType(MAP_TYPE_TERRAIN);
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    private void setUpMapIfNeeded() {

        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map)).getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    private void setUpMap() {

        Document doc = null;
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            doc = builder
                    .parse("http://dds.orgfree.com/DDS/landmarks_genxmlv2.php");
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        NodeList markers = doc.getElementsByTagName("marker");

        try {
            for (int i = 0; i < markers.getLength(); i++) {
                Element item = (Element) markers.item(i);
                String name = item.getAttribute("name");
                String address = item.getAttribute("address");
                String stringLat = item.getAttribute("lat");
                String stringLng = item.getAttribute("lng");
                String icon = item.getAttribute("icon");
                Double lat = Double.valueOf(stringLat);
                Double lng = Double.valueOf(stringLng);

                mMap = ((SupportMapFragment) getSupportFragmentManager()
                        .findFragmentById(R.id.map)).getMap();

                mMap.addMarker(new MarkerOptions()
                        .position(new LatLng(lat, lng))
                        .title(name)
                        .snippet(address)
                        .icon(BitmapDescriptorFactory.fromAsset(new String(icon
                                + ".png"))));

                // Move the camera instantly to City Hall with a zoom of 15.
                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(CITYHALL, 15));
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }
}

Here’s what I have tried.

public class MapViewActivity extends android.support.v4.app.FragmentActivity {

    static final LatLng CITYHALL = new LatLng(07.0644444, 125.6077778);
    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_mapview);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();

        StrictMode.setThreadPolicy(policy);
        setUpMapIfNeeded();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.mapview_options_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.map_menu_showlegend) {
            Log.d("MENU", "Show Legend was CLICKED!");
        } else if (item.getItemId() == R.id.map_menu_maptype) {
            Log.d("MENU", "Map Type was CLICKED!");
        } else if (item.getItemId() == R.id.normal_mapstyle) {
            Log.d("MENU", "MAP_TYPE_NORMAL was CLICKED!");
            mMap.setMapType(MAP_TYPE_NORMAL);
        } else if (item.getItemId() == R.id.hybrid_mapstyle) {
            Log.d("MENU", "MAP_TYPE_HYBRID was CLICKED!");
            mMap.setMapType(MAP_TYPE_HYBRID);
        } else if (item.getItemId() == R.id.satellite_mapstyle) {
            Log.d("MENU", "MAP_TYPE_SATELLITE was CLICKED!");
            mMap.setMapType(MAP_TYPE_SATELLITE);
        } else if (item.getItemId() == R.id.terrain_mapstyle) {
            Log.d("MENU", "MAP_TYPE_TERRAIN was CLICKED!");
            mMap.setMapType(MAP_TYPE_TERRAIN);
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the
        // map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map)).getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    private Boolean isOnline() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if (ni != null && ni.isConnected())
            return true;

        return false;
    }

    private void setUpMap() {
        new ParseXML().execute();
    }

    private class ParseXML extends AsyncTask<Void, Void, Document> {
        Document doc = null;

        @Override
        protected Document doInBackground(Void... params) {

            try {
                Thread.sleep(2000);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }

            if (isOnline()) {

                try {
                    DocumentBuilderFactory factory = DocumentBuilderFactory
                            .newInstance();
                    DocumentBuilder builder = factory.newDocumentBuilder();
                    doc = builder
                            .parse("http://dds.orgfree.com/DDS/landmarks_genxmlv2.php");
                } catch (ParserConfigurationException e) {
                    e.printStackTrace();
                } catch (SAXException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                NodeList markers = doc.getElementsByTagName("marker");

                try {
                    for (int i = 0; i < markers.getLength(); i++) {
                        Element item = (Element) markers.item(i);
                        String name = item.getAttribute("name");
                        String address = item.getAttribute("address");
                        String stringLat = item.getAttribute("lat");
                        String stringLng = item.getAttribute("lng");
                        String icon = item.getAttribute("icon");
                        Double lat = Double.valueOf(stringLat);
                        Double lng = Double.valueOf(stringLng);

                        mMap = ((SupportMapFragment) getSupportFragmentManager()
                                .findFragmentById(R.id.map)).getMap();

                        mMap.addMarker(new MarkerOptions()
                                .position(new LatLng(lat, lng))
                                .title(name)
                                .snippet(address)
                                .icon(BitmapDescriptorFactory
                                        .fromAsset(new String(icon + ".png"))));

                        // Move the camera instantly to City Hall with a zoom of
                        // 15.
                        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
                                CITYHALL, 15));
                    }
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            } else {
                Toast.makeText(MapViewActivity.this, "No connection..",
                        Toast.LENGTH_LONG).show();
            }

            return doc;
        }

        @Override
        protected void onPostExecute(Document doc) {
            super.onPostExecute(doc);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:11:57+00:00Added an answer on June 18, 2026 at 8:11 am

    You cannot change UI from background thread.
    Therefore move everything that changes UI to onPostExecute() method.

    I already answered today the same question with code example : Android disable button on successful doinbackground call

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing an app that uses the Google Maps api to give you a
I am currently developing an android application uses Google map API. I am wondering
I'm currently developing the presentation layer of an android app. The api which I'm
A new question about android and services. Currently I'm developing a App that should
i'm developing an app using google drive SDK currently on Android using eclipse, i
I'm currently making an app that can upload and download file from Google Drive.
I'm trying to use the new google maps android api v2 and i'm developing
I am currently developing an Android app that is using a remote MySQL database
I am currently developing an android app that displays a list view. When an
I have an Android app that I am currently developing. I have created icons

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.