简体   繁体   English

JSON解析以在Google Map上将标记传递标记添加到URL

[英]JSON Parsing to add marker on Google Map passing tag to URL

I have an URL where i will parse some information including latitude longitude and show them in map. 我有一个URL,我将在其中解析一些信息,包括纬度经度,并在地图中显示它们。 I have done parsing and added markers in the map too. 我已经完成解析并在地图中添加了标记。 But the markers are not displaying, only the map is displaying. 但是标记未显示,仅显示地图。 At first i thought the link is broken but then i got the information that the URL won't give any return until i pass tag. 起初,我以为链接已断开,但随后我得到的信息是,直到我通过标签,URL才会返回任何信息。 Can anyone explain what does that mean? 谁能解释这是什么意思?

Here is the URL and the sending data which includes the tag too. 这是URL和包含标签的发送数据。 Can anyone make this clear to me,how can i do that? 谁能对我说清楚,我该怎么做? URL: "some url" Sending data: {"tag":"getAvailableDriver","lat":41.022348,"lng":-91.966721} Here is my code: URL:“一些URL”发送数据:{“标记”:“ getAvailableDriver”,“纬度”:41.022348,“ lng”:-91.966721}这是我的代码:

MainActivity.java MainActivity.java

package com.hasibhasan.sampletask;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;

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;

public class MainActivity extends ActionBarActivity {
    private GoogleMap googlemap;
    private static String TAG_POSTS = "posts";
    private static String TAG_DRIVER = "driver";
    private static String TAG_ID = "id";
    private static String TAG_LATITUDE = "lat";
    private static String TAG_LONGITUDE = "lon";
    private static String TAG_DATETIME = "recorded_datetime";
    private static String TAG_USERID = "user_id";
    private static String TAG_STATE = "cabby_state";
    private static String TAG_VTYPE = "vehicleType";
    private static String TAG_DRIVERNAME = "driver_name";
    private static String TAG_PICNAME = "pic_name";
    private static String TAG_RATING = "rating";
    private static String TAG_CARMODEL = "car_model";
    private static String TAG_NUMBERSIT = "number_sit";
    private static String TAG_DISTANCE = "distance";
    private static String TAG_OPERATOR = "operator";
    private static String TAG_NEARESTDISTANCE = "nearest_distance";
    private static String TAG_NDISTANCE = "distance";
    private static String TAG_TIME = "time";
    private static String TAG_CARMODELS = "car_models";
    ArrayList<Taxi> taxi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        taxi = new ArrayList<Taxi>();
        new ParseJSONTask().execute();
        googlemap = ((MapFragment) getFragmentManager().findFragmentById(
                R.id.map)).getMap();

    }

    private class ParseJSONTask extends
            AsyncTask<Void, Void, List<MarkerOptions>> {
        @Override
        protected void onPreExecute() {

            super.onPreExecute();
        }

        @Override
        protected List<MarkerOptions> doInBackground(Void... params) {
            WebServiceHandler webServiceHandler = new WebServiceHandler();
            String jsonstr = webServiceHandler
                    .getJSONData("http://54.186.247.213/unicabi/mobileservice/CurrentLocationService.php");
            try {
                JSONObject jsonObject = new JSONObject(jsonstr);
                JSONArray postJson = jsonObject.getJSONArray(TAG_POSTS);
                List<MarkerOptions> markers = new ArrayList<MarkerOptions>();
                for (int i = 0; i < postJson.length(); i++) {
                    Taxi aTaxi = new Taxi();
                    JSONObject postObject = postJson.getJSONObject(i);
                    aTaxi.lat = postObject.getString(TAG_LATITUDE);
                    aTaxi.lon = postObject.getString(TAG_LONGITUDE);
                    aTaxi.driver_name = postObject.getString(TAG_DRIVERNAME);
                    // taxi.add(aTaxi);
                    double lati = Double.parseDouble(aTaxi.lat);
                    double lon = Double.parseDouble(aTaxi.lon);
                    markers.add(new MarkerOptions().title(aTaxi.driver_name)
                            .position(new LatLng(lati, lon)));

                    return markers;

                }

            } catch (Exception e) {
                e.printStackTrace();
            }
            return new ArrayList<MarkerOptions>();
        }

        @Override
        protected void onPostExecute(List<MarkerOptions> markers) {

            //super.onPostExecute(result);
            for (MarkerOptions marker : markers) {
                googlemap.addMarker(marker);
            }
        }
    }
}

WebHandlerService.java WebHandlerService.java

package com.hasibhasan.sampletask;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class WebServiceHandler {
    public WebServiceHandler() {

    }

    public String getJSONData(String url) {
        String response = null;
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;
        try {
            httpResponse = httpclient.execute(httpGet);
            httpEntity = httpResponse.getEntity();
            response = EntityUtils.toString(httpEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;

    }

}

Taxi.java Taxi.java

package com.hasibhasan.sampletask;

public class Taxi {
    public String posts = "";
    public String success = "";
    public String driver = "";
    public String id = "";
    public String lat = "";
    public String lon = "";
    public String recorded_datetime = "";
    public String vehicleType = "";
    public String driver_name = "";
    public String pic_name = "";
    public String rating = "";
    public String car_model = "";
    public String number_sit = "";
    public String distance = "";
    public String operator = "";
    public String nearest_distance = "";
    public String car_models = "";

}

I think the doInBackground is returning empty list as the url does not return anything to parse thus the PostExecute does not add any markers. 我认为doInBackground返回的是空列表,因为url不返回任何要解析的内容,因此PostExecute不添加任何标记。 can anybody figure this out? 有人可以解决吗? how to pass tag form the app to url when requesting so that the markers are being displayed. 如何在请求时将标记从应用程序传递到url,以便显示标记。 I am so lost. 我很迷失。

http://54.186.247.213/unicabi/mobileservice/CurrentLocationService.php网址未返回jsonString ...请检查您是否从该网址获取json。

Yep the link has a problem 是的,链接有问题

54.186.247.213/unicabi/mobileservice/CurrentLocationService.php 54.186.247.213/unicabi/mobileservice/CurrentLocationService.php

your php file should return basically json maybe you forget to encode your data to be in json. 您的php文件应该基本上返回json,也许您忘记了将数据编码为json。

example in php you should have something calling the method json_encode(your_data) PHP中的示例,您应该有一些调用方法json_encode(your_data)的东西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM