Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何将多个标记添加到GoogleMap中存储在数组中的位置_Android_Google Maps_Android Asynctask_Google Maps Markers - Fatal编程技术网

Android 如何将多个标记添加到GoogleMap中存储在数组中的位置

Android 如何将多个标记添加到GoogleMap中存储在数组中的位置,android,google-maps,android-asynctask,google-maps-markers,Android,Google Maps,Android Asynctask,Google Maps Markers,我正在编写一个Android应用程序,它将多个用户的位置存储到一个远程数据库中,然后任何用户都可以获得这些位置并将它们放在谷歌地图上 我可以成功地存储位置。我可以成功地将所有位置作为一个数组获取。我在地图上绘制它们时遇到了麻烦:应用程序总是崩溃 这是我的代码,添加标记在“在后台执行”部分的“attemptLoad”类中: package com.paul.locations; 导入java.text.simpleDataFormat; 导入java.util.ArrayList; 导入java.

我正在编写一个Android应用程序,它将多个用户的位置存储到一个远程数据库中,然后任何用户都可以获得这些位置并将它们放在谷歌地图上

我可以成功地存储位置。我可以成功地将所有位置作为一个数组获取。我在地图上绘制它们时遇到了麻烦:应用程序总是崩溃

这是我的代码,添加标记在“在后台执行”部分的“attemptLoad”类中:

