Android 从webservice获取谷歌地图标记图标

Android 从webservice获取谷歌地图标记图标,android,google-maps,google-maps-api-3,Android,Google Maps,Google Maps Api 3,JSON数据:我想要的是从服务器获取所有纬度、经度、post_标题、map_图标-我怎么做?我有异步任务的知识,我尝试过。我能做什么?我对谷歌地图一无所知,我正在研究它 { "action": "all_products", "success": 1, "all_products": [ { "ID": "5689", "post_title": "Fruitique", "l

JSON数据:我想要的是从服务器获取所有纬度、经度、post_标题、map_图标-我怎么做?我有异步任务的知识,我尝试过。我能做什么?我对谷歌地图一无所知,我正在研究它

    {
      "action": "all_products",
      "success": 1,
      "all_products": [
        {
          "ID": "5689",
          "post_title": "Fruitique",
          "latitude": "50.85632",
          "longitude": "0.58126",
          "map_icon": "http:/192.168.1.3/googlemapicons/fruitandveg.png"
        },
        {
          "ID": "5691",
          "post_title": "Gourmet Shop Delicatessen",
          "latitude": "50.85517",
          "longitude": "0.57734",
          "map_icon": "http:/192.168.1.3/googlemapicons/deli.png"
        },
        {
          "ID": "5693",
          "post_title": "Penbuckles Delicatessen",
          "latitude": "50.85692",
          "longitude": "0.59099",
          "map_icon": "http:/192.168.1.3/googlemapicons/10percent.png"
        },
        {
          "ID": "5699",
          "post_title": "Rainbows Sweetshop",
          "latitude": "50.856011",
          "longitude": "0.581003",
          "map_icon": "http:/192.168.1.3/googlemapicons/10percent.png"
        },
        {
          "ID": "5701",
          "post_title": "Sultans Supermarket",
          "latitude": "50.85517",
          "longitude": "0.57734",
          "map_icon": "http:/192.168.1.3/googlemapicons/save£5.png"
        },
        {
          "ID": "5705",
          "post_title": "Arcade Butchers",
          "latitude": "50.85632",
          "longitude": "0.58126",
          "map_icon": "http:/192.168.1.3/googlemapicons/save50p.png"
        }
      ]
    }
我所尝试的-

public class MainActivityMap extends AppCompatActivity implements OnMapReadyCallback {
    private GoogleMap mMap;
    ArrayList<All_products_lat_long> venueList;

protected int getLayoutId() {
    return R.layout.frnt_map_activity_map;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(getLayoutId());
    setUpMap();
}

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

@Override
public void onMapReady(GoogleMap map) {
    if (mMap != null) {
        new MarkerTask().execute();
    }
    mMap = map;
}

private void setUpMap() {
    ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);
}

/**
 * Run the demo-specific code.
 */

protected GoogleMap getMap() {
    return mMap;
}
private class MarkerTask extends AsyncTask<Void, Void, String> {

    private static final String LOG_TAG = "ExampleApp";

    private static final String SERVICE_URL = "https://www.towncitycards.com/webservice_action.php?action=all_products";

    // Invoked by execute() method of this object
    @Override
    protected String doInBackground(Void... args) {

        HttpURLConnection conn = null;
        final StringBuilder json = new StringBuilder();
        try {
            // Connect to the web service
            URL url = new URL(SERVICE_URL);
            conn = (HttpURLConnection) url.openConnection();
            InputStreamReader in = new InputStreamReader(conn.getInputStream());

            // Read the JSON data into the StringBuilder
            int read;
            char[] buff = new char[1024];
            while ((read = in.read(buff)) != -1) {
                json.append(buff, 0, read);
            }
        } catch (IOException e) {
            Log.e(LOG_TAG, "Error connecting to service", e);
            //throw new IOException("Error connecting to service", e); //uncaught
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }

        return json.toString();
    }

    // Executed after the complete execution of doInBackground() method
    @Override
    protected void onPostExecute(String json) {
        for (int k = 0; k < venueList.size(); k += 1) {

            try {

                double ln = Double.parseDouble(venueList.get(k).getLongitude());
                double la = Double.parseDouble(venueList.get(k).getLatitude());

                // De-serialize the JSON string into an array of city objects
                JSONArray jsonArray = new JSONArray(json);
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObj = jsonArray.getJSONObject(i);

                    LatLng latLng = new LatLng( Double.parseDouble(jsonObj.getString("latitude")),
                            Double.parseDouble( jsonObj.getString("longitude")));

                    //move CameraPosition on first result
                    if (i == 0) {
                        CameraPosition cameraPosition = new CameraPosition.Builder()
                                .target(latLng).zoom(13).build();

                        mMap.animateCamera(CameraUpdateFactory
                                .newCameraPosition(cameraPosition));
                    }

                    // Create a marker for each city in the JSON data.
                    mMap.addMarker(new MarkerOptions()
                            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
                            .title(jsonObj.getString("post_title"))
                            .position(latLng));
                }
            } catch (JSONException e) {
                Log.e(LOG_TAG, "Error processing JSON", e);
            }

        }
    }
}
public类MainActivityMap扩展了AppCompatActivity在MapReadyCallback上的实现{
私有谷歌地图;
ArrayList venueList;
受保护的int getLayoutId(){
返回R.layout.frnt\u map\u activity\u map;
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(getLayoutId());
setUpMap();
}
@凌驾
受保护的void onResume(){
super.onResume();
setUpMap();
}
@凌驾
已于4月1日公开作废(谷歌地图){
如果(mMap!=null){
新建MarkerTask().execute();
}
mMap=map;
}
私有void setUpMap(){
((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);
}
/**
*运行特定于演示的代码。
*/
受保护的GoogleMap getMap(){
返回mMap;
}
私有类MarkerTask扩展了AsyncTask{
私有静态最终字符串日志\u TAG=“ExampleApp”;
私有静态最终字符串服务\u URL=”https://www.towncitycards.com/webservice_action.php?action=all_products";
//由此对象的execute()方法调用
@凌驾
受保护字符串doInBackground(无效…参数){
HttpURLConnection conn=null;
final StringBuilder json=新StringBuilder();
试一试{
//连接到web服务
URL=新URL(服务URL);
conn=(HttpURLConnection)url.openConnection();
InputStreamReader in=新的InputStreamReader(conn.getInputStream());
//将JSON数据读入StringBuilder
int-read;
char[]buff=新字符[1024];
while((read=in.read(buff))!=-1){
append(buff,0,read);
}
}捕获(IOE异常){
Log.e(日志标签,“连接到服务时出错”,e);
//抛出新IOException(“连接到服务时出错”,e);//未捕获
}最后{
如果(conn!=null){
连接断开();
}
}
返回json.toString();
}
//在完全执行doInBackground()方法后执行
@凌驾
受保护的void onPostExecute(字符串json){
对于(int k=0;k
}

这会有所帮助

mapView = (MapView) rootView.findViewById(R.id.aboutUs_map_Maps);
    mapView.onCreate(savedInstanceState);
    mapView.onResume();

    MapsInitializer.initialize(getActivity().getApplicationContext());
    mapView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(GoogleMap googleMap) {
            googleMap = googleMap;
            if (ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            googleMap.setMyLocationEnabled(true);
            LatLng pune=new LatLng(18.5,73.8);
            googleMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)).anchor(0.0f,1.0f).position(pune).title("Pune City").snippet("Find the colleges around pune city"));
            CameraPosition cameraPosition=new CameraPosition.Builder().target(pune).zoom(12).build();
        }
    });

您可以更改
LatLng pune=新LatLng(18.5,73.8)的位置根据您的json数据设置pune位置

如果您使用的是marshmallow版本,还可以为地图授予运行时权限迄今为止您所做的工作??添加代码请查看编辑您现在还有什么问题?人们不会调试所有这些,所以请为他们缩小范围。我无法在上面的代码中找到错误。它没有运行。我希望使用异步任务的示例从Web服务器获取标记图标