package com.paul.locations;
导入java.text.simpleDataFormat;
导入java.util.ArrayList;
导入java.util.Calendar;
导入java.util.HashMap;
导入java.util.List;
导入org.apache.http.NameValuePair;
导入org.apache.http.message.BasicNameValuePair;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入com.paul.locations.JSONParser;
导入com.paul.locations.R;
导入com.google.android.gms.maps.GoogleMap;
导入com.google.android.gms.maps.MapFragment;
导入com.google.android.gms.maps.model.LatLng;
导入com.google.android.gms.maps.model.MarkerOptions;
导入android.location.LocationManager;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.preference.PreferenceManager;
导入android.app.Activity;
导入android.app.ProgressDialog;
导入android.content.Context;
导入android.content.SharedReferences;
导入android.util.Log;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.Toast;
公共类位置扩展活动实现OnClickListener{
私人谷歌地图谷歌地图;
私有字符串lat=“”,lon=“”,current=“”,lati,loni;
私人按钮区,b商店;
私人花车啦,啦;
//进度对话框
私人对话;
//JSON解析器类
JSONParser JSONParser=新的JSONParser();
//php添加注释脚本
//本地主机:
//在您的设备上进行测试
//将本地ip改为在windows上运行CMD>ipconfig
//或者在mac的终端类型ifconfig中,查找en0或en1下的ip
私有静态最终字符串加载\u信息\u URL=”http://192.168.1.14/locations/load_infos.php";
私有静态最终字符串更新\u信息\u URL=”http://192.168.1.14/locations/update_info.php";
//在模拟器上测试:
//私有静态最终字符串POST_COMMENT_URL=”http://10.0.2.2:1234/webservice/addcomment.php";
//从真实服务器进行测试:
//私有静态最终字符串POST_COMMENT_URL=”http://www.mybringback.com/webservice/addcomment.php";
//身份证
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串标记_MESSAGE=“MESSAGE”;
私有静态最终字符串标记_LON=“LON”;
私有静态最终字符串标记_LAT=“LAT”;
私有静态最终字符串标记\u USERNAME=“USERNAME”;
私有静态最终字符串标记_POSTS=“POSTS”;
私有静态最终字符串标记\u TIME=“TIME”;
私有静态最终字符串标记\u ID=“ID”;
//产品JSONArray
JSONArray产品=null;
ArrayList productsList;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
setUpMap();
//ListView的Hashmap
productsList=新的ArrayList();
bLoad=(按钮)findViewById(R.id.bLoad);
bLoad.setOnClickListener(this);
b存储=(按钮)findViewById(R.id.b存储);
bStore.setOnClickListener(this);
}
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
开关(v.getId()){
案例R.id.bLoad:
新建AttemptLoad().execute();
打破
案例R.id.B存储:
LocationManager lm=(LocationManager)getSystemService(Context.LOCATION\u服务);
android.location.location location=lm.getlastnownlocation(LocationManager.GPS\u提供程序);
lon=Double.toString(location.getLongitude());
lat=Double.toString(location.getLatitude());
系统输出打印LN(lon+“,”+lat);
Calendar c=Calendar.getInstance();
SimpleDataFormat df=新的SimpleDataFormat(“HH:mm:ss”);
current=df.format(c.getTime());
系统输出打印项次(当前);
新的AttemptStore().execute();
打破
违约:
打破
}
}
类AttemptLoad扩展了AsyncTask{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(Location.this);
setMessage(“正在尝试加载位置…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
//TODO自动生成的方法存根
//实例化arraylist以包含所有JSON数据。
//我们将使用一组键值对,引用
//例如,json元素名和内容,
//在标签上留言,在内容上写上“我太棒了”。
//建筑参数
List params1=new ArrayList();
//从URL获取JSON字符串
JSONObject json=jsonParser.makeHttpRequest(加载信息URL,“GET”,参数1);
//检查日志cat中的JSON响应
Log.d(“所有产品:,json.toString());
//在解析JSON内容时,我们可能应该
//尝试捕获任何异常:
试一试{
//检查成功标签
int success=json.getInt(TAG_success);
如果(成功==1){
//发现的产品
//通用电气
package com.paul.locations;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.paul.locations.JSONParser;
import com.paul.locations.R;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


public class Location extends Activity implements OnClickListener {

    private GoogleMap googlemap;
    private String lat="", lon="", current = "", lati, loni;
    private Button  bLoad, bStore;
    private Float la, lo;

     // Progress Dialog
    private ProgressDialog pDialog;

    // JSON parser class
    JSONParser jsonParser = new JSONParser();

    //php add a comment script

    //localhost :  
    //testing on your device
    //put your local ip instead,  on windows, run CMD > ipconfig
    //or in mac's terminal type ifconfig and look for the ip under en0 or en1
    private static final String LOAD_INFO_URL = "http://192.168.1.14/locations/load_infos.php";
    private static final String UPDATE_INFO_URL = "http://192.168.1.14/locations/update_info.php";


    //testing on Emulator:
    //private static final String POST_COMMENT_URL = "http://10.0.2.2:1234/webservice/addcomment.php";

    //testing from a real server:
    //private static final String POST_COMMENT_URL = "http://www.mybringback.com/webservice/addcomment.php";

    //ids
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MESSAGE = "message";
    private static final String TAG_LON = "lon";
    private static final String TAG_LAT = "lat";
    private static final String TAG_USERNAME = "username";
    private static final String TAG_POSTS = "posts";
    private static final String TAG_TIME = "time";
    private static final String TAG_ID = "id";

    // products JSONArray
    JSONArray products = null;

    ArrayList<HashMap<String, String>> productsList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

            setContentView(R.layout.map);
            setUpMap();
            // Hashmap for ListView
            productsList = new ArrayList<HashMap<String, String>>();


        bLoad = (Button)findViewById(R.id.bLoad);
        bLoad.setOnClickListener(this);
        bStore = (Button)findViewById(R.id.bStore);
        bStore.setOnClickListener(this);        
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.bLoad:
            new AttemptLoad().execute();
            break;
        case R.id.bStore:
            LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
            android.location.Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            lon = Double.toString(location.getLongitude());
            lat = Double.toString(location.getLatitude());
            System.out.println(lon + ", " + lat);
            Calendar c = Calendar.getInstance();
            SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
            current = df.format(c.getTime());
            System.out.println(current);
            new AttemptStore().execute();
            break;

        default:
            break;
        }
    }

    class AttemptLoad extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Location.this);
            pDialog.setMessage("Attempting to load locations...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            // Instantiate the arraylist to contain all the JSON data.
            // we are going to use a bunch of key-value pairs, referring
            // to the json element name, and the content, for example,
            // message it the tag, and "I'm awesome" as the content.

            // Building Parameters
                        List<NameValuePair> params1 = new ArrayList<NameValuePair>();
                        // getting JSON string from URL
                        JSONObject json = jsonParser.makeHttpRequest(LOAD_INFO_URL, "GET", params1);

                        // Check your log cat for JSON reponse
                        Log.d("All Products: ", json.toString());

            // when parsing JSON stuff, we should probably
            // try to catch any exceptions:
            try {

                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // products found
                    // Getting Array of Products
                    products = json.getJSONArray(TAG_POSTS);

                    // looping through All Products
                    for (int i = 0; i < products.length(); i++) {
                        JSONObject c = products.getJSONObject(i);

                        // Storing each json item in variable
                        loni = c.getString(TAG_LON);
                        String username = c.getString(TAG_USERNAME);
                        lati = c.getString(TAG_LAT);
                        String time = c.getString(TAG_TIME);
                        String id = c.getString(TAG_ID);
                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put(TAG_LON, loni);
                        map.put(TAG_LAT, lati);
                        map.put(TAG_USERNAME, username);
                        map.put(TAG_TIME, time);
                        map.put(TAG_ID, id);

                        // adding HashList to ArrayList
                        productsList.add(map);


                        la = Float.valueOf(lati);
                        lo = Float.valueOf(loni);


                        System.out.println(la);
                        System.out.println(lo);

                        //this is where i try to put all the markers on the map and where the app crashes
                        googlemap.addMarker(new MarkerOptions()
                        .position(new LatLng(la, lo))
                        .title("Hello world"));

                }
                }       

            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        protected void onPostExecute(String file_url) {
            // dismiss the dialog once product deleted
            pDialog.dismiss();
            if (file_url != null){
                Toast.makeText(Location.this, file_url, Toast.LENGTH_LONG).show();
            }
        }
    }

    class AttemptStore extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Location.this);
            pDialog.setMessage("Attempting to store your location...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... args) {
            // TODO Auto-generated method stub
            String success;

            SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Location.this);
            String post_username = sp.getString("username", "anon");

            try {
                // Building Parameters
                List<NameValuePair> params1 = new ArrayList<NameValuePair>();
                params1.add(new BasicNameValuePair("username", post_username));
                params1.add(new BasicNameValuePair("lat", lat));
                params1.add(new BasicNameValuePair("lon", lon));
                params1.add(new BasicNameValuePair("time", current));

                Log.d("request!", "starting");

                //Posting user data to script 
                JSONObject json = jsonParser.makeHttpRequest(
                        UPDATE_INFO_URL, "POST", params1);

                // full json response
                Log.d("Store location attempt", json.toString());

                // json success element
                success = json.getString(TAG_SUCCESS);
                if(success.equals("SUCCESS")) {
                    Log.d("Location Stored!", json.toString());    
                    finish();
                    return json.getString(TAG_MESSAGE);
                }else{
                    Log.d("Storing Location Failure!", json.getString(TAG_MESSAGE));
                    return json.getString(TAG_MESSAGE);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }           
            return null;
        }

        protected void onPostExecute(String file_url) {
            // dismiss the dialog once product deleted
            pDialog.dismiss();
            if (file_url != null){
                Toast.makeText(Location.this, file_url, Toast.LENGTH_LONG).show();
            }
        }
    }

    private void setUpMap() {
        if(googlemap == null){
            googlemap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

            if(googlemap != null){
                //code to initialize the map
                googlemap.setMyLocationEnabled(true);
            }
        }
    }   
}
I/System.out(19285): -117.21224441, 32.75089894
I/System.out(19285): 13:18:33
D/request!(19285): starting
I/Adreno200-EGLSUB(19285): <ConfigWindowMatch:2087>: Format RGBA_8888.
D/Store location attempt(19285): {"message":"Post Successfully  Added!","success":1}
D/Storing Location Failure!(19285): Post Successfully Added!
I/Adreno200-EGLSUB(19285): <ConfigWindowMatch:2087>: Format RGBA_8888.
D/All Products:(19285): {"posts":[{"id":"1","time":"13:18:33","lon":"-117.21224441","username":"paul","lat":"32.75089894"},{"id":"2","time":"01:00:30","lon":"13","lat":"13.01"},{"id":"3","time":"12:00:59","lon":"12.51","username":"ed","lat":"12.52"},{"id":"4","time":"14:00:00","lon":"20","username":"eric","lat":"20"},{03:03","lon":"17.01","username":"sumner","lat":"16.01"}],"success":1}
I/System.out(19285): 32.7509
I/System.out(19285): -117.21224
W/dalvikvm(19285): threadid=28: thread exiting with uncaught exception (group=0x41077438)

E/AndroidRuntime(19285): FATAL EXCEPTION: AsyncTask #3
E/AndroidRuntime(19285): java.lang.RuntimeException: An error occured while executing doInBackground()
E/AndroidRuntime(19285):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
E/AndroidRuntime(19285):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
E/AndroidRuntime(19285):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
E/AndroidRuntime(19285):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
E/AndroidRuntime(19285):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
E/AndroidRuntime(19285):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
E/AndroidRuntime(19285):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
E/AndroidRuntime(19285):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
E/AndroidRuntime(19285):    at java.lang.Thread.run(Thread.java:856)

E/AndroidRuntime(19285): Caused by: java.lang.IllegalStateException: Not on the main thread
E/AndroidRuntime(19285):    at maps.as.i.b(Unknown Source)
E/AndroidRuntime(19285):    at maps.am.g.b(Unknown Source)
E/AndroidRuntime(19285):    at maps.ah.an.a(Unknown Source)
E/AndroidRuntime(19285):    at bgc.onTransact(SourceFile:167)
E/AndroidRuntime(19285):    at android.os.Binder.transact(Binder.java:326)
E/AndroidRuntime(19285):    at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.addMarker(Unknown Source)
E/AndroidRuntime(19285):    at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source)
E/AndroidRuntime(19285):    at com.paul.locations.Location$AttemptLoad.doInBackground(Location.java:194)
E/AndroidRuntime(19285):    at com.paul.locations.Location$AttemptLoad.doInBackground(Location.java:1)
E/AndroidRuntime(19285):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
E/AndroidRuntime(19285):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
E/AndroidRuntime(19285):    ... 5 more
I/Choreographer(19285): Skipped 474 frames!  The application may be doing too much work on its main thread.

W/SurfaceView(19285): CHECK surface infomation creating=false formatChanged=false sizeChanged=false visible=false visibleChanged=true surfaceChanged=true e redrawNeeded=false left=false top=false
E/WindowManager(19285): Activity com.paul.locations.Location has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4243bb10 that was e
E/WindowManager(19285): android.view.WindowLeaked: Activity com.paul.locations.Location has leaked window com.android.internal.policy.impl.w@4243bb10 that was originally added here
E/WindowManager(19285):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:408)
E/WindowManager(19285):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:311)
E/WindowManager(19285):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
E/WindowManager(19285):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
E/WindowManager(19285):     at android.view.Window$LocalWindowManager.addView(Window.java:552)
E/WindowManager(19285):     at android.app.Dialog.show(Dialog.java:277)
E/WindowManager(19285):     at com.paul.locations.Location$AttemptLoad.onPreExecute(Location.java:131)
E/WindowManager(19285):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
E/WindowManager(19285):     at android.os.AsyncTask.execute(AsyncTask.java:534)
E/WindowManager(19285):     at com.paul.locations.Location.onClick(Location.java:102)
E/WindowManager(19285):     at android.view.View.performClick(View.java:4203)
E/WindowManager(19285):     at android.view.View$PerformClick.run(View.java:17189)
E/WindowManager(19285):     at android.os.Handler.handleCallback(Handler.java:615)
E/WindowManager(19285):     at android.os.Handler.dispatchMessage(Handler.java:92)
E/WindowManager(19285):     at android.os.Looper.loop(Looper.java:137)
E/WindowManager(19285):     at android.app.ActivityThread.main(ActivityThread.java:4950)
E/WindowManager(19285):     at java.lang.reflect.Method.invokeNative(Native Method)
E/WindowManager(19285):     at java.lang.reflect.Method.invoke(Method.java:511)
E/WindowManager(19285):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
E/WindowManager(19285):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
E/WindowManager(19285):     at dalvik.system.NativeStart.main(Native Method)
googlemap.addMarker(new MarkerOptions()    
.position(new LatLng(la, lo))   
.title("Hello world"));